Headline

Struct Headline 

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

Represents a headline in Org struct.

Each Org has zero or more Headlines.

Implementations§

Source§

impl Headline

Source

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

Creates a new detached Headline.

Source

pub fn level(self) -> usize

Returns the level of this headline.

Source

pub fn headline_node(self) -> NodeId

Returns the ID of the headline element of this headline.

Source

pub fn title_node(self) -> NodeId

Returns the ID of the title element of this headline.

Source

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

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

Source

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

Returns a reference to the title element of this headline.

Source

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

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

pub fn set_level( &mut self, lvl: usize, org: &mut Org<'_>, ) -> Result<(), ValidationError>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn detach(self, org: &mut Org<'_>)

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

pub fn is_detached(self, org: &Org<'_>) -> bool

Returns true if this headline is detached.

Source

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

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

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

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

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

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

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

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§

Source§

impl Clone for Headline

Source§

fn clone(&self) -> Headline

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 Headline

Source§

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

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

impl Copy for Headline

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.