Skip to main content

spin_templates/
app_info.rs

1// Information about the application manifest that is of
2// interest to the template system.  spin_loader does too
3// much processing to fit our needs here.
4
5use std::path::Path;
6
7pub(crate) struct AppInfo {
8    manifest_format: u32,
9}
10
11impl AppInfo {
12    pub fn from_file(manifest_path: &Path) -> Option<anyhow::Result<AppInfo>> {
13        if manifest_path.exists() {
14            Some(Self::from_existent_file(manifest_path))
15        } else {
16            None
17        }
18    }
19
20    fn from_existent_file(manifest_path: &Path) -> anyhow::Result<Self> {
21        let manifest_str = std::fs::read_to_string(manifest_path)?;
22        Self::from_manifest_text(&manifest_str)
23    }
24
25    fn from_manifest_text(manifest_str: &str) -> anyhow::Result<Self> {
26        let manifest_version = spin_manifest::ManifestVersion::detect(manifest_str)?;
27        let manifest_format = match manifest_version {
28            spin_manifest::ManifestVersion::V1 => 1,
29            spin_manifest::ManifestVersion::V2 => 2,
30        };
31        Ok(Self { manifest_format })
32    }
33
34    pub fn manifest_format(&self) -> u32 {
35        self.manifest_format
36    }
37}
38
39#[cfg(test)]
40mod test {
41    use super::*;
42
43    #[test]
44    fn can_detect_v1_manifest_format() {
45        let manifest = r#"spin_manifest_version = "1"
46        name = "test"
47        version = "1.2.3"
48        trigger = { type = "http" }
49
50        [[component]]
51        id = "test"
52        source = "test.wasm"
53        [component.trigger]
54        route = "/"
55        "#;
56
57        let info = AppInfo::from_manifest_text(manifest).unwrap();
58        assert_eq!(1, info.manifest_format);
59    }
60
61    #[test]
62    fn can_detect_v2_manifest_format() {
63        let manifest = r#"spin_manifest_version = 2
64        name = "test"
65        version = "1.2.3"
66
67        [[trigger.http]]
68        route = "/"
69        component = "test"
70
71        [component.test]
72        source = "test.wasm"
73        "#;
74
75        let info = AppInfo::from_manifest_text(manifest).unwrap();
76        assert_eq!(2, info.manifest_format);
77    }
78}