about summary refs log tree commit diff
path: root/wikarden/src/table/sectiondata.rs
blob: 745d768e9c8c89c2ae1e1c1b9797021abcf4094a (plain)
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
use std::fmt;

use crate::{fs::WikPath, table::TableData};

#[derive(Clone, Debug)]
pub struct SectionData {
	home: Option<WikPath>,
}

impl SectionData {
	pub fn home(&self) -> Option<&WikPath> {
		self.home.as_ref()
	}

	pub fn set_home<P: Into<WikPath>>(&mut self, home: Option<P>) {
		self.home = home.map(|h| h.into());
	}
}

impl Default for SectionData {
	fn default() -> Self {
		Self { home: None }
	}
}

impl TableData for SectionData {
	fn parse(raw: &str) -> Result<Self, &'static str> {
		// WARNING: the split here is unsafe, as it will split comma in the
		// quoted strings as well. we need to handle this, probably with a
		// new iterator. it is okay for now, but be warned.
		let mut splits = raw.split(',').filter_map(|s| s.split_once('='));

		let mut home = None;
		while let Some((key, value)) = splits.next() {
			let decoded = match crate::quotes_decode(value) {
				None => continue,
				Some((value, _rest)) => value,
			};

			match key {
				"home" => home = Some(decoded.into()),
				_ => (),
			}
		}

		Ok(Self { home })
	}
}

impl fmt::Display for SectionData {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if let Some(home) = self.home.as_ref() {
			let quoted_home = crate::quotes_encode(home.as_ref());
			write!(f, "home={quoted_home}")?;
		}

		Ok(())
	}
}