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