[][src]Struct orgize::Headline

pub struct Headline { /* fields omitted */ }

Represents a headline in Org struct.

Each Org has zero or more Headlines.

Implementations

impl Headline[src]

pub fn new<'a>(ttl: Title<'a>, org: &mut Org<'a>) -> Headline[src]

Creates a new detached Headline.

pub fn level(self) -> usize[src]

Returns the level of this headline.

pub fn headline_node(self) -> NodeId[src]

Returns the ID of the headline element of this headline.

pub fn title_node(self) -> NodeId[src]

Returns the ID of the title element of this headline.

pub fn section_node(self) -> Option<NodeId>[src]

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

pub fn title<'a: 'b, 'b>(self, org: &'b Org<'a>) -> &'b Title<'a>[src]

Returns a reference to the title element of this headline.

pub fn title_mut<'a: 'b, 'b>(self, org: &'b mut Org<'a>) -> &'b mut Title<'a>[src]

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

pub fn set_level(
    &mut self,
    lvl: usize,
    org: &mut Org
) -> Result<(), ValidationError>
[src]

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

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

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

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

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

pub fn parent(self, org: &Org) -> Option<Headline>[src]

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

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

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

pub fn first_child(self, org: &Org) -> Option<Headline>[src]

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

pub fn last_child(self, org: &Org) -> Option<Headline>[src]

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

pub fn previous(self, org: &Org) -> Option<Headline>[src]

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

pub fn next(self, org: &Org) -> Option<Headline>[src]

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

pub fn detach(self, org: &mut Org)[src]

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

pub fn is_detached(self, org: &Org) -> bool[src]

Returns true if this headline is detached.

pub fn append(self, hdl: Headline, org: &mut Org) -> Result<(), ValidationError>[src]

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

pub fn prepend(
    self,
    hdl: Headline,
    org: &mut Org
) -> Result<(), ValidationError>
[src]

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

pub fn insert_before(
    self,
    hdl: Headline,
    org: &mut Org
) -> Result<(), ValidationError>
[src]

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

pub fn insert_after(
    self,
    hdl: Headline,
    org: &mut Org
) -> Result<(), ValidationError>
[src]

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

impl Clone for Headline[src]

impl Copy for Headline[src]

impl Debug for Headline[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.