Struct Document

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

Represents the document in Org struct.

Each Org struct only has one Document.

Implementations§

Source§

impl Document

Source

pub fn section_node(self) -> Option<NodeId>

Returns the ID of the section element of this document, or None if it has no section.

Source

pub fn children<'a>( self, org: &'a Org<'_>, ) -> impl Iterator<Item = Headline> + 'a

Returns an iterator of this document’s children.

let mut org = Org::parse(
    r#"
** h1
** h2
*** h2_1
*** h2_2
** h3
"#,
    );

let d = org.document();

let mut iter = d.children(&org);

assert_eq!(iter.next().unwrap().title(&org).raw, "h1");
assert_eq!(iter.next().unwrap().title(&org).raw, "h2");
assert_eq!(iter.next().unwrap().title(&org).raw, "h3");
assert!(iter.next().is_none());
Source

pub fn first_child(self, org: &Org<'_>) -> Option<Headline>

Returns the first child of this document, or None if it has no child.

let mut org = Org::parse(
    r#"
** h1
** h2
*** h2_1
*** h2_2
** h3
"#,
    );

let d = org.document();

assert_eq!(d.first_child(&org).unwrap().title(&org).raw, "h1");
let org = Org::new();

assert!(org.document().first_child(&org).is_none());
Source

pub fn last_child(self, org: &Org<'_>) -> Option<Headline>

Returns the last child of this document, or None if it has no child.

let mut org = Org::parse(
    r#"
** h1_1
** h1_2
*** h1_2_1
*** h1_2_2
** h1_3
"#,
    );

let d = org.document();

assert_eq!(d.last_child(&org).unwrap().title(&org).raw, "h1_3");
let org = Org::new();

assert!(org.document().last_child(&org).is_none());
Source

pub fn set_section_content<'a, S>(&mut self, content: S, org: &mut Org<'a>)
where S: Into<Cow<'a, str>>,

Changes the section content of this document.

let mut org = Org::parse(
    r#"
** h1_1
** h1_2
"#,
);

let mut d = org.document();

d.set_section_content("s", &mut org);

let mut writer = Vec::new();
org.write_org(&mut writer).unwrap();
assert_eq!(
    String::from_utf8(writer).unwrap(),
    r#"
s
** h1_1
** h1_2
"#,
);
Source

pub fn append( self, hdl: Headline, org: &mut Org<'_>, ) -> Result<(), ValidationError>

Appends a new child to this document.

Returns an error if the given new child was already attached, or the given new child didn’t meet the requirements.

let mut org = Org::parse(
    r#"
***** h1
**** h2
*** h3
"#,
);

let d = org.document();

let mut h4 = Headline::new(
    Title {
        raw: "h4".into(),
        ..Default::default()
    },
    &mut org,
);

// level must be smaller than or equal to 3
h4.set_level(4, &mut org).unwrap();
assert!(d.append(h4, &mut org).is_err());

h4.set_level(2, &mut org).unwrap();
assert!(d.append(h4, &mut org).is_ok());

let mut writer = Vec::new();
org.write_org(&mut writer).unwrap();
assert_eq!(
    String::from_utf8(writer).unwrap(),
    r#"
***** h1
**** h2
*** h3
** h4
"#,
);

// cannot append an attached headline
assert!(d.append(h4, &mut org).is_err());
Source

pub fn prepend( self, hdl: Headline, org: &mut Org<'_>, ) -> Result<(), ValidationError>

Prepends a new child to this document.

Returns an error if the given new child was already attached, or the given new child didn’t meet the requirements.

let mut org = Org::parse(
    r#"
** h2
** h3
"#,
);

let d = org.document();

let mut h1 = Headline::new(
    Title {
        raw: "h1".into(),
        ..Default::default()
    },
    &mut org,
);

// level must be greater than 2
h1.set_level(1, &mut org).unwrap();
assert!(d.prepend(h1, &mut org).is_err());

h1.set_level(4, &mut org).unwrap();
assert!(d.prepend(h1, &mut org).is_ok());

let mut writer = Vec::new();
org.write_org(&mut writer).unwrap();
assert_eq!(
    String::from_utf8(writer).unwrap(),
    r#"
**** h1
** h2
** h3
"#,
);

// cannot prepend an attached headline
assert!(d.prepend(h1, &mut org).is_err());

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Document

Returns a duplicate of the value. Read more
1.0.0 · 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 Copy 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, 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.