#![deny(missing_docs)]
pub mod compat;
pub mod error;
pub mod normalize;
pub mod schema;
use std::path::Path;
use schema::v2::AppManifest;
pub use error::Error;
pub fn manifest_from_file(path: impl AsRef<Path>) -> Result<AppManifest, Error> {
let manifest_str = std::fs::read_to_string(path)?;
manifest_from_str(&manifest_str)
}
pub fn manifest_from_str(v1_or_v2_toml: &str) -> Result<AppManifest, Error> {
match ManifestVersion::detect(v1_or_v2_toml)? {
ManifestVersion::V1 => {
let deserialized_v1 = toml::from_str(v1_or_v2_toml)?;
compat::v1_to_v2_app(deserialized_v1)
}
ManifestVersion::V2 => Ok(toml::from_str(v1_or_v2_toml)?),
}
}
#[derive(Debug, PartialEq)]
pub enum ManifestVersion {
V1,
V2,
}
impl ManifestVersion {
pub fn detect(s: &str) -> Result<Self, Error> {
let schema::VersionProbe {
spin_manifest_version,
} = toml::from_str(s)?;
if spin_manifest_version.as_str() == Some("1") {
Ok(Self::V1)
} else if spin_manifest_version.as_integer() == Some(2) {
Ok(Self::V2)
} else {
Err(Error::InvalidVersion(spin_manifest_version.to_string()))
}
}
}