Skip to main content

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