1#![allow(missing_docs)]
2use indexmap::map::IndexMap;
3use serde::{Deserialize, Serialize};
4use vector_lib::codecs::{
5 decoding::{DeserializerConfig, FramingConfig},
6 BytesDecoderConfig, BytesDeserializerConfig,
7};
8use vector_lib::configurable::configurable_component;
9pub use vector_lib::serde::{bool_or_struct, is_default};
10
11pub const fn default_true() -> bool {
12 true
13}
14
15pub const fn default_false() -> bool {
16 false
17}
18
19pub fn default_max_length() -> usize {
23 bytesize::kib(100u64) as usize
24}
25
26pub fn default_framing_message_based() -> FramingConfig {
27 BytesDecoderConfig::new().into()
28}
29
30pub fn default_decoding() -> DeserializerConfig {
31 BytesDeserializerConfig::new().into()
32}
33
34pub mod json {
36 use bytes::{BufMut, BytesMut};
37 use serde::Serialize;
38
39 pub fn to_string(value: impl Serialize) -> String {
46 let value = serde_json::to_value(value).unwrap();
47 value.as_str().unwrap().into()
48 }
49
50 pub fn to_bytes<T>(value: &T) -> serde_json::Result<BytesMut>
57 where
58 T: ?Sized + Serialize,
59 {
60 let mut bytes = BytesMut::with_capacity(128);
63 serde_json::to_writer((&mut bytes).writer(), value)?;
64 Ok(bytes)
65 }
66}
67
68#[derive(Clone, Debug, Deserialize, Serialize)]
70#[serde(untagged)]
71pub enum FieldsOrValue<V> {
72 Fields(Fields<V>),
74
75 Value(V),
77}
78
79#[derive(Clone, Debug, Deserialize, Serialize)]
81pub struct Fields<V>(IndexMap<String, FieldsOrValue<V>>);
82
83impl<V: 'static> Fields<V> {
84 pub fn all_fields(self) -> impl Iterator<Item = (String, V)> {
85 self.0
86 .into_iter()
87 .flat_map(|(k, v)| -> Box<dyn Iterator<Item = (String, V)>> {
88 match v {
89 FieldsOrValue::Value(v) => Box::new(std::iter::once((k, v))),
91 FieldsOrValue::Fields(f) => Box::new(
92 f.all_fields()
93 .map(move |(nested_k, v)| (format!("{k}.{nested_k}"), v)),
94 ),
95 }
96 })
97 }
98}
99
100#[configurable_component]
102#[derive(Clone, Debug, Eq, Hash, PartialEq)]
103#[serde(untagged)]
104pub enum OneOrMany<T: 'static> {
105 One(T),
106 Many(Vec<T>),
107}
108
109impl<T> OneOrMany<T> {
110 pub fn to_vec(self) -> Vec<T> {
111 match self {
112 Self::One(value) => vec![value],
113 Self::Many(list) => list,
114 }
115 }
116}
117
118impl<T> From<T> for OneOrMany<T> {
119 fn from(value: T) -> Self {
120 Self::One(value)
121 }
122}
123
124impl<T> From<Vec<T>> for OneOrMany<T> {
125 fn from(value: Vec<T>) -> Self {
126 Self::Many(value)
127 }
128}