Skip to main content

reovim_module_codec_utf8/
lib.rs

1#![cfg_attr(coverage_nightly, allow(unused_features))]
2#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
3//! UTF-8 content codec module for reovim.
4//!
5//! Provides UTF-8 encoding support for the content codec pipeline:
6//! - BOM detection and stripping on decode, restoration on encode
7//! - CRLF normalization to LF on decode, restoration on encode
8//! - Round-trip fidelity: `encode(decode(bytes)) == bytes`
9//!
10//! # Architecture
11//!
12//! ```text
13//! reovim-driver-codec              (trait definitions + stores)
14//!         ^
15//!         |
16//! reovim-module-codec-utf8         (THIS CRATE - UTF-8 implementation)
17//! ```
18//!
19//! # Self-Registration Pattern
20//!
21//! During `init()`, this module registers:
22//! - [`Utf8CodecFactory`] into [`ContentCodecFactoryStore`]
23//! - [`Utf8Classifier`] into [`ContentClassifierStore`]
24
25use std::sync::Arc;
26
27use {
28    reovim_driver_codec::{ContentClassifierStore, ContentCodecFactoryStore},
29    reovim_kernel::api::v1::{Module, ModuleContext, ModuleError, ModuleId, ProbeResult, Version},
30};
31
32pub mod classifier;
33pub mod codec;
34pub mod factory;
35
36pub use {classifier::Utf8Classifier, codec::Utf8Codec, factory::Utf8CodecFactory};
37
38/// UTF-8 content codec module.
39///
40/// Registers UTF-8 classifier (priority 10) and factory during init.
41pub struct CodecUtf8Module;
42
43impl CodecUtf8Module {
44    /// Create a new UTF-8 codec module.
45    #[must_use]
46    pub const fn new() -> Self {
47        Self
48    }
49}
50
51#[cfg_attr(coverage_nightly, coverage(off))]
52impl Default for CodecUtf8Module {
53    fn default() -> Self {
54        Self::new()
55    }
56}
57
58impl Module for CodecUtf8Module {
59    fn id(&self) -> ModuleId {
60        ModuleId::new("codec-utf8")
61    }
62
63    fn name(&self) -> &'static str {
64        "Codec UTF-8"
65    }
66
67    fn version(&self) -> Version {
68        Version::new(0, 1, 0)
69    }
70
71    #[cfg_attr(coverage_nightly, coverage(off))]
72    fn init(&mut self, ctx: &ModuleContext) -> ProbeResult {
73        // Register codec factory
74        let factory_store = ctx.services.get_or_create::<ContentCodecFactoryStore>();
75        factory_store.add_factory(Arc::new(Utf8CodecFactory::new()));
76
77        // Register classifier
78        let classifier_store = ctx.services.get_or_create::<ContentClassifierStore>();
79        classifier_store.add(Arc::new(Utf8Classifier::new()));
80
81        tracing::info!("CodecUtf8Module: registered UTF-8 codec and classifier");
82        ProbeResult::Success
83    }
84
85    fn exit(&mut self) -> Result<(), ModuleError> {
86        tracing::info!("CodecUtf8Module: exiting");
87        Ok(())
88    }
89
90    fn provides(&self) -> &[&'static str] {
91        &[reovim_capabilities::CODEC_PROVIDER]
92    }
93}
94
95// Generate FFI entry points for dynamic loading
96#[cfg(feature = "dynamic")]
97reovim_module_macros::declare_module!(CodecUtf8Module);
98
99#[cfg(test)]
100#[path = "lib_tests.rs"]
101mod tests;