Skip to content

Commit

Permalink
Alter router display to use different characters
Browse files Browse the repository at this point in the history
  • Loading branch information
CathalMullan committed Sep 4, 2024
1 parent 6b7dc2a commit 9d7ad51
Show file tree
Hide file tree
Showing 8 changed files with 860 additions and 865 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

...
### Changed

- Router display now uses different characters to represent root and matchable nodes.

## [0.2.1] - 2024-09-04

Expand Down
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ fn main() -> Result<(), Box<dyn Error>> {

Routers can print their routes as an tree diagram.

`[*]` here represents nodes within the route tree that can be matched against.
- `` represents the root node.
- `` represents nodes within the tree that can be matched against.

Currenty, this doesn't handle split multi-byte characters well.

Expand All @@ -236,27 +237,28 @@ use std::error::Error;
use wayfind::Router;

const ROUTER_DISPLAY: &str = "
$
╰─ /
├─ pet [*]
│ ╰─ /
│ ├─ findBy
│ │ ├─ Status [*]
│ │ ╰─ Tags [*]
│ ╰─ {petId} [*]
│ ╰─ /uploadImage [*]
├─ store/
│ ├─ inventory [*]
│ ╰─ order [*]
│ ╰─ /
│ ╰─ {orderId} [*]
╰─ user [*]
╰─ /
├─ createWithList [*]
├─ log
│ ├─ in [*]
│ ╰─ out [*]
╰─ {username} [*]
├─ /
│ ├─ pet ●
│ │ ╰─ /
│ │ ├─ findBy
│ │ │ ├─ Status ●
│ │ │ ╰─ Tags ●
│ │ ╰─ {petId} ●
│ │ ╰─ /uploadImage ●
│ ├─ store/
│ │ ├─ inventory ●
│ │ ╰─ order ●
│ │ ╰─ /
│ │ ╰─ {orderId} ●
│ ╰─ user ●
│ ╰─ /
│ ├─ createWithList ●
│ ├─ log
│ │ ├─ in ●
│ │ ╰─ out ●
│ ╰─ {username} ●
╰─ {*catch_all} ●
";

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -278,7 +280,9 @@ fn main() -> Result<(), Box<dyn Error>> {
router.insert("/user/logout", 12)?;
router.insert("/user/{username}", 13)?;

assert_eq!(router.to_string(), ROUTER_DISPLAY.trim_end());
router.insert("{*catch_all}", 14)?;

assert_eq!(router.to_string(), ROUTER_DISPLAY.trim());
Ok(())
}
```
Expand Down
15 changes: 7 additions & 8 deletions src/node/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<T> Display for Node<T> {
) -> std::fmt::Result {
let constraint = node.constraint.as_ref().map(|c| String::from_utf8_lossy(c));
let key = match &node.kind {
NodeKind::Root => "$".to_string(),
NodeKind::Root => "".to_string(),
NodeKind::Static => String::from_utf8_lossy(&node.prefix).to_string(),
NodeKind::Dynamic => {
let name = String::from_utf8_lossy(&node.prefix);
Expand All @@ -31,16 +31,15 @@ impl<T> Display for Node<T> {
}
};

let value = node
.data
.as_ref()
.map_or(String::new(), |_node_data| " [*]".to_string());

if is_root {
writeln!(output, "{key}")?;
} else {
let branch = if is_last { "╰─" } else { "├─" };
writeln!(output, "{padding}{branch} {key}{value}")?;
if node.data.is_some() {
writeln!(output, "{padding}{branch} {key} ●")?;
} else {
writeln!(output, "{padding}{branch} {key}")?;
}
}

// Ensure we align children correctly
Expand Down Expand Up @@ -79,6 +78,6 @@ impl<T> Display for Node<T> {
};

debug_node(&mut output, self, &padding, true, true)?;
write!(f, "\n{}", output.trim_end())
write!(f, "{}", output.trim_end())
}
}
8 changes: 4 additions & 4 deletions tests/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ fn test_multiple_constraints() -> Result<(), Box<dyn Error>> {
router.insert("/posts/{year:even_year}/{slug:valid_slug}", 3)?;

insta::assert_snapshot!(router, @r###"
$
╰─ /
├─ p
│ ├─ osts/
│ │ ╰─ {year:even_year}
│ │ ╰─ /
│ │ ╰─ {slug:valid_slug} [*]
│ │ ╰─ {slug:valid_slug}
│ ╰─ rofile/
│ ╰─ {username:length_3_to_10}
│ ╰─ .
│ ╰─ {ext:png_or_jpg} [*]
│ ╰─ {ext:png_or_jpg}
╰─ user/
╰─ {name:length_3_to_10}
╰─ /
╰─ {id:year_1000_to_10000} [*]
╰─ {id:year_1000_to_10000}
"###);

assert_router_matches!(router, {
Expand Down
Loading

0 comments on commit 9d7ad51

Please sign in to comment.