Skip to main content

hyperlane_cli/template/
impl.rs

1use super::*;
2
3impl FromStr for TemplateType {
4    type Err = TemplateError;
5
6    /// Parses a lowercase template type name.
7    ///
8    /// # Arguments
9    ///
10    /// - `&str` - The template type name to parse.
11    ///
12    /// # Returns
13    ///
14    /// - `Result<Self, Self::Err>` - The parsed template type or an error.
15    fn from_str(s: &str) -> Result<Self, Self::Err> {
16        match s.to_lowercase().as_str() {
17            TEMPLATE_TYPE_CONTROLLER => Ok(Self::Controller),
18            TEMPLATE_TYPE_DOMAIN => Ok(Self::Domain),
19            TEMPLATE_TYPE_EXCEPTION => Ok(Self::Exception),
20            TEMPLATE_TYPE_MAPPER => Ok(Self::Mapper),
21            TEMPLATE_TYPE_MODEL => Ok(Self::Model),
22            TEMPLATE_TYPE_REPOSITORY => Ok(Self::Repository),
23            TEMPLATE_TYPE_SERVICE => Ok(Self::Service),
24            TEMPLATE_TYPE_UTILS => Ok(Self::Utils),
25            TEMPLATE_TYPE_VIEW => Ok(Self::View),
26            _ => Err(TemplateError::InvalidTemplateType(s.to_string())),
27        }
28    }
29}
30
31impl TemplateConfig {
32    /// Create a new template configuration
33    ///
34    /// # Arguments
35    ///
36    /// - `TemplateType` - Type of template component
37    /// - `String` - Name of the component
38    /// - `Option<ModelSubType>` - Optional model subtype for model components
39    ///
40    /// # Returns
41    ///
42    /// - `Self` - Configuration instance
43    pub fn new(
44        template_type: TemplateType,
45        component_name: String,
46        model_sub_type: Option<ModelSubType>,
47    ) -> Self {
48        Self {
49            template_type,
50            component_name,
51            model_sub_type,
52            base_directory: TEMPLATE_CONFIG_BASE_DIRECTORY.to_string(),
53        }
54    }
55}
56
57impl FromStr for ModelSubType {
58    type Err = TemplateError;
59
60    /// Parses a lowercase model sub type name.
61    ///
62    /// # Arguments
63    ///
64    /// - `&str` - The model sub type name to parse.
65    ///
66    /// # Returns
67    ///
68    /// - `Result<Self, Self::Err>` - The parsed model sub type or an error.
69    fn from_str(s: &str) -> Result<Self, Self::Err> {
70        match s.to_lowercase().as_str() {
71            MODEL_SUB_TYPE_APPLICATION => Ok(Self::Application),
72            MODEL_SUB_TYPE_REQUEST => Ok(Self::Request),
73            MODEL_SUB_TYPE_RESPONSE => Ok(Self::Response),
74            _ => Err(TemplateError::InvalidModelSubType(s.to_string())),
75        }
76    }
77}