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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::fmt;
use crate::{Catalyst, ReactionValidationError, Solvent};
/// A lightweight reaction condition label.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ReactionCondition {
/// Temperature descriptor.
Temperature(String),
/// Pressure descriptor.
Pressure(String),
/// Catalyst descriptor.
Catalyst(Catalyst),
/// Solvent descriptor.
Solvent(Solvent),
/// Heat label.
Heat,
/// Light label.
Light,
/// Electrolysis label.
Electrolysis,
/// Custom condition label and optional value.
Custom {
/// Custom condition label.
label: String,
/// Optional custom condition value.
value: Option<String>,
},
}
impl ReactionCondition {
/// Creates a temperature condition descriptor.
///
/// # Errors
///
/// Returns [`ReactionValidationError::EmptyTemperatureLabel`] when `label` is empty after
/// trimming.
pub fn temperature(label: &str) -> Result<Self, ReactionValidationError> {
let trimmed = label.trim();
if trimmed.is_empty() {
Err(ReactionValidationError::EmptyTemperatureLabel)
} else {
Ok(Self::Temperature(trimmed.to_owned()))
}
}
/// Creates a pressure condition descriptor.
///
/// # Errors
///
/// Returns [`ReactionValidationError::EmptyPressureLabel`] when `label` is empty after
/// trimming.
pub fn pressure(label: &str) -> Result<Self, ReactionValidationError> {
let trimmed = label.trim();
if trimmed.is_empty() {
Err(ReactionValidationError::EmptyPressureLabel)
} else {
Ok(Self::Pressure(trimmed.to_owned()))
}
}
/// Creates a catalyst condition descriptor.
///
/// # Errors
///
/// Returns [`ReactionValidationError::EmptyCatalystLabel`] when `label` is empty after
/// trimming.
pub fn catalyst(label: &str) -> Result<Self, ReactionValidationError> {
Ok(Self::Catalyst(Catalyst::new(label)?))
}
/// Creates a solvent condition descriptor.
///
/// # Errors
///
/// Returns [`ReactionValidationError::EmptySolventLabel`] when `label` is empty after
/// trimming.
pub fn solvent(label: &str) -> Result<Self, ReactionValidationError> {
Ok(Self::Solvent(Solvent::new(label)?))
}
/// Creates a custom condition descriptor.
///
/// # Errors
///
/// Returns [`ReactionValidationError::EmptyConditionLabel`] when `label` is empty after
/// trimming, or [`ReactionValidationError::EmptyConditionValue`] when `value` is present but
/// empty after trimming.
pub fn custom(label: &str, value: Option<&str>) -> Result<Self, ReactionValidationError> {
let label = label.trim();
if label.is_empty() {
return Err(ReactionValidationError::EmptyConditionLabel);
}
let value = value
.map(str::trim)
.map(|trimmed| {
if trimmed.is_empty() {
Err(ReactionValidationError::EmptyConditionValue)
} else {
Ok(trimmed.to_owned())
}
})
.transpose()?;
Ok(Self::Custom {
label: label.to_owned(),
value,
})
}
/// Validates this condition descriptor.
///
/// # Errors
///
/// Returns a [`ReactionValidationError`] when a directly constructed condition contains an
/// empty label or value.
pub fn validate(&self) -> Result<(), ReactionValidationError> {
match self {
Self::Temperature(label) if label.trim().is_empty() => {
Err(ReactionValidationError::EmptyTemperatureLabel)
},
Self::Pressure(label) if label.trim().is_empty() => {
Err(ReactionValidationError::EmptyPressureLabel)
},
Self::Custom { label, .. } if label.trim().is_empty() => {
Err(ReactionValidationError::EmptyConditionLabel)
},
Self::Custom {
value: Some(value), ..
} if value.trim().is_empty() => Err(ReactionValidationError::EmptyConditionValue),
_ => Ok(()),
}
}
}
impl From<Catalyst> for ReactionCondition {
fn from(catalyst: Catalyst) -> Self {
Self::Catalyst(catalyst)
}
}
impl From<Solvent> for ReactionCondition {
fn from(solvent: Solvent) -> Self {
Self::Solvent(solvent)
}
}
impl fmt::Display for ReactionCondition {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Temperature(label) => write!(formatter, "temperature: {label}"),
Self::Pressure(label) => write!(formatter, "pressure: {label}"),
Self::Catalyst(catalyst) => write!(formatter, "catalyst: {catalyst}"),
Self::Solvent(solvent) => write!(formatter, "solvent: {solvent}"),
Self::Heat => formatter.write_str("heat"),
Self::Light => formatter.write_str("light"),
Self::Electrolysis => formatter.write_str("electrolysis"),
Self::Custom { label, value } => match value {
Some(value) => write!(formatter, "{label}: {value}"),
None => formatter.write_str(label),
},
}
}
}