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

use crate::table::TableData;

#[derive(Clone, Debug)]
pub struct Tags {
	tags: Vec<String>,
}

impl Default for Tags {
	fn default() -> Self {
		Self { tags: vec![] }
	}
}

impl TableData for Tags {
	fn parse(tag_str: &str) -> Result<Self, &'static str> {
		let tags = tag_str
			.split(',')
			.filter_map(|s| match s.is_empty() {
				true => None,
				false => Some(s.to_owned()),
			})
			.collect();

		Ok(Self { tags })
	}
}

impl fmt::Display for Tags {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let tag_str = self.tags.join(",");

		write!(f, "{tag_str}")
	}
}