Files
AFFiNE-Mirror/packages/backend/native/src/doc_loader.rs
T
Brooooooklyn d02aa8c7e0 fix(native): opt out napi-derive noop feature (#12686)
It would cause the napi-derive not work as expect in workspace level

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **Refactor**
  - Improved internal handling and type definitions for document parsing, resulting in clearer and more maintainable data structures.
- **Chores**
  - Introduced a new feature flag for mobile native builds, enabling conditional compilation for enhanced flexibility across Android and iOS.
  - Updated build scripts to support the new feature flag for both Android and iOS platforms.
  - Updated iOS app dependencies to newer versions, including Apollo iOS, ChidoriMenu, and swift-collections, and removed SQLite.swift.
- **Tests**
  - Enhanced Rust linting and testing workflows to run selectively across workspace packages with the new feature flag enabled.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-06-03 06:46:55 +00:00

76 lines
1.4 KiB
Rust

use affine_common::doc_loader::Doc;
use napi::{
anyhow::anyhow,
bindgen_prelude::{AsyncTask, Buffer},
Env, Result, Task,
};
#[napi(object)]
pub struct Chunk {
pub index: i64,
pub content: String,
}
#[napi(object)]
pub struct ParsedDoc {
pub name: String,
pub chunks: Vec<Chunk>,
}
pub struct Document {
inner: Doc,
}
impl Document {
fn name(&self) -> String {
self.inner.name.clone()
}
fn chunks(&self) -> Vec<Chunk> {
self
.inner
.chunks
.iter()
.enumerate()
.map(|(i, chunk)| {
let content = crate::utils::clean_content(&chunk.content);
Chunk {
index: i as i64,
content,
}
})
.collect::<Vec<Chunk>>()
}
}
pub struct AsyncParseDocResponse {
file_path: String,
doc: Vec<u8>,
}
#[napi]
impl Task for AsyncParseDocResponse {
type Output = Document;
type JsValue = ParsedDoc;
fn compute(&mut self) -> Result<Self::Output> {
let doc = Doc::new(&self.file_path, &self.doc).map_err(|e| anyhow!(e))?;
Ok(Document { inner: doc })
}
fn resolve(&mut self, _: Env, doc: Document) -> Result<Self::JsValue> {
Ok(ParsedDoc {
name: doc.name(),
chunks: doc.chunks(),
})
}
}
#[napi]
pub fn parse_doc(file_path: String, doc: Buffer) -> AsyncTask<AsyncParseDocResponse> {
AsyncTask::new(AsyncParseDocResponse {
file_path,
doc: doc.to_vec(),
})
}