3
3
4
4
/**
5
5
* Regex for a custom four-byte string.
6
- *
6
+ *
7
7
* This is a result of an attempt to create a formatter
8
8
* which translates normal, human readable thai regex
9
9
* into 4-bytes zero-left-pad bytes regex pattern string
@@ -15,6 +15,7 @@ use regex_syntax::{
15
15
is_meta_character, Parser ,
16
16
} ;
17
17
use std:: { error:: Error , fmt:: Display } ;
18
+
18
19
trait ToCustomStringRepr {
19
20
fn to_custom_byte_repr ( & self ) -> Result < String > ;
20
21
}
@@ -28,6 +29,7 @@ enum UnsupportedCustomRegexParserError {
28
29
AnchorStartLine ,
29
30
AnchorEndLine ,
30
31
}
32
+
31
33
enum IterableHirKind {
32
34
Alternation ( Vec < Hir > ) ,
33
35
Concat ( Vec < Hir > ) ,
@@ -56,13 +58,15 @@ impl Display for UnsupportedCustomRegexParserError {
56
58
}
57
59
}
58
60
}
61
+
59
62
impl Error for UnsupportedCustomRegexParserError { }
60
63
61
64
impl ToCustomStringRepr for Hir {
62
65
fn to_custom_byte_repr ( & self ) -> Result < String > {
63
66
self . kind ( ) . to_custom_byte_repr ( )
64
67
}
65
68
}
69
+
66
70
impl ToCustomStringRepr for HirKind {
67
71
fn to_custom_byte_repr ( & self ) -> Result < String > {
68
72
match self {
@@ -80,6 +84,7 @@ impl ToCustomStringRepr for HirKind {
80
84
}
81
85
}
82
86
}
87
+
83
88
impl ToCustomStringRepr for Anchor {
84
89
fn to_custom_byte_repr ( & self ) -> Result < String > {
85
90
match self {
@@ -90,16 +95,18 @@ impl ToCustomStringRepr for Anchor {
90
95
}
91
96
}
92
97
}
98
+
93
99
impl ToCustomStringRepr for LiteralEnum {
94
100
fn to_custom_byte_repr ( & self ) -> Result < String > {
95
101
match self {
96
102
LiteralEnum :: Unicode ( a) => Ok ( a. to_four_byte_string ( ) ) ,
97
- LiteralEnum :: Byte ( b ) => Err ( AnyError :: new (
103
+ LiteralEnum :: Byte ( _b ) => Err ( AnyError :: new (
98
104
UnsupportedCustomRegexParserError :: ByteLiteral ,
99
105
) ) ,
100
106
}
101
107
}
102
108
}
109
+
103
110
impl ToCustomStringRepr for Class {
104
111
fn to_custom_byte_repr ( & self ) -> Result < String > {
105
112
match self {
@@ -108,6 +115,7 @@ impl ToCustomStringRepr for Class {
108
115
}
109
116
}
110
117
}
118
+
111
119
impl ToCustomStringRepr for Repetition {
112
120
fn to_custom_byte_repr ( & self ) -> Result < String > {
113
121
let symbol: Result < String > = match & self . kind {
@@ -143,6 +151,7 @@ impl ToCustomStringRepr for Repetition {
143
151
}
144
152
}
145
153
}
154
+
146
155
impl ToCustomStringRepr for IterableHirKind {
147
156
fn to_custom_byte_repr ( & self ) -> Result < String > {
148
157
match self {
@@ -251,6 +260,7 @@ impl ToCustomStringRepr for IterableHirKind {
251
260
}
252
261
}
253
262
}
263
+
254
264
impl ToCustomStringRepr for Group {
255
265
fn to_custom_byte_repr ( & self ) -> Result < String > {
256
266
let recur = match self . hir . kind ( ) {
@@ -288,6 +298,7 @@ enum UTFBytesLength {
288
298
Three ,
289
299
Four ,
290
300
}
301
+
291
302
fn char_class ( character : char ) -> UTFBytesLength {
292
303
let mut bytes_buffer: [ u8 ; 4 ] = [ 0 ; 4 ] ;
293
304
@@ -307,6 +318,7 @@ fn is_in_range<T: PartialEq + PartialOrd>(value: T, range: (T, T)) -> bool {
307
318
trait PadLeftZeroFourBytesRep {
308
319
fn to_four_byte_string ( & self ) -> String ;
309
320
}
321
+
310
322
fn escape_meta_character ( c : char ) -> String {
311
323
if is_meta_character ( c) {
312
324
format ! ( r"\{}" , c)
@@ -316,6 +328,7 @@ fn escape_meta_character(c: char) -> String {
316
328
c. to_string ( )
317
329
}
318
330
}
331
+
319
332
impl PadLeftZeroFourBytesRep for & [ ClassUnicodeRange ] {
320
333
fn to_four_byte_string ( & self ) -> String {
321
334
let urange = self ;
@@ -363,6 +376,7 @@ impl PadLeftZeroFourBytesRep for &[ClassUnicodeRange] {
363
376
}
364
377
}
365
378
}
379
+
366
380
impl PadLeftZeroFourBytesRep for char {
367
381
fn to_four_byte_string ( & self ) -> String {
368
382
let character = self ;
0 commit comments