1#![deny(missing_docs)]
4
5pub mod compat;
6pub mod error;
7pub mod normalize;
8pub mod schema;
9
10use std::path::Path;
11
12use schema::v2::AppManifest;
13
14pub use error::Error;
15
16pub fn manifest_from_file(path: impl AsRef<Path>) -> Result<AppManifest, Error> {
18 let manifest_str = std::fs::read_to_string(path)?;
19 manifest_from_str(&manifest_str)
20}
21
22pub fn manifest_from_str(v1_or_v2_toml: &str) -> Result<AppManifest, Error> {
24 match ManifestVersion::detect(v1_or_v2_toml)? {
26 ManifestVersion::V1 => {
27 let deserialized_v1 = toml::from_str(v1_or_v2_toml)?;
28 compat::v1_to_v2_app(deserialized_v1)
29 }
30 ManifestVersion::V2 => Ok(toml::from_str(v1_or_v2_toml)?),
31 }
32}
33
34#[derive(Debug, PartialEq)]
36pub enum ManifestVersion {
37 V1,
39 V2,
41}
42
43impl ManifestVersion {
44 pub fn detect(s: &str) -> Result<Self, Error> {
46 let schema::VersionProbe {
47 spin_manifest_version,
48 } = toml::from_str(s)?;
49 if spin_manifest_version.as_str() == Some("1") {
50 Ok(Self::V1)
51 } else if spin_manifest_version.as_integer() == Some(2) {
52 Ok(Self::V2)
53 } else {
54 Err(Error::InvalidVersion(spin_manifest_version.to_string()))
55 }
56 }
57}