yew_route_breadcrumbs/
lib.rs

1//! This crate is intended to be used with yew-router though it is not required
2//! to build this library.
3//!
4//! The BreadCrumbs derive macro is the main way to interact with this crate. It derives a
5//! [`BreadCrumbs`](./trait.BreadCrumbs.html) implementation block and a single `breadcrumbs`
6//! helper function of the same type as the [`BreadCrumbs`](./trait.BreadCrumbs.html) trait.
7//!
8//! It gives access to 2 attribute macros.
9//! - ### `breadcrumb`
10//! The breadcrumb attribute is used to create a breadcrumb. It expects the first argument
11//! to be a string literal that will be used as the breadcrumb text. The `route` argument
12//! can be used to set the breadcrumb route.
13//! - ### `breadcrumbs`
14//! The breadcrumbs attribute is used to mark the field as nested. This only functions on
15//! structs with a single unnamed field.
16//!
17//! ##### Example
18//! ```
19//! use yew_route_breadcrumbs::BreadCrumbs;
20//!
21//! #[derive(BreadCrumbs)]
22//! #[breadcrumb("Global1")]
23//! enum AppRoutes {
24//!     #[breadcrumb("Blog")]
25//!     Blog, // Global1 > Blog
26//!     #[breadcrumb("Auth")]
27//!     Auth(AuthRoutes),
28//!     #[breadcrumbs]
29//!     Admin(AdminRoutes)
30//! }
31//!
32//! #[derive(BreadCrumbs)]
33//! #[breadcrumb("Admin")]
34//! enum AdminRoutes {
35//!     #[breadcrumb("Users", route = "/admin/users")]
36//!     Users, // Global1 > Admin > Users
37//!     #[breadcrumb("Roles")]
38//!     Roles // Global1 > Admin > Roles
39//! }
40//!
41//! #[derive(BreadCrumbs)]
42//! enum AuthRoutes {
43//!     #[breadcrumb("Login", route = "/auth/login")]
44//!     Login, // Global1 > Auth > Login
45//!     #[breadcrumb("Register")]
46//!     Register // Global1 > Auth > Register
47//! }
48//! ```
49pub use yew_route_breadcrumbs_derive::BreadCrumbs;
50
51/// A single UI BreadCrumb. A Vector of `Crumbs` can be used to render
52/// the route position in the UI.
53#[derive(Debug, PartialEq, Clone)]
54pub struct CrumbOwned {
55    pub text: String,
56    pub route: Option<String>,
57}
58
59#[derive(Debug, PartialEq, Clone)]
60pub struct Crumb {
61    pub text: &'static str,
62    pub route: Option<&'static str>,
63}
64
65/// Helper trait used by the library
66pub trait BreadCrumbs {
67    /// The resulting vector should always contain atleast one element.
68    fn breadcrumbs(&self) -> Option<Vec<Crumb>>;
69
70    fn breadcrumbs_owned(&self) -> Option<Vec<CrumbOwned>> {
71        self.breadcrumbs().map(|item| {
72            item.into_iter()
73                .map(|item| CrumbOwned {
74                    text: item.text.to_string(),
75                    route: item.route.map(|item| item.to_string()),
76                })
77                .collect()
78        })
79    }
80}