Skip to main content

api_bones/
has_id.rs

1//! `HasId` — universal "this resource has an identifier" trait.
2//!
3//! Transport-agnostic. Lets helpers like `created_under` (in socle) compose
4//! a Location header from a route prefix + the resource's id without
5//! coupling DTOs to HTTP paths.
6
7use core::fmt::Display;
8
9pub trait HasId {
10    type Id: Display;
11    fn id(&self) -> &Self::Id;
12}
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17
18    struct Thing {
19        id: u64,
20    }
21
22    impl HasId for Thing {
23        type Id = u64;
24        fn id(&self) -> &u64 {
25            &self.id
26        }
27    }
28
29    #[test]
30    fn id_is_displayable() {
31        let t = Thing { id: 42 };
32        assert_eq!(format!("{}", t.id()), "42");
33    }
34}