Skip to main content

use_bond/
bond_descriptor.rs

1use std::fmt;
2
3use crate::BondValidationError;
4
5/// A lightweight bond descriptor or reference label.
6#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct BondDescriptor(String);
8
9impl BondDescriptor {
10    /// Creates a bond descriptor.
11    ///
12    /// # Errors
13    ///
14    /// Returns [`BondValidationError::EmptyDescriptor`] when `descriptor` is empty after trimming.
15    pub fn new(descriptor: &str) -> Result<Self, BondValidationError> {
16        let trimmed = descriptor.trim();
17        if trimmed.is_empty() {
18            Err(BondValidationError::EmptyDescriptor)
19        } else {
20            Ok(Self(trimmed.to_owned()))
21        }
22    }
23
24    /// Returns the descriptor text.
25    #[must_use]
26    pub fn as_str(&self) -> &str {
27        &self.0
28    }
29
30    /// Consumes the descriptor and returns the owned text.
31    #[must_use]
32    pub fn into_string(self) -> String {
33        self.0
34    }
35}
36
37impl AsRef<str> for BondDescriptor {
38    fn as_ref(&self) -> &str {
39        self.as_str()
40    }
41}
42
43impl TryFrom<&str> for BondDescriptor {
44    type Error = BondValidationError;
45
46    fn try_from(value: &str) -> Result<Self, Self::Error> {
47        Self::new(value)
48    }
49}
50
51impl fmt::Display for BondDescriptor {
52    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
53        formatter.write_str(self.as_str())
54    }
55}