libhaystack/haystack/val/uri.rs
1// Copyright (C) 2020 - 2022, J2 Innovations
2
3//! Haystack Uri
4
5use crate::haystack::val::Value;
6
7/// Haystack `Uri`
8///
9/// # Example
10/// Create `Uri` value
11/// ```
12/// use libhaystack::val::*;
13///
14/// // Create `Uri` from `&str` primitive
15/// let uri_value = Value::from(Uri::from("/an/uri"));
16/// assert!(uri_value.is_uri());
17///
18/// // Get the uri value from the Value
19/// assert_eq!(Uri::try_from(&uri_value).unwrap(), Uri::from("/an/uri"));
20/// ```
21#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Default)]
22pub struct Uri {
23 pub value: String,
24}
25
26impl Uri {
27 /// Make a [Uri](crate::val::Uri) from a `&str`
28 pub fn make(val: &str) -> Self {
29 Uri { value: val.into() }
30 }
31}
32
33// Make a Haystack `Uri` from a string value
34impl From<&str> for Uri {
35 fn from(value: &str) -> Self {
36 Uri {
37 value: String::from(value),
38 }
39 }
40}
41
42/// Converts from `Uri` to a `Uri` `Value`
43impl From<Uri> for Value {
44 fn from(value: Uri) -> Self {
45 Value::Uri(value)
46 }
47}
48
49/// Tries to convert from `Value` to a `Uri`
50impl TryFrom<&Value> for Uri {
51 type Error = &'static str;
52 fn try_from(value: &Value) -> Result<Self, Self::Error> {
53 match value {
54 Value::Uri(v) => Ok(v.clone()),
55 _ => Err("Value is not an `Uri`"),
56 }
57 }
58}