about summary refs log tree commit diff
path: root/markup/src/lineup/token.rs
blob: 78d2311c62db9a74d6688f300adff4e19995b5bd (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use core::fmt;

#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) struct IndexedToken<'raw> {
	pub idx: usize,
	pub tok: Token<'raw>,
}

impl<'raw> IndexedToken<'raw> {
	pub fn new(idx: usize, tok: Token<'raw>) -> Self {
		Self { idx, tok }
	}
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Token<'raw> {
	/// String of non-space characters
	Text(&'raw str),
	/// A space; ' '; 0x20
	Space,
	/// backtick
	Code,
	/// asterisk
	Italic,
	/// Double-asterisk
	Bold,
	/// backslash
	Escape,
	/// Percent %
	Percent,
	/// Pipe character |
	Pipe,
	/// Link Opening [
	LinkOpen,
	/// Link Closing ]
	LinkClose,
	/// Interlink Open [[
	InterlinkOpen,
	/// Interlink CLose ]]
	InterlinkClose,
}

impl<'raw> Token<'raw> {
	pub fn is_text(&self) -> bool {
		if let Self::Text(_) = self {
			true
		} else {
			false
		}
	}

	pub fn is_space(&self) -> bool {
		if let Self::Space = self { true } else { false }
	}

	pub fn len(&self) -> usize {
		match self {
			Self::Text(txt) => txt.len(),
			Self::Space
			| Self::Code
			| Self::Italic
			| Self::Escape
			| Self::Percent
			| Self::Pipe
			| Self::LinkOpen
			| Self::LinkClose => 1,
			Self::Bold | Self::InterlinkOpen | Self::InterlinkClose => 2,
		}
	}

	pub fn as_str(&self) -> &'raw str {
		match self {
			Token::Bold => "**",
			Token::Space => " ",
			Token::Code => "`",
			Token::Escape => "\\",
			Token::Percent => "*",
			Token::Pipe => "|",
			Token::LinkClose => "]",
			Token::LinkOpen => "[",
			Token::InterlinkOpen => "[[",
			Token::InterlinkClose => "]]",
			Token::Italic => "*",
			Token::Text(str) => str,
		}
	}
}

impl<'raw> fmt::Display for Token<'raw> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}", self.as_str())
	}
}