Struct orgize::Headline[][src]

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

Represents a headline in Org struct.

Each Org has zero or more Headlines.

Implementations

Creates a new detached Headline.

Returns the level of this headline.

Returns the ID of the headline element of this headline.

Returns the ID of the title element of this headline.

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

Returns a reference to the title element of this headline.

Returns a mutual reference to the title element of this headline.

Don’t change the level and content of the &mut Titile directly. Alternatively, uses Headline::set_level and Headline::set_title_content.

let mut org = Org::parse("* h1");

let h1 = org.headlines().nth(0).unwrap();

h1.title_mut(&mut org).priority = Some('A');

let mut writer = Vec::new();
org.write_org(&mut writer).unwrap();
assert_eq!(
    String::from_utf8(writer).unwrap(),
    "* [#A] h1\n",
);

Changes the level of this headline.

Returns an error if this headline is attached and the given new level doesn’t meet the requirements.

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

let mut h1_2 = org.headlines().nth(2).unwrap();

// level must be greater than or equal to 2, and smaller than or equal to 6
assert!(h1_2.set_level(42, &mut org).is_err());

assert!(h1_2.set_level(5, &mut org).is_ok());

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

// detached headline's levels can be changed freely
let mut new_headline = Headline::new(
    Title {
        raw: "new".into(),
        ..Default::default()
    },
    &mut org,
);
new_headline.set_level(42, &mut org).unwrap();

Changes the title content of this headline.

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

let h1 = org.headlines().nth(0).unwrap();
let h1_1 = org.headlines().nth(1).unwrap();

h1.set_title_content("H1", &mut org);
h1_1.set_title_content(String::from("*H1_1*"), &mut org);

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

Changes the section content of this headline.

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

let mut h1 = org.headlines().nth(0).unwrap();
let mut h1_1 = org.headlines().nth(1).unwrap();

h1.set_section_content("s1", &mut org);
h1_1.set_section_content(String::from("*s1_1*"), &mut org);

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

Returns the parent of this headline, or None if it is detached or attached to the document.

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

let h1 = org.headlines().nth(0).unwrap();
let h1_1 = org.headlines().nth(1).unwrap();
let h1_2_1 = org.headlines().nth(3).unwrap();

assert_eq!(h1_1.parent(&org).unwrap().title(&org).raw, "h1");
assert_eq!(h1_2_1.parent(&org).unwrap().title(&org).raw, "h1_2");

assert!(h1.parent(&org).is_none());

// detached headline have no parent
assert!(Headline::new(Title::default(), &mut org).parent(&org).is_none());

Returns an iterator of this headline’s children.

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

let h1 = org.headlines().nth(0).unwrap();

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

assert_eq!(iter.next().unwrap().title(&org).raw, "h1_1");
assert_eq!(iter.next().unwrap().title(&org).raw, "h1_2");
assert_eq!(iter.next().unwrap().title(&org).raw, "h1_3");
assert!(iter.next().is_none());

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

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

let h1_1 = org.headlines().nth(1).unwrap();
let h1_2 = org.headlines().nth(2).unwrap();
let h1_3 = org.headlines().nth(5).unwrap();

assert_eq!(h1_2.first_child(&org).unwrap().title(&org).raw, "h1_2_1");

assert!(h1_1.first_child(&org).is_none());
assert!(h1_3.first_child(&org).is_none());

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

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

let h1_1 = org.headlines().nth(1).unwrap();
let h1_2 = org.headlines().nth(2).unwrap();
let h1_3 = org.headlines().nth(5).unwrap();

assert_eq!(h1_2.last_child(&org).unwrap().title(&org).raw, "h1_2_2");

assert!(h1_1.last_child(&org).is_none());
assert!(h1_3.last_child(&org).is_none());

Returns the previous sibling of this headline, or None if it is a first child.

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

let h1_1 = org.headlines().nth(1).unwrap();
let h1_2 = org.headlines().nth(2).unwrap();
let h1_2_1 = org.headlines().nth(3).unwrap();

assert_eq!(h1_2.previous(&org).unwrap().title(&org).raw, "h1_1");

assert!(h1_1.previous(&org).is_none());
assert!(h1_2_1.previous(&org).is_none());

Returns the next sibling of this headline, or None if it is a last child.

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

let h1_2 = org.headlines().nth(2).unwrap();
let h1_2_2 = org.headlines().nth(4).unwrap();
let h1_3 = org.headlines().nth(5).unwrap();

assert_eq!(h1_2.next(&org).unwrap().title(&org).raw, "h1_3");

assert!(h1_3.next(&org).is_none());
assert!(h1_2_2.next(&org).is_none());

Detaches this headline from arena.

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

let h1_2 = org.headlines().nth(2).unwrap();

h1_2.detach(&mut org);

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

Returns true if this headline is detached.

Appends a new child to this headline.

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

let h1_1 = org.headlines().nth(1).unwrap();

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

// level must be greater than 2, and smaller than or equal to 5
h1_1_2.set_level(2, &mut org).unwrap();
assert!(h1_1.append(h1_1_2, &mut org).is_err());
h1_1_2.set_level(6, &mut org).unwrap();
assert!(h1_1.append(h1_1_2, &mut org).is_err());

h1_1_2.set_level(4, &mut org).unwrap();
assert!(h1_1.append(h1_1_2, &mut org).is_ok());

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

// cannot append an attached headline
assert!(h1_1.append(h1_1_2, &mut org).is_err());

Prepends a new child to this headline.

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

let h1_1 = org.headlines().nth(1).unwrap();

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

// level must be greater than or equal to 5
h1_1_2.set_level(2, &mut org).unwrap();
assert!(h1_1.prepend(h1_1_2, &mut org).is_err());

h1_1_2.set_level(5, &mut org).unwrap();
assert!(h1_1.prepend(h1_1_2, &mut org).is_ok());

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

Inserts a new sibling before this headline.

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
** h1_1
**** h1_1_1
*** h1_1_3
"#,
    );

let h1_1_3 = org.headlines().nth(3).unwrap();

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

// level must be greater than or equal to 3, but smaller than or equal to 4
h1_1_2.set_level(2, &mut org).unwrap();
assert!(h1_1_3.insert_before(h1_1_2, &mut org).is_err());
h1_1_2.set_level(5, &mut org).unwrap();
assert!(h1_1_3.insert_before(h1_1_2, &mut org).is_err());

h1_1_2.set_level(4, &mut org).unwrap();
assert!(h1_1_3.insert_before(h1_1_2, &mut org).is_ok());

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

// cannot insert an attached headline
assert!(h1_1_3.insert_before(h1_1_2, &mut org).is_err());

Inserts a new sibling after this headline.

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
** h1_1
**** h1_1_1
*** h1_1_3
"#,
    );

let h1_1_1 = org.headlines().nth(2).unwrap();

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

// level must be greater than or equal to 3, but smaller than or equal to 4
h1_1_2.set_level(2, &mut org).unwrap();
assert!(h1_1_1.insert_after(h1_1_2, &mut org).is_err());
h1_1_2.set_level(5, &mut org).unwrap();
assert!(h1_1_1.insert_after(h1_1_2, &mut org).is_err());

h1_1_2.set_level(4, &mut org).unwrap();
assert!(h1_1_1.insert_after(h1_1_2, &mut org).is_ok());

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

// cannot insert an attached headline
assert!(h1_1_1.insert_after(h1_1_2, &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.