Skip to main content

reovim_module_codec_csv/
lib.rs

1#![cfg_attr(coverage_nightly, allow(unused_features))]
2#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
3//! CSV/TSV content codec module for reovim.
4//!
5//! Provides delimiter-separated value file support for the content codec
6//! pipeline:
7//! - [`CsvClassifier`] detects CSV/TSV/PSV files by delimiter analysis
8//!   or file extension (priority 15)
9//! - [`CsvCodecFactory`] creates [`CsvCodec`] instances for column-aligned
10//!   tabular view with round-trip editing support
11//!
12//! This is a BIDIRECTIONAL codec — CSV files can be edited and saved.
13//!
14//! # Architecture
15//!
16//! ```text
17//! reovim-driver-codec              (trait definitions + stores)
18//!         ^
19//!         |
20//! reovim-module-codec-csv          (THIS CRATE - CSV/TSV/PSV)
21//! ```
22//!
23//! # Self-Registration Pattern
24//!
25//! During `init()`, this module registers:
26//! - [`CsvClassifier`] into [`ContentClassifierStore`] (priority 15)
27//! - [`CsvCodecFactory`] into [`ContentCodecFactoryStore`]
28
29use std::sync::Arc;
30
31use {
32    reovim_driver_codec::{ContentClassifierStore, ContentCodecFactoryStore},
33    reovim_kernel::api::v1::{Module, ModuleContext, ModuleError, ModuleId, ProbeResult, Version},
34};
35
36pub mod classifier;
37pub mod codec;
38pub mod factory;
39
40pub use {classifier::CsvClassifier, codec::CsvCodec, factory::CsvCodecFactory};
41
42/// CSV/TSV content codec module.
43///
44/// Registers CSV classifier (priority 15) and CSV codec factory during init.
45pub struct CodecCsvModule;
46
47impl CodecCsvModule {
48    /// Create a new CSV codec module.
49    #[must_use]
50    pub const fn new() -> Self {
51        Self
52    }
53}
54
55#[cfg_attr(coverage_nightly, coverage(off))]
56impl Default for CodecCsvModule {
57    fn default() -> Self {
58        Self::new()
59    }
60}
61
62impl Module for CodecCsvModule {
63    fn id(&self) -> ModuleId {
64        ModuleId::new("codec-csv")
65    }
66
67    fn name(&self) -> &'static str {
68        "Codec CSV"
69    }
70
71    fn version(&self) -> Version {
72        Version::new(0, 1, 0)
73    }
74
75    #[cfg_attr(coverage_nightly, coverage(off))]
76    fn init(&mut self, ctx: &ModuleContext) -> ProbeResult {
77        // Register codec factory
78        let factory_store = ctx.services.get_or_create::<ContentCodecFactoryStore>();
79        factory_store.add_factory(Arc::new(CsvCodecFactory::new()));
80
81        // Register classifier
82        let classifier_store = ctx.services.get_or_create::<ContentClassifierStore>();
83        classifier_store.add(Arc::new(CsvClassifier::new()));
84
85        tracing::info!("CodecCsvModule: registered CSV codec and classifier");
86        ProbeResult::Success
87    }
88
89    fn exit(&mut self) -> Result<(), ModuleError> {
90        tracing::info!("CodecCsvModule: exiting");
91        Ok(())
92    }
93
94    fn provides(&self) -> &[&'static str] {
95        &[reovim_capabilities::CODEC_PROVIDER]
96    }
97}
98
99// Generate FFI entry points for dynamic loading
100#[cfg(feature = "dynamic")]
101reovim_module_macros::declare_module!(CodecCsvModule);
102
103#[cfg(test)]
104#[path = "lib_tests.rs"]
105mod tests;