use crate::json::JsonSource;
pub struct VersionAndData {
pub version: String,
pub data: serde_json::Value,
}
pub fn detect_version<T: JsonSource>(source: T) -> std::io::Result<String> {
detect_version_with(source).map(|r| r.version)
}
pub fn detect_version_with<T: JsonSource>(source: T) -> std::io::Result<VersionAndData> {
let json_value: serde_json::Value = source.parse()?;
let version = json_value
.get("document")
.and_then(|doc| doc.get("csaf_version"))
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Could not detect CSAF version. Make sure the document has a 'document.csaf_version' field",
)
})?;
Ok(VersionAndData {
version,
data: json_value,
})
}