vector_config_common/
constants.rs

1use serde_json::Value;
2use syn::Path;
3
4pub const COMPONENT_TYPE_API: &str = "api";
5pub const COMPONENT_TYPE_ENRICHMENT_TABLE: &str = "enrichment_table";
6pub const COMPONENT_TYPE_PROVIDER: &str = "provider";
7pub const COMPONENT_TYPE_SECRETS: &str = "secrets";
8pub const COMPONENT_TYPE_SINK: &str = "sink";
9pub const COMPONENT_TYPE_SOURCE: &str = "source";
10pub const COMPONENT_TYPE_TRANSFORM: &str = "transform";
11pub const COMPONENT_TYPE_GLOBAL_OPTION: &str = "global_option";
12pub const DOCS_META_ADDITIONAL_PROPS_DESC: &str = "docs::additional_props_description";
13pub const DOCS_META_ADVANCED: &str = "docs::advanced";
14pub const DOCS_META_COMPONENT_BASE_TYPE: &str = "docs::component_base_type";
15pub const DOCS_META_COMPONENT_NAME: &str = "docs::component_name";
16pub const DOCS_META_COMPONENT_TYPE: &str = "docs::component_type";
17pub const DOCS_META_ENUM_CONTENT_FIELD: &str = "docs::enum_content_field";
18pub const DOCS_META_ENUM_TAG_DESCRIPTION: &str = "docs::enum_tag_description";
19pub const DOCS_META_ENUM_TAG_FIELD: &str = "docs::enum_tag_field";
20pub const DOCS_META_ENUM_TAGGING: &str = "docs::enum_tagging";
21pub const DOCS_META_EXAMPLES: &str = "docs::examples";
22pub const DOCS_META_HIDDEN: &str = "docs::hidden";
23pub const DOCS_META_HUMAN_NAME: &str = "docs::human_name";
24pub const DOCS_META_NUMERIC_TYPE: &str = "docs::numeric_type";
25pub const DOCS_META_OPTIONAL: &str = "docs::optional";
26pub const DOCS_META_COMMON: &str = "docs::common";
27pub const DOCS_META_REQUIRED: &str = "docs::required";
28pub const DOCS_META_SYNTAX_OVERRIDE: &str = "docs::syntax_override";
29pub const DOCS_META_TEMPLATEABLE: &str = "docs::templateable";
30pub const DOCS_META_TYPE_OVERRIDE: &str = "docs::type_override";
31pub const DOCS_META_TYPE_UNIT: &str = "docs::type_unit";
32pub const LOGICAL_NAME: &str = "logical_name";
33pub const METADATA: &str = "_metadata";
34
35/// Well-known component types.
36#[derive(Clone, Copy, Debug, Eq, PartialEq)]
37pub enum ComponentType {
38    Api,
39    EnrichmentTable,
40    GlobalOption,
41    Provider,
42    Secrets,
43    Sink,
44    Source,
45    Transform,
46}
47
48impl ComponentType {
49    /// Gets the type of this component as a string.
50    pub const fn as_str(&self) -> &'static str {
51        match self {
52            ComponentType::Api => COMPONENT_TYPE_API,
53            ComponentType::EnrichmentTable => COMPONENT_TYPE_ENRICHMENT_TABLE,
54            ComponentType::GlobalOption => COMPONENT_TYPE_GLOBAL_OPTION,
55            ComponentType::Provider => COMPONENT_TYPE_PROVIDER,
56            ComponentType::Secrets => COMPONENT_TYPE_SECRETS,
57            ComponentType::Sink => COMPONENT_TYPE_SINK,
58            ComponentType::Source => COMPONENT_TYPE_SOURCE,
59            ComponentType::Transform => COMPONENT_TYPE_TRANSFORM,
60        }
61    }
62
63    pub fn is_valid_type(path: &Path) -> bool {
64        ComponentType::try_from(path).is_ok()
65    }
66}
67
68impl<'a> TryFrom<&'a str> for ComponentType {
69    type Error = ();
70
71    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
72        match value {
73            COMPONENT_TYPE_API => Ok(ComponentType::Api),
74            COMPONENT_TYPE_ENRICHMENT_TABLE => Ok(ComponentType::EnrichmentTable),
75            COMPONENT_TYPE_GLOBAL_OPTION => Ok(ComponentType::GlobalOption),
76            COMPONENT_TYPE_PROVIDER => Ok(ComponentType::Provider),
77            COMPONENT_TYPE_SECRETS => Ok(ComponentType::Secrets),
78            COMPONENT_TYPE_SINK => Ok(ComponentType::Sink),
79            COMPONENT_TYPE_SOURCE => Ok(ComponentType::Source),
80            COMPONENT_TYPE_TRANSFORM => Ok(ComponentType::Transform),
81            _ => Err(()),
82        }
83    }
84}
85
86impl<'a> TryFrom<&'a Path> for ComponentType {
87    type Error = ();
88
89    fn try_from(path: &'a Path) -> Result<Self, Self::Error> {
90        path.get_ident()
91            .ok_or(())
92            .map(|id| id.to_string())
93            .and_then(|s| Self::try_from(s.as_str()))
94    }
95}
96
97impl From<&ComponentType> for Value {
98    fn from(value: &ComponentType) -> Self {
99        Value::String(value.as_str().to_string())
100    }
101}