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