#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParameterUse {
In,
Out,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParameterSource {
Version,
PreAdopted,
Ecosystem,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cardinality {
pub min: u32,
pub max: Option<u32>,
}
impl Cardinality {
#[must_use]
pub const fn admits(self, count: u32) -> bool {
count >= self.min
&& match self.max {
Some(max) => count <= max,
None => true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Parameter {
pub name: &'static str,
pub usage: ParameterUse,
pub cardinality: Cardinality,
pub type_code: Option<&'static str>,
pub scope: &'static [&'static str],
pub parts: &'static [Parameter],
pub source: ParameterSource,
}
impl Parameter {
#[must_use]
pub fn part(&self, name: &str) -> Option<&'static Parameter> {
self.parts.iter().find(|part| part.name == name)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Operation {
pub url: &'static str,
pub resource: &'static str,
pub code: &'static str,
pub system: bool,
pub type_level: bool,
pub instance: bool,
pub parameters: &'static [Parameter],
}
impl Operation {
#[must_use]
pub fn parameter(&self, usage: ParameterUse, name: &str) -> Option<&'static Parameter> {
self.parameters
.iter()
.find(|parameter| parameter.usage == usage && parameter.name == name)
}
pub fn parameters_of(&self, usage: ParameterUse) -> impl Iterator<Item = &'static Parameter> {
self.parameters
.iter()
.filter(move |parameter| parameter.usage == usage)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParametersError {
Unnamed {
operation: &'static str,
},
Undeclared {
operation: &'static str,
name: std::string::String,
},
Repeated {
operation: &'static str,
name: &'static str,
},
Missing {
operation: &'static str,
name: &'static str,
},
MissingValue {
operation: &'static str,
name: &'static str,
},
WrongType {
operation: &'static str,
name: &'static str,
expected: &'static str,
},
}
impl std::fmt::Display for ParametersError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unnamed { operation } => {
write!(f, "{operation}: a parameter has no name")
}
Self::Undeclared { operation, name } => {
write!(f, "{operation}: parameter `{name}` is not declared")
}
Self::Repeated { operation, name } => {
write!(f, "{operation}: parameter `{name}` is given more than once")
}
Self::Missing { operation, name } => {
write!(f, "{operation}: parameter `{name}` is required")
}
Self::MissingValue { operation, name } => {
write!(f, "{operation}: parameter `{name}` has no value")
}
Self::WrongType {
operation,
name,
expected,
} => write!(f, "{operation}: parameter `{name}` is not a {expected}"),
}
}
}
impl std::error::Error for ParametersError {}