harmos_domain/inventory/
operations.rs1use std::collections::{BTreeMap, BTreeSet};
18use crate::operations::OperationDefinition;
22
23#[derive(Debug, thiserror::Error)]
34pub enum Error {
35 #[error("the operation definitions differ from the admitted catalog")]
36 Mismatch,
37
38 #[error("operation inventory is invalid: {source}")]
39 Invalid {
40 #[source]
41 source: crate::Error,
42 },
43}
44
45pub fn validate(operations: &[OperationDefinition]) -> Result<(), Error> {
52 let mut ids = BTreeSet::new();
53 for operation in operations {
54 operation.validate().map_err(|source| Error::Invalid { source })?;
55 if !ids.insert(&operation.id) {
56 return Err(Error::Invalid {
57 source: crate::Error::DuplicateOperation { id: operation.id.as_str().to_owned() },
58 });
59 }
60 }
61 Ok(())
62}
63
64pub fn validate_match(expected: &[OperationDefinition], actual: &[OperationDefinition]) -> Result<(), Error> {
65 validate(expected)?;
66 validate(actual)?;
67 let expected: BTreeMap<_, _> = expected.iter().map(|entry| (&entry.id, entry)).collect();
68 if expected.len() != actual.len() || actual.iter().any(|entry| expected.get(&entry.id).is_none_or(|declared| *declared != entry)) {
69 return Err(Error::Mismatch);
70 }
71 Ok(())
72}
73
74