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