bc_ur/
ur_type.rs

1use crate::{utils::URTypeString, error::URError};
2
3use anyhow::{Error, Result, bail};
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct URType(String);
7
8impl URType {
9    /// Creates a new URType from the provided type.
10    pub fn new(ur_type: impl Into<String>) -> Result<URType> {
11        let ur_type = ur_type.into();
12        if !ur_type.is_ur_type() {
13            bail!(URError::InvalidType);
14        }
15        Ok(URType(ur_type))
16    }
17
18    /// Returns the String representation of the URType.
19    pub fn string(&self) -> &str {
20        &self.0
21    }
22}
23
24impl TryFrom<String> for URType {
25    type Error = Error;
26
27    fn try_from(value: String) -> Result<Self> {
28        URType::new(value)
29    }
30}
31
32impl TryFrom<&str> for URType {
33    type Error = Error;
34
35    fn try_from(value: &str) -> Result<Self> {
36        URType::new(value)
37    }
38}