Skip to main content

vantage_csv/
csv.rs

1use std::path::PathBuf;
2
3use indexmap::IndexMap;
4use vantage_core::error;
5use vantage_dataset::traits::Result;
6use vantage_table::column::core::Column;
7use vantage_table::traits::column_like::ColumnLike;
8use vantage_types::Record;
9
10use crate::type_system::{AnyCsvType, CsvTypeVariants, parse_with_type, variant_from_type_name};
11
12/// CSV backend for Vantage — reads data from CSV files.
13///
14/// Each table maps to a CSV file: `{base_dir}/{table_name}.csv`.
15/// CSV is a read-only data source — write operations return errors.
16#[derive(Clone, Debug)]
17pub struct Csv {
18    base_dir: PathBuf,
19    pub(crate) id_column: String,
20}
21
22impl Csv {
23    /// Create a new CSV data source reading files from `base_dir`.
24    pub fn new(base_dir: impl Into<PathBuf>) -> Self {
25        Self {
26            base_dir: base_dir.into(),
27            id_column: "id".to_string(),
28        }
29    }
30
31    /// Set which CSV column to use as the record ID.
32    pub fn with_id_column(mut self, column: impl Into<String>) -> Self {
33        self.id_column = column.into();
34        self
35    }
36
37    fn file_path(&self, table_name: &str) -> PathBuf {
38        self.base_dir.join(format!("{}.csv", table_name))
39    }
40
41    pub(crate) fn base_dir(&self) -> &std::path::Path {
42        &self.base_dir
43    }
44
45    /// Parse a CSV file and return all rows as records.
46    ///
47    /// Behavior under aliasing: a column defined with [`Column::with_alias`]
48    /// reads from the CSV header named by its alias and stores under the
49    /// column name. Headers without a corresponding column are passed
50    /// through untyped (string) under their raw header name — so a table
51    /// with zero declared columns still surfaces every field of the file.
52    pub(crate) fn read_csv(
53        &self,
54        table_name: &str,
55        columns: &IndexMap<String, Column<AnyCsvType>>,
56    ) -> Result<IndexMap<String, Record<AnyCsvType>>> {
57        let path = self.file_path(table_name);
58        let mut reader = csv::Reader::from_path(&path)
59            .map_err(|e| error!("Failed to open CSV file", path = path.display(), detail = e))?;
60
61        let headers = reader
62            .headers()
63            .map_err(|e| error!("Failed to read CSV headers", detail = e))?
64            .clone();
65
66        // For each CSV column index, decide the record key + parsing variant.
67        // Default: pass the header through untyped. If a declared column
68        // matches (by alias-or-name), use the column name + its type.
69        let mut key_for: Vec<String> = headers.iter().map(|h| h.to_string()).collect();
70        let mut variant_for: Vec<Option<CsvTypeVariants>> = vec![None; headers.len()];
71        for (name, col) in columns {
72            let csv_header = col.alias().unwrap_or(name);
73            if let Some(idx) = headers.iter().position(|h| h == csv_header) {
74                key_for[idx] = name.clone();
75                variant_for[idx] = variant_from_type_name(col.get_type());
76            }
77        }
78
79        // id column may itself carry an alias.
80        let id_header = columns
81            .get(&self.id_column)
82            .and_then(|c| c.alias())
83            .unwrap_or(&self.id_column);
84        let id_col_index = headers.iter().position(|h| h == id_header);
85
86        let mut records = IndexMap::new();
87        for (row_idx, result) in reader.records().enumerate() {
88            let csv_record = result.map_err(|e| error!("Failed to read CSV row", detail = e))?;
89
90            let id = if let Some(idx) = id_col_index {
91                csv_record.get(idx).unwrap_or_default().to_string()
92            } else {
93                row_idx.to_string()
94            };
95
96            let mut record = Record::new();
97            for (i, field) in csv_record.iter().enumerate() {
98                if let Some(key) = key_for.get(i) {
99                    let variant = variant_for.get(i).copied().flatten();
100                    record.insert(key.clone(), parse_with_type(field, variant));
101                }
102            }
103
104            records.insert(id, record);
105        }
106
107        Ok(records)
108    }
109}