use super::{ItemType, ParsedRegisteredItem, RegisteredItem, ENV};
use crate::Error;
use serde_yaml::Value;
use std::collections::HashMap;
use tracing::trace;
pub async fn parse_configuration_item(
itype: ItemType,
map: &HashMap<String, Value>,
) -> Result<ParsedRegisteredItem, Error> {
let keys: Vec<String> = map.keys().cloned().collect();
let first_key = keys.first().ok_or(Error::ConfigFailedValidation(format!(
"unable to determine {} key",
itype
)))?;
trace!("validating item {} of type {}", first_key, itype);
let item = get_item(&itype, first_key)?;
let content = map
.get(first_key)
.ok_or(Error::ConfigFailedValidation(format!(
"unable to validate {} key {}",
itype, first_key
)))?;
let content_str = serde_yaml::to_string(content)?;
item.format.validate(&content_str)?;
trace!("Format for {} validated", first_key);
Ok(ParsedRegisteredItem {
creator: item.creator,
config: content.clone(),
})
}
fn get_item(itype: &ItemType, key: &String) -> Result<RegisteredItem, Error> {
match ENV.read() {
Ok(lock) => {
match lock.get(itype) {
Some(i) => {
if let Some(item) = i.get(key) {
return Ok(item.clone());
}
}
None => return Err(Error::UnableToSecureLock),
};
}
Err(_) => return Err(Error::UnableToSecureLock),
};
Err(Error::ConfigurationItemNotFound(key.clone()))
}