sophia_api/prefix/
_trait.rs

1use super::*;
2use std::borrow::Borrow;
3
4/// Marker trait guaranteeing that the underlying `str` is a valid Turtle/SPARQL prefix.
5pub trait IsPrefix: Borrow<str> {}
6
7//
8
9/// Automatic trait for [`IsPrefix`], providing cheap conversion to [`Prefix`].
10pub trait AsPrefix {
11    /// Extract an [`Prefix`] wrapping the underlying `str`.
12    fn as_prefix(&self) -> Prefix<&str>;
13}
14
15impl<T: IsPrefix> AsPrefix for T {
16    fn as_prefix(&self) -> Prefix<&str> {
17        Prefix::new_unchecked(self.borrow())
18    }
19}
20
21#[cfg(test)]
22#[allow(clippy::unused_unit)] // test_case! generated warnings
23mod test {
24    use super::*;
25    use test_case::test_case;
26
27    #[test_case(""; "empty")]
28    #[test_case("a")]
29    #[test_case("foo")]
30    #[test_case("é.hê"; "with dot and accents")]
31    fn valid_prefix(p: &str) {
32        assert!(is_valid_prefix(p));
33        assert!(Prefix::new(p).is_ok());
34    }
35
36    #[test_case(" "; "space")]
37    #[test_case("1a")]
38    #[test_case("a."; "ending with dot")]
39    fn invalid_prefix(p: &str) {
40        assert!(!is_valid_prefix(p));
41        assert!(Prefix::new(p).is_err());
42    }
43}