use_stoichiometry/
excess_reagent.rs1use std::fmt;
2
3use crate::StoichiometryValidationError;
4
5#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct ExcessReagent(String);
8
9impl ExcessReagent {
10 pub fn new(label: &str) -> Result<Self, StoichiometryValidationError> {
17 let label = label.trim();
18
19 if label.is_empty() {
20 Err(StoichiometryValidationError::EmptyExcessReagentLabel)
21 } else {
22 Ok(Self(label.to_owned()))
23 }
24 }
25
26 #[must_use]
28 pub fn as_str(&self) -> &str {
29 &self.0
30 }
31
32 #[must_use]
34 pub fn into_string(self) -> String {
35 self.0
36 }
37}
38
39impl AsRef<str> for ExcessReagent {
40 fn as_ref(&self) -> &str {
41 self.as_str()
42 }
43}
44
45impl TryFrom<&str> for ExcessReagent {
46 type Error = StoichiometryValidationError;
47
48 fn try_from(value: &str) -> Result<Self, Self::Error> {
49 Self::new(value)
50 }
51}
52
53impl fmt::Display for ExcessReagent {
54 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
55 formatter.write_str(self.as_str())
56 }
57}