docx_rs/documents/elements/
footnote_reference.rs1use serde::ser::{Serialize, SerializeStruct, Serializer};
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::{xml_builder::*, Footnote, Paragraph};
6
7#[derive(Debug, Clone, PartialEq)]
8
9pub struct FootnoteReference {
10 pub id: usize,
11 pub style: String,
12 pub content: Vec<Paragraph>,
13}
14
15impl FootnoteReference {
16 pub fn new(id: usize) -> Self {
17 FootnoteReference {
18 id,
19 style: "FootnoteReference".to_string(),
20 content: vec![],
21 }
22 }
23 pub fn footnote(&mut self, p: Paragraph) {
25 self.content.push(p)
26 }
27}
28impl From<Footnote> for FootnoteReference {
29 fn from(footnote: Footnote) -> Self {
30 FootnoteReference {
31 id: footnote.id,
32 style: "FootnoteReference".to_string(),
33 content: footnote.content,
34 }
35 }
36}
37
38impl BuildXML for FootnoteReference {
39 fn build_to<W: Write>(
40 &self,
41 stream: xml::writer::EventWriter<W>,
42 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
43 XMLBuilder::from(stream)
44 .footnote_reference(self.id)?
45 .into_inner()
46 }
47}
48
49impl Serialize for FootnoteReference {
50 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51 where
52 S: Serializer,
53 {
54 let mut t = serializer.serialize_struct("FootnoteReference", 2)?;
55 t.serialize_field("id", &self.id)?;
56 t.end()
57 }
58}
59
60#[cfg(test)]
61mod tests {
62
63 use super::*;
64 #[cfg(test)]
65 use pretty_assertions::assert_eq;
66 use std::str;
67
68 #[test]
69 fn test_footnotereference_build() {
70 let b = FootnoteReference::new(1).build();
71 assert_eq!(
72 str::from_utf8(&b).unwrap(),
73 r#"<w:footnoteReference w:id="1" />"#
74 );
75 }
76
77 #[test]
78 fn test_footnotereference_json() {
79 let t = FootnoteReference::new(1);
80 assert_eq!(serde_json::to_string(&t).unwrap(), r#"{"id":1}"#);
81 }
82}