spin_manifest/
lib.rs

1//! Configuration of an application for the Spin runtime.
2
3#![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
16/// Parses a V1 or V2 app manifest file into a [`AppManifest`].
17pub 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
22/// Parses a V1 or V2 app manifest into a [`AppManifest`].
23pub fn manifest_from_str(v1_or_v2_toml: &str) -> Result<AppManifest, Error> {
24    // TODO: would it be faster to parse into a toml::Table rather than parse twice?
25    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/// A Spin manifest schema version.
35#[derive(Debug, PartialEq)]
36pub enum ManifestVersion {
37    /// Spin manifest schema version 1.
38    V1,
39    /// Spin manifest schema version 2.
40    V2,
41}
42
43impl ManifestVersion {
44    /// Detects the Spin manifest schema version of the given TOML content.
45    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}