Skip to main content

reovim_module_codec_csv/
factory.rs

1//! CSV codec factory.
2
3use reovim_driver_codec::{ContentCodec, ContentCodecFactory, ContentType};
4
5use crate::{
6    classifier::{CSV, PSV, SCSV, TSV},
7    codec::CsvCodec,
8};
9
10/// Factory for creating CSV/TSV/PSV codecs.
11///
12/// Creates [`CsvCodec`] instances with the appropriate delimiter for
13/// each content type.
14pub struct CsvCodecFactory;
15
16impl CsvCodecFactory {
17    /// Create a new CSV codec factory.
18    #[must_use]
19    pub const fn new() -> Self {
20        Self
21    }
22}
23
24#[cfg_attr(coverage_nightly, coverage(off))]
25impl Default for CsvCodecFactory {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl ContentCodecFactory for CsvCodecFactory {
32    fn create(&self, content_type: &ContentType) -> Option<Box<dyn ContentCodec>> {
33        match content_type.as_str() {
34            s if s == CSV => Some(Box::new(CsvCodec::new(b',', CSV))),
35            s if s == TSV => Some(Box::new(CsvCodec::new(b'\t', TSV))),
36            s if s == PSV => Some(Box::new(CsvCodec::new(b'|', PSV))),
37            s if s == SCSV => Some(Box::new(CsvCodec::new(b';', SCSV))),
38            _ => None,
39        }
40    }
41
42    fn supported_content_types(&self) -> Vec<&str> {
43        vec![CSV, TSV, PSV, SCSV]
44    }
45
46    fn name(&self) -> &'static str {
47        "csv"
48    }
49}
50
51#[cfg(test)]
52#[path = "factory_tests.rs"]
53mod tests;