vector/config/loading/
source.rs

1use std::io::Read;
2
3use serde_toml_merge::merge_into_table;
4use toml::{map::Map, value::Table};
5
6use super::{ComponentHint, Loader, Process};
7
8pub struct SourceLoader {
9    table: Table,
10}
11
12impl SourceLoader {
13    pub fn new() -> Self {
14        Self { table: Map::new() }
15    }
16}
17
18impl Process for SourceLoader {
19    /// Prepares input by simply reading bytes to a string. Unlike other loaders, there's no
20    /// interpolation of environment variables. This is on purpose to preserve the original config.
21    fn prepare<R: Read>(&mut self, mut input: R) -> Result<String, Vec<String>> {
22        let mut source_string = String::new();
23        input
24            .read_to_string(&mut source_string)
25            .map_err(|e| vec![e.to_string()])?;
26
27        Ok(source_string)
28    }
29
30    /// Merge values by combining with the internal TOML `Table`.
31    fn merge(&mut self, table: Table, _hint: Option<ComponentHint>) -> Result<(), Vec<String>> {
32        merge_into_table(&mut self.table, table).map_err(|e| vec![e.to_string()])
33    }
34}
35
36impl Loader<Table> for SourceLoader {
37    /// Returns the resulting TOML `Table`.
38    fn take(self) -> Table {
39        self.table
40    }
41}