hyprstream_core/storage/
release_store.rs1use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::path::Path;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct ReleaseManifest {
14 pub release: ReleaseInfo,
15 pub variants: HashMap<String, VariantRequirements>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct ReleaseInfo {
21 pub version: String,
22 pub libtorch_version: String,
23 pub abi: String,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct VariantRequirements {
30 pub host_requires: String,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct VariantManifest {
37 pub variant: VariantInfo,
38 #[serde(default)]
39 pub host: Option<HostRequirements>,
40 #[serde(default)]
41 pub files: Option<FileCategories>,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct VariantInfo {
47 pub id: String,
48 #[serde(default)]
49 pub binary_sha256: Option<String>,
50 #[serde(default)]
51 pub libtorch_sha256: Option<String>,
52 #[serde(default)]
53 pub total_size_bytes: Option<u64>,
54 #[serde(default)]
55 pub runtime_size_bytes: Option<u64>,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct HostRequirements {
61 #[serde(default)]
62 pub min_driver_version: Option<String>,
63 #[serde(default)]
64 pub cuda_compat: Option<String>,
65 #[serde(default)]
66 pub rocm_version: Option<String>,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct FileCategories {
75 #[serde(default)]
77 pub runtime: Vec<String>,
78 #[serde(default)]
80 pub dev: Vec<String>,
81 #[serde(default)]
83 pub test: Vec<String>,
84}
85
86impl ReleaseManifest {
87 pub fn from_toml(s: &str) -> Result<Self, toml::de::Error> {
89 toml::from_str(s)
90 }
91
92 pub fn from_file(path: &Path) -> anyhow::Result<Self> {
94 let content = std::fs::read_to_string(path)?;
95 Ok(Self::from_toml(&content)?)
96 }
97
98 pub fn variant_ids(&self) -> Vec<&str> {
100 self.variants
101 .keys()
102 .map(std::string::String::as_str)
103 .collect()
104 }
105}
106
107impl VariantManifest {
108 pub fn from_toml(s: &str) -> Result<Self, toml::de::Error> {
110 toml::from_str(s)
111 }
112
113 pub fn from_file(path: &Path) -> anyhow::Result<Self> {
115 let content = std::fs::read_to_string(path)?;
116 Ok(Self::from_toml(&content)?)
117 }
118
119 pub fn runtime_pathspecs(&self) -> Vec<String> {
124 let variant_id = &self.variant.id;
125 let mut paths = vec![format!("backends/{variant_id}/")];
126
127 if let Some(ref files) = self.files {
129 if !files.runtime.is_empty() {
130 paths.clear();
131 for f in &files.runtime {
132 paths.push(format!("backends/{variant_id}/{f}"));
133 }
134 }
135 }
136
137 paths.push("manifest.toml".to_owned());
139 paths
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use super::*;
146
147 #[test]
148 fn test_parse_release_manifest() -> Result<(), Box<dyn std::error::Error>> {
149 let toml = r#"
150[release]
151version = "0.1.0"
152libtorch_version = "2.11.0"
153abi = "pre-cxx11"
154
155[variants]
156cpu = { host_requires = "glibc" }
157cuda130 = { host_requires = "nvidia-driver" }
158rocm71 = { host_requires = "rocm" }
159"#;
160 let manifest = ReleaseManifest::from_toml(toml)?;
161 assert_eq!(manifest.release.version, "0.1.0");
162 assert_eq!(manifest.variants.len(), 3);
163 assert!(manifest.variants.contains_key("cuda130"));
164 Ok(())
165 }
166
167 #[test]
168 fn test_parse_variant_manifest() -> Result<(), Box<dyn std::error::Error>> {
169 let toml = r#"
170[variant]
171id = "cuda130"
172total_size_bytes = 2500000000
173runtime_size_bytes = 1800000000
174
175[host]
176min_driver_version = "535.0"
177cuda_compat = "13.0"
178
179[files]
180runtime = [
181 "hyprstream",
182 "libtorch/lib/libtorch.so",
183 "libtorch/lib/libtorch_cpu.so",
184 "libtorch/lib/libtorch_cuda.so",
185 "libtorch/lib/libc10.so",
186]
187dev = ["libtorch/include/**"]
188test = ["libtorch/lib/*_test*"]
189"#;
190 let manifest = VariantManifest::from_toml(toml)?;
191 assert_eq!(manifest.variant.id, "cuda130");
192 let host = manifest.host.as_ref().ok_or("host missing")?;
193 assert_eq!(host.min_driver_version, Some("535.0".to_owned()));
194 let paths = manifest.runtime_pathspecs();
195 assert!(paths.contains(&"manifest.toml".to_owned()));
196 assert!(paths.iter().any(|p| p.contains("libtorch")));
197 Ok(())
198 }
199
200 #[test]
201 fn test_runtime_pathspecs_default() -> Result<(), Box<dyn std::error::Error>> {
202 let toml = r#"
203[variant]
204id = "cpu"
205"#;
206 let manifest = VariantManifest::from_toml(toml)?;
207 let paths = manifest.runtime_pathspecs();
208 assert_eq!(paths, vec!["backends/cpu/", "manifest.toml"]);
209 Ok(())
210 }
211}