Skip to main content

Document

Struct Document 

Source
pub struct Document {
    pub frontmatter: Frontmatter,
    pub body: String,
}
Expand description

A parsed OKF concept document.

Fields§

§frontmatter: Frontmatter

The YAML frontmatter block (empty if the file had none).

§body: String

Everything after the frontmatter.

Implementations§

Source§

impl Document

Source

pub fn new(frontmatter: Frontmatter, body: impl Into<String>) -> Self

Creates a document from frontmatter and a body.

Source

pub fn parse(text: &str) -> Result<Self, DocumentError>

Parses a document from raw file text.

If the file does not begin with a --- frontmatter delimiter, the entire text is treated as the body and the frontmatter is empty (matching the reference parser). An opened-but-unclosed frontmatter block is an error.

§Line endings

The two paths handle line endings the same way the reference implementation does, by deliberate parity:

  • No frontmatter: the body is kept verbatim, so a file with CRLF line endings round-trips byte-identically. This mirrors the reference’s return cls(frontmatter={}, body=text).
  • With frontmatter: the body is rebuilt via lines().join("\n"), which normalizes \r\n (and a trailing \r) to \n. This mirrors the reference’s text.splitlines() + "\n".join(...). Anything inside the frontmatter block is likewise normalized before YAML parsing.
§Errors

Returns DocumentError::UnterminatedFrontmatter if the opening --- has no matching close, DocumentError::InvalidYaml if the frontmatter is not valid YAML, and DocumentError::FrontmatterNotMapping if it parses to a scalar or sequence rather than a mapping.

Source

pub fn serialize(&self) -> String

Serializes the document back to text: frontmatter delimited by ---, a blank line, then the body (terminated by a newline).

parse followed by serialize preserves frontmatter key order and the body (modulo trailing-newline normalization), matching the reference. Flow collections are re-emitted in block style, which is the same value written differently.

Source

pub fn validate(&self) -> Result<(), DocumentError>

Validates the document: the frontmatter must carry a non-empty type, and nothing else is required.

That single check is the whole of document-level validation in v0.2, and it matches the reference implementation’s OKFDocument.validate. Every other field the spec describes is a SHOULD, so a concept carrying only type passes here; see Document::missing_recommended for the producer-side checklist and validate_bundle (in the okf-validator crate) for the full diagnostics.

§Errors

Returns DocumentError::MissingKeys listing every required key that is absent, empty, or has the wrong shape.

The recommended frontmatter keys this document leaves unset, plus runtime when the concept is an Attested Computation, which the spec requires it to carry.

None of these is a conformance failure, so Document::validate ignores them: the spec forbids rejecting a concept for a missing optional field. This is the checklist a producer wants before publishing, and it is what validate_bundle (in the okf-validator crate) reports as warnings. An empty result means the document is fully filled in.

generated counts as set when a legacy v0.1 timestamp stands in for it, since consumers may read one for the other.

Extracts all markdown links found in the body.

Source

pub fn section(&self, heading: &str) -> Vec<&str>

The non-blank lines under a top-level # heading in the body, up to the next top-level heading.

The spec gives # Schema, # Examples, and # Computation conventional meaning without attaching required behaviour, so this is the primitive a consumer needs to read any of them. A port of the reference’s _section_content_lines, including its details: heading is matched in full (pass "# Schema"), only # counts as a heading so ## subheadings stay inside the section, and each line keeps its original indentation.

Returns an empty vector when no such section exists. A repeated heading contributes its lines to the same result.

Source

pub fn footnote_refs(&self) -> Vec<FootnoteRef>

Extracts the body’s [^label] attribution markers.

Source

pub fn footnote_definitions(&self) -> Vec<FootnoteDef>

Extracts the body’s [^label]: text footnote definitions.

Source

pub fn attributions(&self) -> Vec<Attribution>

Joins the body’s footnotes to the sources entries they name, giving per-claim attribution.

Labels that match no source are still returned, with Attribution::source set to None.

Source

pub fn inline_computation(&self) -> Option<InlineComputation>

The # Computation code block from the body, if there is one.

Source

pub fn attested_computation(&self) -> Option<AttestedComputation>

The Attested Computation contract: the computation frontmatter resolved against the body’s # Computation block.

Returns None unless type is Attested Computation; call AttestedComputation::from_parts directly to read the same keys off a concept of another type.

Source

pub fn citations(&self) -> Vec<Citation>

Extracts numbered entries from a legacy v0.1 # Citations section.

v0.2 supersedes this with sources and footnote attribution; Document::attributions is the v0.2 equivalent. Consumers MAY keep reading # Citations for v0.1 documents.

Source

pub fn has_legacy_citations(&self) -> bool

true when the body carries a legacy # Citations section, which a v0.2 producer should have migrated to sources.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Document

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() -> Document

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

impl Display for Document

Source§

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

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

impl FromStr for Document

Source§

type Err = DocumentError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Document

Source§

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

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

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

Inequality operator !=. 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.