vector_config/
metadata.rs1use std::fmt;
2
3use vector_config_common::{attributes::CustomAttribute, validation};
4
5use crate::ToValue;
6
7#[derive(Default)]
9pub struct Metadata {
10 title: Option<&'static str>,
11 description: Option<&'static str>,
12 default_value: Option<Box<dyn ToValue>>,
13 custom_attributes: Vec<CustomAttribute>,
14 deprecated: bool,
15 deprecated_message: Option<&'static str>,
16 transparent: bool,
17 validations: Vec<validation::Validation>,
18}
19
20impl Metadata {
21 pub fn with_title(title: &'static str) -> Self {
22 Self {
23 title: Some(title),
24 ..Default::default()
25 }
26 }
27
28 pub fn title(&self) -> Option<&'static str> {
29 self.title
30 }
31
32 pub fn set_title(&mut self, title: &'static str) {
33 self.title = Some(title);
34 }
35
36 pub fn clear_title(&mut self) {
37 self.title = None;
38 }
39
40 pub fn with_description(desc: &'static str) -> Self {
41 Self {
42 description: Some(desc),
43 ..Default::default()
44 }
45 }
46
47 pub fn description(&self) -> Option<&'static str> {
48 self.description
49 }
50
51 pub fn set_description(&mut self, desc: &'static str) {
52 self.description = Some(desc);
53 }
54
55 pub fn clear_description(&mut self) {
56 self.description = None;
57 }
58
59 pub fn default_value(&self) -> Option<&dyn ToValue> {
60 self.default_value.as_deref()
61 }
62
63 pub fn set_default_value(&mut self, default_value: impl ToValue + 'static) {
64 self.default_value = Some(Box::new(default_value));
65 }
66
67 pub fn deprecated(&self) -> bool {
68 self.deprecated
69 }
70
71 pub fn set_deprecated(&mut self) {
72 self.deprecated = true;
73 }
74
75 pub fn deprecated_message(&self) -> Option<&'static str> {
76 self.deprecated_message
77 }
78
79 pub fn set_deprecated_message(&mut self, message: &'static str) {
80 self.deprecated_message = Some(message);
81 }
82
83 pub fn with_transparent(transparent: bool) -> Self {
84 Self {
85 transparent,
86 ..Default::default()
87 }
88 }
89
90 pub fn transparent(&self) -> bool {
91 self.transparent
92 }
93
94 pub fn set_transparent(&mut self) {
95 self.transparent = true;
96 }
97
98 pub fn custom_attributes(&self) -> &[CustomAttribute] {
99 &self.custom_attributes
100 }
101
102 pub fn add_custom_attribute(&mut self, attribute: CustomAttribute) {
103 self.custom_attributes.push(attribute);
104 }
105
106 pub fn validations(&self) -> &[validation::Validation] {
107 &self.validations
108 }
109
110 pub fn add_validation(&mut self, validation: validation::Validation) {
111 self.validations.push(validation);
112 }
113
114 pub fn merge(mut self, other: Metadata) -> Self {
115 self.custom_attributes.extend(other.custom_attributes);
116 self.validations.extend(other.validations);
117
118 Self {
119 title: other.title.or(self.title),
120 description: other.description.or(self.description),
121 default_value: other.default_value.or(self.default_value),
122 custom_attributes: self.custom_attributes,
123 deprecated: other.deprecated,
124 deprecated_message: other.deprecated_message.or(self.deprecated_message),
125 transparent: other.transparent,
126 validations: self.validations,
127 }
128 }
129
130 pub(crate) fn convert(&self) -> Metadata {
134 Metadata {
135 title: self.title,
136 description: self.description,
137 default_value: None,
138 custom_attributes: self.custom_attributes.clone(),
139 deprecated: self.deprecated,
140 deprecated_message: self.deprecated_message,
141 transparent: self.transparent,
142 validations: self.validations.clone(),
143 }
144 }
145}
146
147impl fmt::Debug for Metadata {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 f.debug_struct("Metadata")
150 .field("title", &self.title)
151 .field("description", &self.description)
152 .field(
153 "default_value",
154 if self.default_value.is_some() {
155 &"<some>"
156 } else {
157 &"<none>"
158 },
159 )
160 .field("custom_attributes", &self.custom_attributes)
161 .field("deprecated", &self.deprecated)
162 .field("deprecated_message", &self.deprecated_message)
163 .field("transparent", &self.transparent)
164 .field("validations", &self.validations)
165 .finish()
166 }
167}