Skip to main content

reovim_module_codec_utf8/
factory.rs

1//! UTF-8 codec factory.
2
3use reovim_driver_codec::{ContentCodec, ContentCodecFactory, ContentType};
4
5use crate::codec::Utf8Codec;
6
7/// Factory for creating UTF-8 codecs.
8///
9/// Creates [`Utf8Codec`] instances for `ContentType("text/utf-8")`.
10pub struct Utf8CodecFactory;
11
12impl Utf8CodecFactory {
13    /// Create a new UTF-8 codec factory.
14    #[must_use]
15    pub const fn new() -> Self {
16        Self
17    }
18}
19
20#[cfg_attr(coverage_nightly, coverage(off))]
21impl Default for Utf8CodecFactory {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl ContentCodecFactory for Utf8CodecFactory {
28    fn create(&self, content_type: &ContentType) -> Option<Box<dyn ContentCodec>> {
29        if content_type.as_str() == ContentType::UTF8 {
30            Some(Box::new(Utf8Codec::new()))
31        } else {
32            None
33        }
34    }
35
36    fn supported_content_types(&self) -> Vec<&str> {
37        vec![ContentType::UTF8]
38    }
39
40    fn name(&self) -> &'static str {
41        "utf-8"
42    }
43}
44
45#[cfg(test)]
46#[path = "factory_tests.rs"]
47mod tests;