Struct orgize::Document[][src]

pub struct Document { /* fields omitted */ }
Expand description

Represents the document in Org struct.

Each Org struct only has one Document.

Implementations

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

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

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

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

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
"#,
);

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.