harper_core/
punctuation.rs

1use is_macro::Is;
2use serde::{Deserialize, Serialize};
3
4use crate::Currency;
5
6#[derive(
7    Debug, Is, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Default, Hash,
8)]
9#[serde(tag = "kind")]
10pub enum Punctuation {
11    /// `°`
12    Degree,
13    /// `…`
14    Ellipsis,
15    /// `–`
16    EnDash,
17    /// `—`
18    EmDash,
19    /// `&`
20    Ampersand,
21    /// `.`
22    #[default]
23    Period,
24    /// `!`
25    Bang,
26    /// `?`
27    Question,
28    /// `:`
29    Colon,
30    /// ``;``
31    Semicolon,
32    /// `"`
33    Quote(Quote),
34    /// `,`
35    Comma,
36    /// `-`
37    Hyphen,
38    /// `[`
39    OpenSquare,
40    /// `]`
41    CloseSquare,
42    /// `(`
43    OpenRound,
44    /// `)`
45    CloseRound,
46    /// `{`
47    OpenCurly,
48    /// `}`
49    CloseCurly,
50    /// `"`
51    Hash,
52    /// `'`
53    Apostrophe,
54    /// `%`
55    Percent,
56    /// `/`
57    ForwardSlash,
58    /// `\`
59    Backslash,
60    /// `<`
61    LessThan,
62    /// `>`
63    GreaterThan,
64    /// `=`
65    Equal,
66    /// `*`
67    Star,
68    /// `~`
69    Tilde,
70    /// `@`
71    At,
72    /// `^`
73    Caret,
74    /// `+`
75    Plus,
76    Currency(Currency),
77    /// `|`
78    Pipe,
79    /// `_`
80    Underscore,
81}
82
83impl Punctuation {
84    pub fn from_char(c: char) -> Option<Punctuation> {
85        let punct = match c {
86            '@' => Punctuation::At,
87            '~' => Punctuation::Tilde,
88            '°' => Punctuation::Degree,
89            '=' => Punctuation::Equal,
90            '<' => Punctuation::LessThan,
91            '>' => Punctuation::GreaterThan,
92            '/' => Punctuation::ForwardSlash,
93            '\\' => Punctuation::Backslash,
94            '%' => Punctuation::Percent,
95            '’' => Punctuation::Apostrophe,
96            '\'' => Punctuation::Apostrophe,
97            '.' => Punctuation::Period,
98            '!' => Punctuation::Bang,
99            '?' => Punctuation::Question,
100            ':' => Punctuation::Colon,
101            ';' => Punctuation::Semicolon,
102            ',' => Punctuation::Comma,
103            '、' => Punctuation::Comma,
104            ',' => Punctuation::Comma,
105            '-' => Punctuation::Hyphen,
106            '[' => Punctuation::OpenSquare,
107            ']' => Punctuation::CloseSquare,
108            '{' => Punctuation::OpenCurly,
109            '}' => Punctuation::CloseCurly,
110            '(' => Punctuation::OpenRound,
111            ')' => Punctuation::CloseRound,
112            '#' => Punctuation::Hash,
113            '*' => Punctuation::Star,
114            '&' => Punctuation::Ampersand,
115            '–' => Punctuation::EnDash,
116            '—' => Punctuation::EmDash,
117            '…' => Punctuation::Ellipsis,
118            '^' => Punctuation::Caret,
119            '+' => Punctuation::Plus,
120            '|' => Punctuation::Pipe,
121            '_' => Punctuation::Underscore,
122            _ => Punctuation::Currency(Currency::from_char(c)?),
123        };
124
125        Some(punct)
126    }
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Hash)]
130pub struct Quote {
131    /// The location of the matching quote, if it exists.
132    pub twin_loc: Option<usize>,
133}