dev_scope/shared/models/internal/
known_error.rs1use crate::models::prelude::{ModelMetadata, V1AlphaKnownError};
2use crate::models::HelpMetadata;
3use derivative::Derivative;
4use regex::Regex;
5
6#[derive(Derivative)]
7#[derivative(PartialEq)]
8#[derive(Debug, Clone)]
9pub struct KnownError {
10 pub full_name: String,
11 pub metadata: ModelMetadata,
12 pub pattern: String,
13 #[derivative(PartialEq = "ignore")]
14 pub regex: Regex,
15 pub help_text: String,
16}
17
18impl HelpMetadata for KnownError {
19 fn metadata(&self) -> &ModelMetadata {
20 &self.metadata
21 }
22
23 fn full_name(&self) -> String {
24 self.full_name.to_string()
25 }
26}
27
28impl TryFrom<V1AlphaKnownError> for KnownError {
29 type Error = anyhow::Error;
30
31 fn try_from(value: V1AlphaKnownError) -> Result<Self, Self::Error> {
32 let regex = Regex::new(&value.spec.pattern)?;
33 Ok(KnownError {
34 full_name: value.full_name(),
35 metadata: value.metadata,
36 pattern: value.spec.pattern,
37 regex,
38 help_text: value.spec.help,
39 })
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use crate::shared::models::parse_models_from_string;
46
47 use std::path::Path;
48
49 #[test]
50 fn test_parse_scope_known_error() {
51 let text = "apiVersion: scope.github.com/v1alpha
52kind: ScopeKnownError
53metadata:
54 name: error-exists
55spec:
56 description: Check if the word error is in the logs
57 pattern: error
58 help: The command had an error, try reading the logs around there to find out what happened.";
59
60 let path = Path::new("/foo/bar/file.yaml");
61 let configs = parse_models_from_string(path, text).unwrap();
62 assert_eq!(1, configs.len());
63 let model = configs[0].get_known_error_spec().unwrap();
64
65 assert_eq!("error-exists", model.metadata.name);
66 assert_eq!("ScopeKnownError/error-exists", model.full_name);
67 assert_eq!("The command had an error, try reading the logs around there to find out what happened.", model.help_text);
68 assert_eq!("error", model.pattern);
69 }
70}