Skip to content

Commit

Permalink
chore(router): tree: improve debug
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Oct 27, 2022
1 parent 3d43047 commit c31984d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
52 changes: 51 additions & 1 deletion viz-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod tests {
async_trait,
types::{Params, RouteInfo},
Body, Error, Handler, HandlerExt, IntoResponse, Method, Request, RequestExt, Response,
Result, StatusCode, Transform,
ResponseExt, Result, StatusCode, Transform,
};

use crate::{any, get, Resources, Route, Router, Tree};
Expand Down Expand Up @@ -455,6 +455,56 @@ mod tests {
Ok(())
}

#[test]
fn debug() -> Result<()> {
let search = Route::new().get(|_: Request| async { Ok(Response::text("search")) });

let orgs = Resources::default()
.index(|_: Request| async { Ok(Response::text("list posts")) })
.create(|_: Request| async { Ok(Response::text("create post")) })
.show(|_: Request| async { Ok(Response::text("show post")) });

let settings = Router::new()
.get("/", |_: Request| async { Ok(Response::text("settings")) })
.get("/:page", |_: Request| async {
Ok(Response::text("setting page"))
});

let api = Router::new().route("/search", search.clone());

let app = Router::new()
.get("/", |_: Request| async { Ok(Response::text("index")) })
.route("search", search)
.resources(":org", orgs)
.nest("settings", settings)
.nest("api", api);

let tree: Tree = app.into();

assert_eq!(format!("{:#?}", tree), "Tree {
method: GET,
paths:
/ •0
├── api/search •6
├── se
│ ├── arch •1
│ └── ttings •4
│ └── /
│ └── : •5
└── : •2
└── /
└── : •3
,
method: POST,
paths:
/
└── : •0
,
}");

Ok(())
}

fn client(method: Method, path: &str) -> (Request, Method, String) {
(
Request::builder()
Expand Down
8 changes: 7 additions & 1 deletion viz-router/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl From<Router> for Tree {

impl Debug for Tree {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("Tree").finish()
self.as_ref()
.iter()
.fold(f.debug_struct("Tree"), |mut d, (m, t)| {
d.field("method", m).field("paths", &t.node);
d
})
.finish()
}
}

0 comments on commit c31984d

Please sign in to comment.