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
impl Validator
Sourcepub fn from_cache(name: &str) -> Result<Self, Error>
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
impl Validator
Sourcepub fn builtin(name: &str) -> Result<Self, Error>
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).
Sourcepub fn cen_ubl() -> Result<Self, Error>
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.
Sourcepub fn cen_cii() -> Result<Self, Error>
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.
Sourcepub fn from_bundle(
manifest: &str,
files: &[(&str, &[u8])],
) -> Result<Self, Error>
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.
Sourcepub fn from_bundle_dir(dir: impl AsRef<Path>) -> Result<Self, Error>
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.
Sourcepub const fn rule_set(&self) -> &RuleSet
pub const fn rule_set(&self) -> &RuleSet
The rule set: name, version, schema, stylesheets and their checksums.
Sourcepub fn validate(&self, document: &[u8]) -> Result<Report, Error>
pub fn validate(&self, document: &[u8]) -> Result<Report, Error>
Validates a document with the default options (no document URI).
§Errors
Sourcepub fn validate_owned(&self, document: Vec<u8>) -> Result<Report, Error>
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
Sourcepub fn validate_owned_with(
&self,
document: Vec<u8>,
options: &ValidateOptions<'_>,
) -> Result<Report, Error>
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
Sourcepub fn validate_with(
&self,
document: &[u8],
options: &ValidateOptions<'_>,
) -> Result<Report, Error>
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.