harmos_domain/inventory/
operations.rs1use std::collections::{BTreeMap, BTreeSet};
18use crate::operations::Definition;
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: &[Definition]) -> 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 { source: crate::Error::DuplicateOperation { id: operation.id.as_str().to_owned() } });
57 }
58 }
59 Ok(())
60}
61
62pub fn validate_match(expected: &[Definition], actual: &[Definition]) -> Result<(), Error> {
63 validate(expected)?;
64 validate(actual)?;
65 let expected: BTreeMap<_, _> = expected.iter().map(|entry| (&entry.id, entry)).collect();
66 if expected.len() != actual.len() || actual.iter().any(|entry| expected.get(&entry.id).is_none_or(|declared| *declared != entry)) {
67 return Err(Error::Mismatch);
68 }
69 Ok(())
70}
71
72