gglib_core/ports/
download.rs1use async_trait::async_trait;
7use serde::{Deserialize, Serialize};
8
9use crate::download::{DownloadError, Quantization};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct Resolution {
18 pub quantization: Quantization,
20 pub files: Vec<ResolvedFile>,
22 pub is_sharded: bool,
24}
25
26impl Resolution {
27 pub fn filenames(&self) -> Vec<String> {
29 self.files.iter().map(|f| f.path.clone()).collect()
30 }
31
32 pub fn total_size(&self) -> Option<u64> {
34 let sizes: Option<Vec<u64>> = self.files.iter().map(|f| f.size).collect();
35 sizes.map(|s| s.iter().sum())
36 }
37
38 pub fn first_file(&self) -> Option<&str> {
40 self.files.first().map(|f| f.path.as_str())
41 }
42
43 pub const fn file_count(&self) -> usize {
45 self.files.len()
46 }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct ResolvedFile {
52 pub path: String,
54 #[serde(skip_serializing_if = "Option::is_none")]
56 pub size: Option<u64>,
57}
58
59impl ResolvedFile {
60 pub fn new(path: impl Into<String>) -> Self {
62 Self {
63 path: path.into(),
64 size: None,
65 }
66 }
67
68 pub fn with_size(path: impl Into<String>, size: u64) -> Self {
70 Self {
71 path: path.into(),
72 size: Some(size),
73 }
74 }
75}
76
77#[async_trait]
94pub trait QuantizationResolver: Send + Sync {
95 async fn resolve(
100 &self,
101 repo_id: &str,
102 quantization: Quantization,
103 ) -> Result<Resolution, DownloadError>;
104
105 async fn list_available(&self, repo_id: &str) -> Result<Vec<Quantization>, DownloadError>;
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn test_resolution_methods() {
115 let resolution = Resolution {
116 quantization: Quantization::Q4KM,
117 files: vec![
118 ResolvedFile::with_size("model.gguf", 1000),
119 ResolvedFile::with_size("model-00001-of-00002.gguf", 500),
120 ],
121 is_sharded: true,
122 };
123
124 assert_eq!(resolution.file_count(), 2);
125 assert_eq!(resolution.total_size(), Some(1500));
126 assert_eq!(resolution.first_file(), Some("model.gguf"));
127 assert_eq!(
128 resolution.filenames(),
129 vec!["model.gguf", "model-00001-of-00002.gguf"]
130 );
131 }
132
133 #[test]
134 fn test_resolved_file_creation() {
135 let file = ResolvedFile::new("test.gguf");
136 assert_eq!(file.path, "test.gguf");
137 assert_eq!(file.size, None);
138
139 let file_with_size = ResolvedFile::with_size("test.gguf", 1024);
140 assert_eq!(file_with_size.size, Some(1024));
141 }
142}