Skip to main content

mint_cli/data/
mod.rs

1pub mod args;
2pub mod error;
3mod excel;
4mod helpers;
5mod json;
6
7use crate::layout::value::{DataValue, ValueSource};
8use error::DataError;
9use excel::ExcelDataSource;
10use json::JsonDataSource;
11
12/// Trait for data sources that provide values by name.
13pub trait DataSource: Sync {
14    /// Retrieves a single numeric or boolean value.
15    fn retrieve_single_value(&self, name: &str) -> Result<DataValue, DataError>;
16
17    /// Retrieves a 1D array (from sheet reference) or a literal string.
18    fn retrieve_1d_array_or_string(&self, name: &str) -> Result<ValueSource, DataError>;
19
20    /// Retrieves a 2D array from a sheet reference.
21    fn retrieve_2d_array(&self, name: &str) -> Result<Vec<Vec<DataValue>>, DataError>;
22}
23
24/// Creates a data source from CLI arguments.
25///
26/// Returns `None` if no data source is configured (e.g., no `--xlsx` provided).
27pub fn create_data_source(args: &args::DataArgs) -> Result<Option<Box<dyn DataSource>>, DataError> {
28    match (&args.xlsx, &args.postgres, &args.http, &args.json) {
29        (Some(_), _, _, _) => Ok(Some(Box::new(ExcelDataSource::new(args)?))),
30        (_, Some(_), _, _) => Ok(Some(Box::new(JsonDataSource::from_postgres(args)?))),
31        (_, _, Some(_), _) => Ok(Some(Box::new(JsonDataSource::from_http(args)?))),
32        (_, _, _, Some(_)) => Ok(Some(Box::new(JsonDataSource::from_json(args)?))),
33        _ => Ok(None),
34    }
35}