pub trait Filter: Send + Sync {
// Required methods
fn id(&self) -> &'static str;
fn extensions(&self) -> &'static [&'static str];
fn sniff(&self, bytes: &[u8]) -> bool;
fn extract(
&self,
bytes: &[u8],
opts: &ExtractOptions,
) -> Result<Document, Error>;
fn merge_with(
&self,
doc: &Document,
opts: &MergeOptions,
) -> Result<Vec<u8>, Error>;
// Provided methods
fn bilingual(&self) -> bool { ... }
fn format_option_keys(&self) -> &'static [FormatOptionKey] { ... }
fn merge(&self, doc: &Document) -> Result<Vec<u8>, Error> { ... }
fn preview(&self, _doc: &Document) -> Option<String> { ... }
fn render_segment(&self, _doc: &Document, _seg_id: &str) -> Option<String> { ... }
}Expand description
A file parser. Implementations live in one crate per format (bilingua-html, bilingua-docx, …).
The trait is open: another crate may implement it for a format of its own. It implements id, extensions,
sniff, extract and merge_with; the rest have defaults, and a 1.x release adds methods only with defaults.
Such a filter is called directly (and segmented with bilingua_seg::segment): the bilingua facade’s
filters() list is Bilingua’s own formats. What a writer needs is public: Document::new, Unit::new,
Skeleton::text and friends, parser_info! with a SKELETON_SCHEMA constant, check_parser, the
enc codecs and the guard limits. Start extract and merge_with with check_own_options.
Required Methods§
Sourcefn extensions(&self) -> &'static [&'static str]
fn extensions(&self) -> &'static [&'static str]
Lowercase extensions without the dot, e.g. ["html", "htm"].
Sourcefn sniff(&self, bytes: &[u8]) -> bool
fn sniff(&self, bytes: &[u8]) -> bool
True when the bytes look like this format (used when the extension is missing or ambiguous).
Sourcefn extract(
&self,
bytes: &[u8],
opts: &ExtractOptions,
) -> Result<Document, Error>
fn extract( &self, bytes: &[u8], opts: &ExtractOptions, ) -> Result<Document, Error>
Source file to document. One segment per unit; no sentence splitting here.
Sourcefn merge_with(
&self,
doc: &Document,
opts: &MergeOptions,
) -> Result<Vec<u8>, Error>
fn merge_with( &self, doc: &Document, opts: &MergeOptions, ) -> Result<Vec<u8>, Error>
Document back to a file in the original format, targets in place of sources, with MergeOptions. Every
format honours both options: output_encoding (keep the file’s encoding, or write UTF-8) and
stamp_target_always (stamp the target language and fill plurals even when nothing is translated; the
rule is MergeOptions::should_stamp).
Provided Methods§
Sourcefn bilingual(&self) -> bool
fn bilingual(&self) -> bool
True when the format carries source and target (PO, XLIFF and the like): its extract may fill segment targets and states. False for source files.
Sourcefn format_option_keys(&self) -> &'static [FormatOptionKey]
fn format_option_keys(&self) -> &'static [FormatOptionKey]
The ExtractOptions::format_options keys this filter reads ("csv.columns", …), each with the
<family>. prefix of its format. The facade refuses keys no filter declares; the filter itself refuses
undeclared keys of its own families (check_own_options). Default: none.
Sourcefn merge(&self, doc: &Document) -> Result<Vec<u8>, Error>
fn merge(&self, doc: &Document) -> Result<Vec<u8>, Error>
Filter::merge_with with the default options: the file’s own encoding, and no stamp when nothing is
translated (an untouched merge gives the original bytes).
Sourcefn preview(&self, _doc: &Document) -> Option<String>
fn preview(&self, _doc: &Document) -> Option<String>
For formats a browser can show: the translated document as HTML in which every segment’s text sits in
<span data-bl-seg="SEGMENT_ID">..</span>, so a live preview can patch one segment without reloading.
Scripts must be stripped. None when the format has no preview.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".