use crate::error::{Error, Result};
use crate::spec::version::validate_version;
use crate::spec::ServiceActivatorSpec;
use serde_yaml::Value as YamlValue;
pub struct ServiceSpecYamlParser;
impl ServiceSpecYamlParser {
pub fn parse_value(yaml: &YamlValue) -> Result<ServiceActivatorSpec> {
let _v = validate_version(yaml)?;
let service_root = yaml
.get("service-activator")
.ok_or_else(|| Error::serialization("missing 'service-activator'"))?;
if !service_root.is_mapping() {
return Err(Error::serialization(
"'service-activator' must be a mapping",
));
}
let name_val = service_root
.get("ref-name")
.ok_or_else(|| Error::serialization("service-activator.ref-name required"))?;
let name_str = name_val
.as_str()
.ok_or_else(|| Error::serialization("service-activator.ref-name must be string"))?;
if name_str.is_empty() {
return Err(Error::serialization(
"service-activator.ref-name must not be empty",
));
}
let from_val = service_root
.get("from")
.ok_or_else(|| Error::serialization("service-activator.from required"))?;
let from_str = from_val
.as_str()
.ok_or_else(|| Error::serialization("service-activator.from must be string"))?;
if from_str.is_empty() {
return Err(Error::serialization(
"service-activator.from must not be empty",
));
}
let to_val = service_root
.get("to")
.ok_or_else(|| Error::serialization("service-activator.to required"))?;
let to_str = to_val
.as_str()
.ok_or_else(|| Error::serialization("service-activator.to must be string"))?;
if to_str.is_empty() {
return Err(Error::serialization(
"service-activator.to must not be empty",
));
}
let id_opt = service_root
.get("id")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
if let Some(ref idv) = id_opt {
if idv.is_empty() {
return Err(Error::serialization(
"service-activator.id must not be empty",
));
}
}
Ok(match id_opt {
Some(id) => ServiceActivatorSpec::with_id(id, name_str, from_str, to_str),
None => ServiceActivatorSpec::new(name_str, from_str, to_str),
})
}
pub fn parse_str(raw: &str) -> Result<ServiceActivatorSpec> {
let val: YamlValue = serde_yaml::from_str(raw)
.map_err(|e| Error::serialization(format!("yaml parse error: {e}")))?;
Self::parse_value(&val)
}
}