vector_config_common/attributes.rs
1use std::fmt;
2
3use serde::Serialize;
4
5/// A custom attribute on a container, variant, or field.
6///
7/// Applied by using the `#[configurable(metadata(...))]` helper. Two forms are supported:
8///
9/// - as a flag (`#[configurable(metadata(some_flag))]`)
10/// - as a key/value pair (`#[configurable(metadata(status = "beta"))]`)
11///
12/// Custom attributes are added to the relevant schema definition as a custom field, `_metadata`, and stored as an
13/// object. For key/value pairs, they are added as-is to the object. For flags, the flag name is the property name, and
14/// the value will always be `true`.
15#[derive(Clone, Debug)]
16pub enum CustomAttribute {
17 /// A standalone flag.
18 ///
19 /// Common for marking items as supporting a particular feature i.e. marking fields that can use the event template syntax.
20 Flag(String),
21
22 /// A key/value pair.
23 ///
24 /// Used for most metadata, where a given key could have many different possible values i.e. the status of a
25 /// component (alpha, beta, stable, deprecated, etc).
26 KeyValue {
27 key: String,
28 value: serde_json::Value,
29 },
30}
31
32impl CustomAttribute {
33 pub fn flag<N>(name: N) -> Self
34 where
35 N: fmt::Display,
36 {
37 Self::Flag(name.to_string())
38 }
39
40 pub fn kv<K, V>(key: K, value: V) -> Self
41 where
42 K: fmt::Display,
43 V: Serialize,
44 {
45 Self::KeyValue {
46 key: key.to_string(),
47 value: serde_json::to_value(value).expect("should not fail to serialize value to JSON"),
48 }
49 }
50
51 pub const fn is_flag(&self) -> bool {
52 matches!(self, Self::Flag(_))
53 }
54
55 pub const fn is_kv(&self) -> bool {
56 matches!(self, Self::KeyValue { .. })
57 }
58}