docx_rs/documents/elements/
bookmark_start.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7#[derive(Serialize, Debug, Clone, PartialEq)]
8pub struct BookmarkStart {
9    pub id: usize,
10    pub name: String,
11}
12
13impl BookmarkStart {
14    pub fn new(id: usize, name: impl Into<String>) -> BookmarkStart {
15        BookmarkStart {
16            id,
17            name: name.into(),
18        }
19    }
20}
21
22impl BuildXML for BookmarkStart {
23    fn build_to<W: Write>(
24        &self,
25        stream: xml::writer::EventWriter<W>,
26    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
27        XMLBuilder::from(stream)
28            .bookmark_start(&self.id.to_string(), &self.name)?
29            .into_inner()
30    }
31}
32
33#[cfg(test)]
34mod tests {
35
36    use super::*;
37    #[cfg(test)]
38    use pretty_assertions::assert_eq;
39    use std::str;
40
41    #[test]
42    fn test_bookmark_start() {
43        let c = BookmarkStart::new(0, "mockname");
44        let b = c.build();
45        assert_eq!(
46            str::from_utf8(&b).unwrap(),
47            r#"<w:bookmarkStart w:id="0" w:name="mockname" />"#
48        );
49    }
50}