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
impl ConverterRegistry
Sourcepub fn register<T: 'static, C: DocConverter<T> + Send + Sync + 'static>(
&mut self,
converter: C,
) -> bool
pub fn register<T: 'static, C: DocConverter<T> + Send + Sync + 'static>( &mut self, converter: C, ) -> bool
为类型 T 注册转换器。
如果 T 的转换器已注册,则替换。返回 true 表示新注册,false 表示替换。
对应 Java: ConverterRegistry#registerConverter
Sourcepub fn register_named<T: 'static, C: DocConverter<T> + Send + Sync + 'static>(
&mut self,
name: &str,
converter: C,
) -> bool
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 表示替换。
Sourcepub fn find_converter<T: 'static>(&self) -> Option<&dyn ErasedConverter>
pub fn find_converter<T: 'static>(&self) -> Option<&dyn ErasedConverter>
查找类型 T 的类型擦除转换器。
未注册时返回 None。这是 derive 生成代码的主要查找机制。
Sourcepub fn find_converter_by_name(&self, name: &str) -> Option<&dyn ErasedConverter>
pub fn find_converter_by_name(&self, name: &str) -> Option<&dyn ErasedConverter>
按已注册名称查找类型擦除转换器。
未注册时返回 None。支持 #[docx(converter = StatusConverter)] 模式。
Sourcepub fn to_doc_value<V: 'static + Debug>(
&self,
value: &V,
column: &TableColumn,
) -> Result<DocValue>
pub fn to_doc_value<V: 'static + Debug>( &self, value: &V, column: &TableColumn, ) -> Result<DocValue>
Sourcepub fn from_doc_value<V: 'static>(
&self,
value: &DocValue,
column: &TableColumn,
) -> Result<V>
pub fn from_doc_value<V: 'static>( &self, value: &DocValue, column: &TableColumn, ) -> Result<V>
Trait Implementations§
Source§impl Debug for ConverterRegistry
impl Debug for ConverterRegistry
Source§impl Default for ConverterRegistry
impl Default for ConverterRegistry
Source§fn default() -> ConverterRegistry
fn default() -> ConverterRegistry
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for ConverterRegistry
impl !UnwindSafe for ConverterRegistry
impl Freeze for ConverterRegistry
impl Send for ConverterRegistry
impl Sync for ConverterRegistry
impl Unpin for ConverterRegistry
impl UnsafeUnpin for ConverterRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more