Skip to main content

ConverterRegistry

Struct ConverterRegistry 

Source
pub struct ConverterRegistry { /* private fields */ }
Expand description

持有用户注册和内置 DocConverter 实例的注册表。

转换器以它们处理的 Rust 类型的 TypeId 为键。 注册表通常通过 builder 的 .register_converter() 调用填充, 然后传递给 from_row_with_converters / to_row_with_converters

对应 Java: com.alibaba.excel.converters.ConverterRegistry

§Examples

use easydoc_core::{ConverterRegistry, DocConverter, DocValue, TableColumn};
use easydoc_core::Result;

struct BoolToString;

impl DocConverter<bool> for BoolToString {
    fn support_type() -> std::any::TypeId { std::any::TypeId::of::<bool>() }
    fn to_doc_value(&self, value: &bool, _col: &TableColumn) -> Result<DocValue> {
        Ok(DocValue::String(if *value { "yes".into() } else { "no".into() }))
    }
    fn from_doc_value(&self, value: &DocValue, col: &TableColumn) -> Result<bool> {
        match value {
            DocValue::String(s) => Ok(s == "yes"),
            _ => Err(easydoc_core::DocError::Conversion {
                field: col.field_name.clone(),
                value: format!("{value:?}"),
                message: "expected string".into(),
            }),
        }
    }
}

let mut registry = ConverterRegistry::new();
registry.register::<bool, _>(BoolToString);
assert!(registry.contains::<bool>());

Implementations§

Source§

impl ConverterRegistry

Source

pub fn new() -> Self

创建空注册表。

Source

pub fn register<T: 'static, C: DocConverter<T> + Send + Sync + 'static>( &mut self, converter: C, ) -> bool

为类型 T 注册转换器。

如果 T 的转换器已注册,则替换。返回 true 表示新注册,false 表示替换。

对应 Java: ConverterRegistry#registerConverter

Source

pub fn register_named<T: 'static, C: DocConverter<T> + Send + Sync + 'static>( &mut self, name: &str, converter: C, ) -> bool

为类型 T 注册转换器,并以人类可读名称索引。

名称可用于 find_converter_by_name 在不知道具体 Rust 类型的情况下查找转换器。 当 #[docx(converter = StatusConverter)] 属性在 schema 中以字符串存储 转换器类型名时非常有用。

返回 true 表示新注册,false 表示替换。

Source

pub fn contains<T: 'static>(&self) -> bool

返回类型 T 是否已注册转换器。

Source

pub fn find_converter<T: 'static>(&self) -> Option<&dyn ErasedConverter>

查找类型 T 的类型擦除转换器。

未注册时返回 None。这是 derive 生成代码的主要查找机制。

Source

pub fn find_converter_by_name(&self, name: &str) -> Option<&dyn ErasedConverter>

按已注册名称查找类型擦除转换器。

未注册时返回 None。支持 #[docx(converter = StatusConverter)] 模式。

Source

pub fn to_doc_value<V: 'static + Debug>( &self, value: &V, column: &TableColumn, ) -> Result<DocValue>

使用已注册的转换器将 Rust 值转换为 DocValue

未注册自定义转换器时回退到内置转换。

§Errors

找不到合适的转换器时返回 DocError::Conversion

Source

pub fn from_doc_value<V: 'static>( &self, value: &DocValue, column: &TableColumn, ) -> Result<V>

DocValue 转换为 Rust 类型 V

§Errors

找不到合适的转换器或值无法转换时返回 DocError::Conversion

Trait Implementations§

Source§

impl Debug for ConverterRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ConverterRegistry

Source§

fn default() -> ConverterRegistry

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.