Skip to main content

haystack_core/kinds/
uri.rs

1use std::fmt;
2
3/// A Haystack URI value wrapping an arbitrary string.
4///
5/// URIs represent resource identifiers such as URLs, file paths, or URNs.
6/// In Zinc encoding a URI is delimited by back-ticks: `` `http://example.com` ``.
7/// No validation is performed on the contained string.
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9pub struct Uri(pub String);
10
11impl Uri {
12    /// Creates a new [`Uri`] from any value that can be converted into a `String`.
13    pub fn new(val: impl Into<String>) -> Self {
14        Self(val.into())
15    }
16
17    /// Returns a string slice of the underlying URI value.
18    pub fn val(&self) -> &str {
19        &self.0
20    }
21}
22
23impl fmt::Display for Uri {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(f, "{}", self.0)
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn uri_display() {
35        let u = Uri::new("http://example.com");
36        assert_eq!(u.to_string(), "http://example.com");
37    }
38
39    #[test]
40    fn uri_equality() {
41        assert_eq!(Uri::new("http://a.com"), Uri::new("http://a.com"));
42        assert_ne!(Uri::new("http://a.com"), Uri::new("http://b.com"));
43    }
44
45    #[test]
46    fn uri_val() {
47        let u = Uri::new("http://example.com");
48        assert_eq!(u.val(), "http://example.com");
49    }
50}