Skip to main content

Filter

Trait Filter 

Source
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§

Source

fn id(&self) -> &'static str

Short id, e.g. “html”.

Source

fn extensions(&self) -> &'static [&'static str]

Lowercase extensions without the dot, e.g. ["html", "htm"].

Source

fn sniff(&self, bytes: &[u8]) -> bool

True when the bytes look like this format (used when the extension is missing or ambiguous).

Source

fn extract( &self, bytes: &[u8], opts: &ExtractOptions, ) -> Result<Document, Error>

Source file to document. One segment per unit; no sentence splitting here.

Source

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§

Source

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.

Source

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.

Source

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).

Source

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.

Source

fn render_segment(&self, _doc: &Document, _seg_id: &str) -> Option<String>

The inner HTML of one segment’s span in preview() (its current target, or source when empty), so a live preview can patch that span in place. None when the format has no preview or the id is unknown.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§