Skip to main content

a3s_code_core/moli_runtime/
manifest.rs

1pub const MOLI_REPOSITORY_URL: &str = "https://github.com/lexmount/moli";
2pub const DEFAULT_MOLI_VERSION: &str = "1.1.1";
3
4#[cfg(test)]
5#[derive(Debug, Clone, serde::Deserialize)]
6struct RuntimeManifest {
7    schema: String,
8    repository: String,
9    version: String,
10    assets: std::collections::HashMap<String, ManifestAssetOwned>,
11}
12
13#[cfg(test)]
14#[derive(Debug, Clone, serde::Deserialize)]
15struct ManifestAssetOwned {
16    archive: String,
17    format: String,
18    sha256: String,
19}
20
21#[derive(Debug, Clone)]
22pub(crate) struct ManifestAsset {
23    pub(crate) archive: String,
24    pub(crate) format: &'static str,
25    pub(crate) sha256: &'static str,
26}
27
28pub(crate) fn default_version() -> &'static str {
29    DEFAULT_MOLI_VERSION
30}
31
32#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
33pub(crate) fn current_target() -> Option<&'static str> {
34    Some("aarch64-apple-darwin")
35}
36
37#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
38pub(crate) fn current_target() -> Option<&'static str> {
39    Some("x86_64-apple-darwin")
40}
41
42#[cfg(all(target_os = "linux", target_arch = "aarch64", target_env = "gnu"))]
43pub(crate) fn current_target() -> Option<&'static str> {
44    Some("aarch64-unknown-linux-gnu")
45}
46
47#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
48pub(crate) fn current_target() -> Option<&'static str> {
49    Some("x86_64-unknown-linux-gnu")
50}
51
52#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
53pub(crate) fn current_target() -> Option<&'static str> {
54    Some("aarch64-pc-windows-msvc")
55}
56
57#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
58pub(crate) fn current_target() -> Option<&'static str> {
59    Some("x86_64-pc-windows-msvc")
60}
61
62#[cfg(not(any(
63    all(target_os = "macos", target_arch = "aarch64"),
64    all(target_os = "macos", target_arch = "x86_64"),
65    all(target_os = "linux", target_arch = "aarch64", target_env = "gnu"),
66    all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"),
67    all(target_os = "windows", target_arch = "aarch64"),
68    all(target_os = "windows", target_arch = "x86_64")
69)))]
70pub(crate) fn current_target() -> Option<&'static str> {
71    None
72}
73
74pub(crate) fn executable_name() -> &'static str {
75    #[cfg(windows)]
76    {
77        "moli.exe"
78    }
79    #[cfg(not(windows))]
80    {
81        "moli"
82    }
83}
84
85pub(crate) fn asset_for(target: &str) -> Option<ManifestAsset> {
86    // Keep this table in lockstep with resources/moli-runtime-manifest.json.
87    // Static values avoid parsing user-controlled files during runtime.
88    let (archive, format, sha256) = match target {
89        "aarch64-apple-darwin" => (
90            "moli-aarch64-apple-darwin.tar.gz",
91            "tar.gz",
92            "56deed4634b9c77641ce31f3802b9bb3f32c6d7f28073f73901540429a29864b",
93        ),
94        "x86_64-apple-darwin" => (
95            "moli-x86_64-apple-darwin.tar.gz",
96            "tar.gz",
97            "bb4f80d6a2786909457a66675ec5cd2118038afaaedeac0d90f9911427d38f56",
98        ),
99        "aarch64-unknown-linux-gnu" => (
100            "moli-aarch64-unknown-linux-gnu.tar.gz",
101            "tar.gz",
102            "549484765476b8dd3fd93ebf59a089e4424425a961c14874974a88bba6d8b5b4",
103        ),
104        "x86_64-unknown-linux-gnu" => (
105            "moli-x86_64-unknown-linux-gnu.tar.gz",
106            "tar.gz",
107            "7b3eb9cbbf2cc8bd5ea9ef4a5bdb24cee2df35d26da621216a8b69c2aff3ebaa",
108        ),
109        "aarch64-pc-windows-msvc" => (
110            "moli-aarch64-pc-windows-msvc.zip",
111            "zip",
112            "1f3031a2ad668bc0b235f543b0559be70dd7649ff6ec36f45a44eedd9a43ece7",
113        ),
114        "x86_64-pc-windows-msvc" => (
115            "moli-x86_64-pc-windows-msvc.zip",
116            "zip",
117            "ab87af41cca72531bfc08cff068a8bcf3c9f0f53f15dcdd153e1da547191c87c",
118        ),
119        _ => return None,
120    };
121    Some(ManifestAsset {
122        archive: archive.to_string(),
123        format,
124        sha256,
125    })
126}
127
128#[cfg(test)]
129pub(crate) fn _manifest_resource_is_parseable() -> Result<(), serde_json::Error> {
130    let manifest: RuntimeManifest =
131        serde_json::from_str(include_str!("../../resources/moli-runtime-manifest.json"))?;
132    debug_assert_eq!(manifest.schema, "a3s-code/moli-runtime-manifest/v1");
133    debug_assert_eq!(manifest.repository, MOLI_REPOSITORY_URL);
134    debug_assert_eq!(manifest.version, DEFAULT_MOLI_VERSION);
135    debug_assert!(!manifest.assets.is_empty());
136    for asset in manifest.assets.values() {
137        debug_assert!(!asset.archive.is_empty());
138        debug_assert!(matches!(asset.format.as_str(), "tar.gz" | "zip"));
139        debug_assert_eq!(asset.sha256.len(), 64);
140    }
141    Ok(())
142}