feat: expose 3 FFI functions via UDL - detect_material_type, read_image_meta, read_text_stats

This commit is contained in:
wangdl 2026-06-02 19:45:54 +08:00
parent 917c7a4d2f
commit cfbee9ea53
7 changed files with 107 additions and 9 deletions

View File

@ -1512,6 +1512,27 @@ fileprivate struct FfiConverterOptionTypeReadingPosition: FfiConverterRustBuffer
} }
} }
} }
public func detectMaterialType(filePath: String)throws -> MaterialType {
return try FfiConverterTypeMaterialType_lift(try rustCallWithError(FfiConverterTypeDocumentError_lift) {
uniffi_zx_document_ffi_fn_func_detect_material_type(
FfiConverterString.lower(filePath),$0
)
})
}
public func readImageMeta(filePath: String)throws -> ImageMeta {
return try FfiConverterTypeImageMeta_lift(try rustCallWithError(FfiConverterTypeDocumentError_lift) {
uniffi_zx_document_ffi_fn_func_read_image_meta(
FfiConverterString.lower(filePath),$0
)
})
}
public func readTextStats(filePath: String)throws -> TextStats {
return try FfiConverterTypeTextStats_lift(try rustCallWithError(FfiConverterTypeDocumentError_lift) {
uniffi_zx_document_ffi_fn_func_read_text_stats(
FfiConverterString.lower(filePath),$0
)
})
}
private enum InitializationResult { private enum InitializationResult {
case ok case ok
@ -1528,6 +1549,15 @@ private let initializationResult: InitializationResult = {
if bindings_contract_version != scaffolding_contract_version { if bindings_contract_version != scaffolding_contract_version {
return InitializationResult.contractVersionMismatch return InitializationResult.contractVersionMismatch
} }
if (uniffi_zx_document_ffi_checksum_func_detect_material_type() != 55020) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_zx_document_ffi_checksum_func_read_image_meta() != 62824) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_zx_document_ffi_checksum_func_read_text_stats() != 43426) {
return InitializationResult.apiChecksumMismatch
}
return InitializationResult.ok return InitializationResult.ok
}() }()

View File

@ -1 +1,59 @@
uniffi::setup_scaffolding!(); uniffi::setup_scaffolding!();
use std::sync::Arc;
// Re-export types so the UDL scaffolding can find them
pub use zx_document_core::material_type::{MaterialType, PreviewMode};
pub use zx_document_core::image_meta::ImageMeta;
pub use zx_document_core::text::TextStats;
// Flat DocumentError for UDL [Error]
#[derive(Debug)]
pub enum DocumentError {
FileNotFound,
UnsupportedFormat,
ParseError,
InvalidEncoding,
IoError,
}
impl std::fmt::Display for DocumentError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FileNotFound => write!(f, "File not found"),
Self::UnsupportedFormat => write!(f, "Unsupported format"),
Self::ParseError => write!(f, "Parse error"),
Self::InvalidEncoding => write!(f, "Invalid encoding"),
Self::IoError => write!(f, "IO error"),
}
}
}
impl std::error::Error for DocumentError {}
impl From<zx_document_core::error::DocumentError> for DocumentError {
fn from(e: zx_document_core::error::DocumentError) -> Self {
match e {
zx_document_core::error::DocumentError::FileNotFound(_) => Self::FileNotFound,
zx_document_core::error::DocumentError::UnsupportedFormat(_) => Self::UnsupportedFormat,
zx_document_core::error::DocumentError::ParseError(_) => Self::ParseError,
zx_document_core::error::DocumentError::InvalidEncoding => Self::InvalidEncoding,
zx_document_core::error::DocumentError::IoError(_) => Self::IoError,
}
}
}
// FFI functions matching UDL namespace declarations
fn detect_material_type(file_path: String) -> Result<MaterialType, DocumentError> {
zx_document_core::material_type::detect_material_type(&file_path).map_err(Into::into)
}
fn read_image_meta(file_path: String) -> Result<Arc<ImageMeta>, DocumentError> {
let meta = zx_document_core::image_meta::read_image_meta(&file_path)?;
Ok(Arc::new(meta))
}
fn read_text_stats(file_path: String) -> Result<Arc<TextStats>, DocumentError> {
let content = std::fs::read_to_string(&file_path).map_err(|_| DocumentError::FileNotFound)?;
Ok(Arc::new(zx_document_core::text::text_stats(&content)))
}

View File

@ -1,4 +1,22 @@
namespace zx_document {}; namespace zx_document {
[Throws=DocumentError]
MaterialType detect_material_type([ByRef] string file_path);
[Throws=DocumentError]
ImageMeta read_image_meta([ByRef] string file_path);
[Throws=DocumentError]
TextStats read_text_stats([ByRef] string file_path);
};
[Error]
enum DocumentError {
"FileNotFound",
"UnsupportedFormat",
"ParseError",
"InvalidEncoding",
"IoError",
};
[Enum] [Enum]
interface MaterialType { interface MaterialType {
@ -82,11 +100,3 @@ dictionary TextStats {
u32 word_count; u32 word_count;
}; };
[Error]
enum DocumentError {
"FileNotFound",
"UnsupportedFormat",
"ParseError",
"InvalidEncoding",
"IoError",
};