bc_ur/
ur_type.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::{utils::URTypeString, error::URError};

use anyhow::{Error, Result, bail};

#[derive(Debug, Clone, PartialEq)]
pub struct URType(String);

impl URType {
    /// Creates a new URType from the provided type.
    pub fn new(ur_type: impl Into<String>) -> Result<URType> {
        let ur_type = ur_type.into();
        if !ur_type.is_ur_type() {
            bail!(URError::InvalidType);
        }
        Ok(URType(ur_type))
    }

    /// Returns the String representation of the URType.
    pub fn string(&self) -> &str {
        &self.0
    }
}

impl TryFrom<String> for URType {
    type Error = Error;

    fn try_from(value: String) -> Result<Self> {
        URType::new(value)
    }
}

impl TryFrom<&str> for URType {
    type Error = Error;

    fn try_from(value: &str) -> Result<Self> {
        URType::new(value)
    }
}