pub struct Headline { /* private fields */ }
Expand description
Represents a headline in Org
struct.
Each Org
has zero or more Headline
s.
Implementations§
Source§impl Headline
impl Headline
Sourcepub fn headline_node(self) -> NodeId
pub fn headline_node(self) -> NodeId
Returns the ID of the headline element of this headline.
Sourcepub fn title_node(self) -> NodeId
pub fn title_node(self) -> NodeId
Returns the ID of the title element of this headline.
Sourcepub fn section_node(self) -> Option<NodeId>
pub fn section_node(self) -> Option<NodeId>
Returns the ID of the section element of this headline, or None
if it has no section.
Sourcepub fn title<'a: 'b, 'b>(self, org: &'b Org<'a>) -> &'b Title<'a>
pub fn title<'a: 'b, 'b>(self, org: &'b Org<'a>) -> &'b Title<'a>
Returns a reference to the title element of this headline.
Sourcepub fn title_mut<'a: 'b, 'b>(self, org: &'b mut Org<'a>) -> &'b mut Title<'a>
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",
);
Sourcepub fn set_level(
&mut self,
lvl: usize,
org: &mut Org<'_>,
) -> Result<(), ValidationError>
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();
Sourcepub fn set_title_content<'a, S>(self, content: S, org: &mut Org<'a>)
pub fn set_title_content<'a, S>(self, content: S, org: &mut Org<'a>)
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*
"#,
);
Sourcepub fn set_section_content<'a, S>(&mut self, content: S, org: &mut Org<'a>)
pub fn set_section_content<'a, S>(&mut self, content: S, org: &mut Org<'a>)
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*
"#,
);
Sourcepub fn parent(self, org: &Org<'_>) -> Option<Headline>
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());
Sourcepub fn children<'a>(
self,
org: &'a Org<'_>,
) -> impl Iterator<Item = Headline> + 'a
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());
Sourcepub fn first_child(self, org: &Org<'_>) -> Option<Headline>
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());
Sourcepub fn last_child(self, org: &Org<'_>) -> Option<Headline>
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());
Sourcepub fn previous(self, org: &Org<'_>) -> Option<Headline>
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());
Sourcepub fn next(self, org: &Org<'_>) -> Option<Headline>
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());
Sourcepub fn detach(self, org: &mut Org<'_>)
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
"#,
);
Sourcepub fn is_detached(self, org: &Org<'_>) -> bool
pub fn is_detached(self, org: &Org<'_>) -> bool
Returns true
if this headline is detached.
Sourcepub fn append(
self,
hdl: Headline,
org: &mut Org<'_>,
) -> Result<(), ValidationError>
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());
Sourcepub fn prepend(
self,
hdl: Headline,
org: &mut Org<'_>,
) -> Result<(), ValidationError>
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());
Sourcepub fn insert_before(
self,
hdl: Headline,
org: &mut Org<'_>,
) -> Result<(), ValidationError>
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());
Sourcepub fn insert_after(
self,
hdl: Headline,
org: &mut Org<'_>,
) -> Result<(), ValidationError>
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());