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