hyprstream_core/archetypes/
domain.rs1use super::capabilities::CapabilitySet;
7use std::fmt;
8use std::path::{Path, PathBuf};
9
10#[derive(Debug, Clone)]
12pub enum DetectionError {
13 Io(String),
15 PathNotFound(PathBuf),
17 InvalidStructure(String),
19}
20
21impl fmt::Display for DetectionError {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 match self {
24 DetectionError::Io(msg) => write!(f, "I/O error: {msg}"),
25 DetectionError::PathNotFound(path) => write!(f, "Path not found: {}", path.display()),
26 DetectionError::InvalidStructure(msg) => write!(f, "Invalid structure: {msg}"),
27 }
28 }
29}
30
31impl std::error::Error for DetectionError {}
32
33impl From<std::io::Error> for DetectionError {
34 fn from(e: std::io::Error) -> Self {
35 DetectionError::Io(e.to_string())
36 }
37}
38
39#[derive(Debug, Clone)]
44pub enum DetectionResult {
45 Detected(CapabilitySet),
47 NotDetected,
49 Error(DetectionError),
51}
52
53impl DetectionResult {
54 pub fn is_detected(&self) -> bool {
56 matches!(self, DetectionResult::Detected(_))
57 }
58
59 pub fn is_error(&self) -> bool {
61 matches!(self, DetectionResult::Error(_))
62 }
63
64 pub fn capabilities(&self) -> Option<&CapabilitySet> {
66 match self {
67 DetectionResult::Detected(caps) => Some(caps),
68 _ => None,
69 }
70 }
71
72 pub fn into_option(self) -> Option<CapabilitySet> {
74 match self {
75 DetectionResult::Detected(caps) => Some(caps),
76 _ => None,
77 }
78 }
79}
80
81pub trait Domain: Send + Sync + 'static {
117 fn name(&self) -> &'static str;
121
122 fn description(&self) -> &'static str;
124
125 fn detect(&self, repo_path: &Path) -> DetectionResult;
132
133 fn base_capabilities(&self) -> CapabilitySet {
137 CapabilitySet::new()
138 }
139}
140
141pub trait SimpleDomain: Send + Sync + 'static {
147 fn name(&self) -> &'static str;
149
150 fn description(&self) -> &'static str;
152
153 fn matches(&self, repo_path: &Path) -> bool;
155
156 fn base_capabilities(&self) -> CapabilitySet {
158 CapabilitySet::new()
159 }
160}
161
162impl<T: SimpleDomain> Domain for T {
164 fn name(&self) -> &'static str {
165 SimpleDomain::name(self)
166 }
167
168 fn description(&self) -> &'static str {
169 SimpleDomain::description(self)
170 }
171
172 fn detect(&self, repo_path: &Path) -> DetectionResult {
173 if self.matches(repo_path) {
174 DetectionResult::Detected(SimpleDomain::base_capabilities(self))
175 } else {
176 DetectionResult::NotDetected
177 }
178 }
179
180 fn base_capabilities(&self) -> CapabilitySet {
181 SimpleDomain::base_capabilities(self)
182 }
183}
184
185#[cfg(test)]
186#[allow(clippy::expect_used, clippy::unwrap_used, clippy::print_stdout)]
187mod tests {
188 use super::*;
189 use crate::archetypes::capabilities::{Infer, Serve, Train};
190
191 #[test]
192 fn test_detection_result_is_detected() {
193 let detected = DetectionResult::Detected(CapabilitySet::model());
194 assert!(detected.is_detected());
195
196 let not_detected = DetectionResult::NotDetected;
197 assert!(!not_detected.is_detected());
198
199 let error = DetectionResult::Error(DetectionError::Io("test".into()));
200 assert!(!error.is_detected());
201 }
202
203 #[test]
204 fn test_detection_result_capabilities() {
205 let caps = CapabilitySet::model();
206 let detected = DetectionResult::Detected(caps.clone());
207
208 let result_caps = detected.capabilities().expect("test: get capabilities");
209 assert!(result_caps.has::<Infer>());
210 assert!(result_caps.has::<Train>());
211 assert!(result_caps.has::<Serve>());
212 }
213
214 #[test]
215 fn test_detection_result_into_option() {
216 let caps = CapabilitySet::model();
217 let detected = DetectionResult::Detected(caps);
218 assert!(detected.into_option().is_some());
219
220 let not_detected = DetectionResult::NotDetected;
221 assert!(not_detected.into_option().is_none());
222
223 let error = DetectionResult::Error(DetectionError::Io("test".into()));
224 assert!(error.into_option().is_none());
225 }
226
227 #[test]
228 fn test_detection_error_display() {
229 let io_err = DetectionError::Io("permission denied".into());
230 assert!(io_err.to_string().contains("permission denied"));
231
232 let path_err = DetectionError::PathNotFound(PathBuf::from("/missing"));
233 assert!(path_err.to_string().contains("/missing"));
234
235 let struct_err = DetectionError::InvalidStructure("missing field".into());
236 assert!(struct_err.to_string().contains("missing field"));
237 }
238}