Skip to main content

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    Acute,
83    /// `\``,
84    Backtick,
85}
86
87impl Punctuation {
88    pub fn from_char(c: char) -> Option<Punctuation> {
89        let punct = match c {
90            '@' => Punctuation::At,
91            '~' => Punctuation::Tilde,
92            '°' => Punctuation::Degree,
93            '=' => Punctuation::Equal,
94            '<' => Punctuation::LessThan,
95            '>' => Punctuation::GreaterThan,
96            '/' => Punctuation::ForwardSlash,
97            '\\' => Punctuation::Backslash,
98            '%' => Punctuation::Percent,
99            '’' => Punctuation::Apostrophe,
100            '\'' => Punctuation::Apostrophe,
101            '.' => Punctuation::Period,
102            '!' => Punctuation::Bang,
103            '?' => Punctuation::Question,
104            ':' => Punctuation::Colon,
105            ';' => Punctuation::Semicolon,
106            ',' => Punctuation::Comma,
107            '、' => Punctuation::Comma,
108            ',' => Punctuation::Comma,
109            '-' => Punctuation::Hyphen,
110            '[' => Punctuation::OpenSquare,
111            ']' => Punctuation::CloseSquare,
112            '{' => Punctuation::OpenCurly,
113            '}' => Punctuation::CloseCurly,
114            '(' => Punctuation::OpenRound,
115            ')' => Punctuation::CloseRound,
116            '#' => Punctuation::Hash,
117            '*' => Punctuation::Star,
118            '&' => Punctuation::Ampersand,
119            '–' => Punctuation::EnDash,
120            '—' => Punctuation::EmDash,
121            '…' => Punctuation::Ellipsis,
122            '^' => Punctuation::Caret,
123            '+' => Punctuation::Plus,
124            '|' => Punctuation::Pipe,
125            '_' => Punctuation::Underscore,
126            '´' => Punctuation::Acute,
127            '`' => Punctuation::Backtick,
128            _ => Punctuation::Currency(Currency::from_char(c)?),
129        };
130
131        Some(punct)
132    }
133}
134
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Hash)]
136pub struct Quote {
137    /// The location of the matching quote, if it exists.
138    pub twin_loc: Option<usize>,
139}