pub struct Document { /* private fields */ }
Expand description
Represents the document in Org
struct.
Each Org
struct only has one Document
.
Implementations§
Source§impl Document
impl Document
Sourcepub fn section_node(self) -> Option<NodeId>
pub fn section_node(self) -> Option<NodeId>
Returns the ID of the section element of this document,
or None
if it has no section.
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 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());
Sourcepub fn first_child(self, org: &Org<'_>) -> Option<Headline>
pub fn first_child(self, org: &Org<'_>) -> Option<Headline>
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());
Sourcepub fn last_child(self, org: &Org<'_>) -> Option<Headline>
pub fn last_child(self, org: &Org<'_>) -> Option<Headline>
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());
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 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
"#,
);
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 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());
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 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§
Auto Trait Implementations§
impl Freeze for Document
impl RefUnwindSafe for Document
impl Send for Document
impl Sync for Document
impl Unpin for Document
impl UnwindSafe for Document
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more