docx_reader/documents/
styles.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use serde::Serialize;

use super::*;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Styles {
	doc_defaults: DocDefaults,
	styles: Vec<Style>,
}

impl Styles {
	pub fn new() -> Styles {
		Default::default()
	}

	pub fn add_style(mut self, style: Style) -> Self {
		self.styles.push(style);
		self
	}

	pub fn default_size(mut self, size: usize) -> Self {
		self.doc_defaults = self.doc_defaults.size(size);
		self
	}

	pub fn default_spacing(mut self, spacing: i32) -> Self {
		self.doc_defaults = self.doc_defaults.spacing(spacing);
		self
	}

	pub fn default_fonts(mut self, font: RunFonts) -> Self {
		self.doc_defaults = self.doc_defaults.fonts(font);
		self
	}

	pub(crate) fn doc_defaults(mut self, doc_defaults: DocDefaults) -> Self {
		self.doc_defaults = doc_defaults;
		self
	}

	pub fn find_style_by_id(&self, id: &str) -> Option<&Style> {
		self.styles.iter().find(|s| s.style_id == id)
	}

	pub fn create_heading_style_map(&self) -> std::collections::HashMap<String, usize> {
		self.styles
			.iter()
			.filter_map(|s| {
				if s.name.is_heading() {
					let n = s.name.get_heading_number();
					n.map(|n| (s.style_id.clone(), n))
				} else {
					None
				}
			})
			.collect()
	}
}

impl Default for Styles {
	fn default() -> Self {
		Self {
			doc_defaults: DocDefaults::new(),
			styles: vec![],
		}
	}
}