Skip to main content

Document

Struct Document 

Source
#[non_exhaustive]
pub struct Document { pub format: String, pub name: Option<String>, pub source_lang: Option<String>, pub target_lang: Option<String>, pub granularity: Granularity, pub units: Vec<Unit>, pub skeleton: Skeleton, pub warnings: Vec<Warning>, pub source_encoding: Option<String>, /* private fields */ }
Expand description

An extracted file: its units and its skeleton. Build it with Document::new and the with_* methods; edit targets with Document::set_target and friends, which check the tags. Default is an empty document of no format (no writer accepts it).

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§format: String

Parser id, e.g. “html”, “docx”, “json”.

§name: Option<String>

Original file name, when known.

§source_lang: Option<String>

The source language (a BCP 47 tag, en, de-DE), when known.

§target_lang: Option<String>

The target language, when known.

§granularity: Granularity

How finely the units are segmented.

§units: Vec<Unit>

The translatable units, in file order.

§skeleton: Skeleton

Everything else in the file.

§warnings: Vec<Warning>

Things the reader noticed but did not refuse the file for (text that looks garbled in the code page it was read with, …). Left out of the JSON when empty.

§source_encoding: Option<String>

The encoding a text file was decoded from when it was not UTF-8 (utf-16le, windows-1252, shift_jis, …); None for UTF-8 (with or without BOM) and for zip and binary files. The bilingua facade fills it from the skeleton; left out of the JSON when unset.

Implementations§

Source§

impl Document

Source

pub fn plural_categories(&self, kind: PluralKind) -> Option<Vec<&'static str>>

The categories the target language uses for kind, in CLDR order and always with other (one, few, many, other for Polish cardinals), read from target_lang through the shared CLDR table (the Babel data Localization OS uses; aliases such as deu and iw resolve). None for a select, without a target language, or for one the table does not know. Units hold no copy of them.

Source§

impl Document

Source

pub fn new( format: impl Into<String>, units: Vec<Unit>, skeleton: Skeleton, ) -> Document

A document of format (the filter id) with its units and skeleton; no name, no languages, sentence granularity, no warnings.

Source

pub fn with_options(self, opts: &ExtractOptions) -> Document

Takes the languages and the granularity from the options the file was opened with (what every parser does).

Source

pub fn with_name(self, name: impl IntoOptionString) -> Document

Sets (or with None clears) the file name.

Source

pub fn with_source_lang(self, lang: impl IntoOptionString) -> Document

Sets (or clears) the source language.

Source

pub fn with_target_lang(self, lang: impl IntoOptionString) -> Document

Sets (or clears) the target language.

Source

pub fn with_granularity(self, granularity: Granularity) -> Document

Sets the granularity.

Source

pub fn detected_source_encoding(&self) -> Option<String>

The encoding of a text skeleton (or of a LocOS-profile markdown, po or subtitle skeleton), when it is not UTF-8: the base of its tag (utf-16le-bom and utf-16le both give utf-16le). None for UTF-8, binary plists, zip and binary skeletons.

Source

pub fn with_warnings(self, warnings: Vec<Warning>) -> Document

Replaces the warnings.

Source

pub fn segments(&self) -> impl Iterator<Item = (&Unit, &Segment)>

Every segment with its unit, in order.

Source

pub fn segment_count(&self) -> usize

The number of segments.

Source

pub fn find(&self, seg_id: &str) -> Option<(&Unit, &Segment)>

The segment with this id and its unit.

Source

pub fn set_target( &mut self, seg_id: &str, target: Vec<Inline>, ) -> Result<(), Error>

Sets a segment’s target. Rejects unknown segments, untranslatable units, segments in state SegState::Locked, and tags the source lacks or repeats. A missing tag is allowed (the writer leaves it out), as in catcore.

The first call builds an index of segment ids (one pass over the document); later calls find the segment through it, so setting every segment of a 12 000 unit file is linear, not quadratic. The index is checked on each use and rebuilt when the units were edited since (a segment added, removed or renamed through the public fields); for many segments at once Document::set_targets is still the lighter call. When two segments share an id, the first one in the document wins as long as the units are not edited between calls.

Source

pub fn set_target_repeating( &mut self, seg_id: &str, target: Vec<Inline>, ) -> Result<(), Error>

Document::set_target that lets a translation repeat a standalone placeholder tag (the # of an ICU plural, Chrome $x$, go {{..}}), as Localization OS does; paired tags still keep the exactly-once rule.

Source

pub fn set_targets<I, S>(&mut self, items: I) -> Result<(), Error>
where I: IntoIterator<Item = (S, Vec<Inline>)>, S: AsRef<str>,

Sets many targets in one pass: one id index is built for the whole call, so 100 000 segments take milliseconds where 100 000 Document::set_target calls scan the document each time. Items are applied in order with the same rules as set_target; the first error stops the call and is returned, items before it stay applied. When an id appears more than once, the segment is set by each item in turn (the last wins).

Source

pub fn set_states<I, S>(&mut self, items: I) -> Result<(), Error>
where I: IntoIterator<Item = (S, SegState)>, S: AsRef<str>,

Sets many states in one pass (see Document::set_targets).

Source

pub fn set_target_empty(&mut self, seg_id: &str) -> Result<(), Error>

Marks a segment as translated to nothing on purpose (see Segment::empty_target): the writer writes an empty value for it. State Draft, as for any new target. Rejects unknown segments, untranslatable units and locked segments.

Source

pub fn set_state(&mut self, seg_id: &str, state: SegState) -> Result<(), Error>

Sets a segment’s state (for many, Document::set_states).

Like set_target, the first segment with the id wins as long as the units are not edited between calls.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Document

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Document

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Document

Source§

impl PartialEq for Document

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Document

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Document

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.