Skip to main content

Validator

Struct Validator 

Source
pub struct Validator { /* private fields */ }
Expand description

A rule set ready to validate documents: its schema loaded and its stylesheets prepared, once. Validating a document only reads it, so one Validator serves any number of documents, from any number of threads at once (Send + Sync); nothing of the rule set is copied per document. A clone shares the loaded rule set (it is reference-counted): cloning is cheap, and the clones can be moved to other threads.

Implementations§

Source§

impl Validator

Source

pub fn from_cache(name: &str) -> Result<Self, Error>

The fetched rule set name (see fetched_rule_sets), from the cache in its default directory (RuleSetCache::new), checked in full. Never downloads anything: fetch it first with RuleSetCache::fetch (feature fetch), or einvoice-validate rules fetch NAME.

§Errors

As RuleSetCache::new and RuleSetCache::load; Error::NotCached when it has not been fetched.

Source§

impl Validator

Source

pub fn builtin(name: &str) -> Result<Self, Error>

The built-in rule set name (see BUILTIN_RULE_SETS).

§Errors

Error::Bundle for an unknown name; otherwise as Validator::from_bundle, which none of the built-in rule sets fails in practice (a test loads each).

Source

pub fn cen_ubl() -> Result<Self, Error>

The built-in rule set cen-ubl: the CEN EN 16931 validation artefacts for UBL, release 1.3.17 (EUPL-1.2), embedded unmodified, with the UBL 2.1 schemas.

§Errors

None in practice (a test loads it); the error is returned rather than hidden.

Source

pub fn cen_cii() -> Result<Self, Error>

The built-in rule set cen-cii: the CEN EN 16931 validation artefacts for UN/CEFACT CII D16B, release 1.3.17 (EUPL-1.2), embedded unmodified, with the UN/CEFACT CII D16B schemas embedded in einvoice_xsd (SchemaId::CiiD16b).

§Errors

None in practice (a test loads it); the error is returned rather than hidden.

Source

pub fn from_bundle( manifest: &str, files: &[(&str, &[u8])], ) -> Result<Self, Error>

Loads a bundle from memory: its manifest and its files, by their paths in the manifest. Files not named by the manifest are ignored.

§Errors

Error::Bundle for an invalid manifest, a missing file or a stylesheet that is not well-formed; Error::Checksum for a file whose SHA-256 differs from the manifest’s; Error::Unsupported for a stylesheet or schema outside the supported subset.

Source

pub fn from_bundle_dir(dir: impl AsRef<Path>) -> Result<Self, Error>

Loads the bundle in the directory dir: its manifest MANIFEST_FILE and the files it names, relative to dir.

§Errors

Error::Io when a file cannot be read, otherwise as Validator::from_bundle.

Source

pub const fn rule_set(&self) -> &RuleSet

The rule set: name, version, schema, stylesheets and their checksums.

Source

pub fn validate(&self, document: &[u8]) -> Result<Report, Error>

Validates a document with the default options (no document URI).

§Errors

As Validator::validate_with.

Source

pub fn validate_owned(&self, document: Vec<u8>) -> Result<Report, Error>

Validates a document given as a buffer, with the default options (no document URI).

§Errors

As Validator::validate_with.

Source

pub fn validate_owned_with( &self, document: Vec<u8>, options: &ValidateOptions<'_>, ) -> Result<Report, Error>

Validator::validate_with on a document given as a buffer, which the validation keeps instead of copying: the buffer becomes the text without a copy, and the tree of the last stylesheet refers to it, so that while that stylesheet runs the document is in memory once rather than twice. Same report as Validator::validate_with; for a caller that has no further use for the bytes (a file just read).

§Errors

As Validator::validate_with.

Source

pub fn validate_with( &self, document: &[u8], options: &ValidateOptions<'_>, ) -> Result<Report, Error>

Validates a document: checks that it is well-formed XML, validates it against the rule set’s schema, then runs every stylesheet of the rule set on it. The stylesheets run even when the schema reports errors, so that the report is complete.

In a rule set that selects by scenario (RuleSet::selects_by_scenario), the match expression of each scenario is then evaluated on the document, as the KoSIT validator evaluates it (an expression that raises a dynamic error is false): when exactly one is true, the stylesheets of that scenario run, in its order; otherwise the report has no stylesheet and no schema error, only Report::no_scenario, and the document is invalid.

When a stylesheet raises a dynamic error on a document that has schema errors (a non-numeric amount the rules compute with, …), the report is returned all the same, invalid, with the error in StylesheetReport::failure; without schema errors, the error is returned.

The document must be in an encoding the validator reads (UTF-8, UTF-16, US-ASCII, ISO-8859-1, ISO-8859-15 or windows-1252, see einvoice_xml::decode) and have no document type declaration.

§Errors

Error::NotXml when the document is not well-formed XML (bytes that are not characters of its encoding included); Error::Unsupported for an encoding the validator does not read, a document type declaration, or a construct the engine refuses; Error::Limit for a document beyond a limit; Error::OptionOutOfRange for options beyond what the target allows (on WebAssembly); Error::Transform when a stylesheet raises a dynamic error on the document; Error::Bundle when a stylesheet’s result is not an SVRL report; Error::Internal for a bug.

Trait Implementations§

Source§

impl Clone for Validator

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 Validator

Source§

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

Formats the value using the given formatter. Read more

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> 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> Same for T

Source§

type Output = T

Should always be Self
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.