readable-regex 0.1.0-alpha1

Regex made for humans. Wrapper to build regexes in a verbose style.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
use crate::ReadableRe;
use std::fmt::{Display, Formatter};
use std::ops::{Bound, RangeBounds};

macro_rules! impl_builder_from_iter {
    ($struct_name:ident) => {
        impl<'a> FromIterator<ReadableRe<'a>> for $struct_name<'a> {
            fn from_iter<T: IntoIterator<Item = ReadableRe<'a>>>(iter: T) -> Self {
                Self(Box::new(ReadableRe::Concat(Concat::new(
                    iter.into_iter().collect::<Vec<_>>(),
                ))))
            }
        }
    };
}

/// Just a [`ReadableRe`] concatenation wrapper
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::Concat;
/// use readable_regex::ReadableRe::Raw;
/// assert_eq!(&Concat::from_iter([Raw("foo"), Raw("bar")]).to_string(), "foobar");
/// ```
#[derive(Clone)]
pub struct Concat<'a>(pub(crate) Vec<ReadableRe<'a>>);

impl<'a> Concat<'a> {
    pub fn new(v: impl IntoIterator<Item = ReadableRe<'a>>) -> Self {
        Self::from_iter(v)
    }
}

impl<'a> FromIterator<ReadableRe<'a>> for Concat<'a> {
    fn from_iter<T: IntoIterator<Item = ReadableRe<'a>>>(iter: T) -> Self {
        Self(iter.into_iter().collect())
    }
}

impl<'a> Display for Concat<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for re in &self.0 {
            write!(f, "{}", re)?;
        }
        Ok(())
    }
}

#[cfg(feature = "re-fancy")]
/// Returns a string in the regex syntax for a back reference, such as \1, \2, etc.
/// ## Example
/// ```
/// use readable_regex::solvers::BackReference;
/// let back_3 = BackReference(3);
/// assert_eq!(back_3.to_string(), "\\3");
/// ```
#[derive(Clone)]
pub struct BackReference(pub usize);

#[cfg(feature = "re-fancy")]
impl Display for BackReference {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, r"\{}", self.0)
    }
}

/// A wrapper for [`regex::escape`]. Escape special characters in the input str
/// ## Example
/// ```
/// use readable_regex::solvers::Escape;
/// use readable_regex::ReadableRe;
/// let scaped = Escape::new_str("!#$%&");
/// assert_eq!(scaped.to_string(), "!\\#\\$%\\&");
/// ```
#[derive(Clone)]
pub struct Escape<'a>(Box<ReadableRe<'a>>);

impl Display for Escape<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", regex::escape(&self.0.to_string()))
    }
}

impl<'a> Escape<'a> {
    pub fn new_str(s: &'a str) -> Self {
        Self(Box::new(ReadableRe::Raw(s)))
    }

    pub fn new(readable_regex: ReadableRe<'a>) -> Self {
        Self(Box::new(readable_regex))
    }
}

impl_builder_from_iter!(Escape);

/// Regex syntax for a regex group surrounded by parentheses of the regex input str
/// ### Example
/// ```
/// use readable_regex::solvers::{Concat, Group};
/// use readable_regex::ReadableRe::{self, Raw};
/// assert_eq!(Group::new(Raw("cat")).to_string(), "(cat)");
/// assert_eq!(Group::new(ReadableRe::Concat(Concat::new([Raw("cat"), Raw("dog"), Raw("moose")]))).to_string(), "(catdogmoose)");
/// assert_eq!(
///     Group::new(ReadableRe::Concat(Concat::new([
///         Raw("cat"),
///         ReadableRe::Group(Group::new(Raw("dog"))),
///         ReadableRe::Group(Group::new(Raw("moose"))),
///     ]))).to_string(),
///     "(cat(dog)(moose))",
/// );
/// ```
#[derive(Clone)]
pub struct Group<'a>(Box<ReadableRe<'a>>);

impl<'a> Group<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for Group<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "({})", self.0)
    }
}

impl_builder_from_iter!(Group);

#[cfg(feature = "re-fancy")]
/// Regex syntax for a positive lookahead assertion of the regex strings
/// A lookahead matches text but does not consume it in the original parsed text.
///
/// ## Example
///
/// In the following example, 'kitty' is matched but only if 'cat' follows
/// 'kitty'. Note that the match only includes 'kitty' and not 'kittycat'.
///
/// ```
/// use readable_regex::solvers::{Concat, PositiveLookAhead};
/// use readable_regex::ReadableRe;
/// use std::fmt::Display;
/// let query = Concat::new([
///     ReadableRe::Raw("kitty"),
///     ReadableRe::PositiveLookAhead(PositiveLookAhead::new(ReadableRe::Raw("cat"))),
///  ]);
/// assert_eq!(
///     query.to_string(),
///     "kitty(?=cat)"
/// );
///
/// assert!(!fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("kitty").unwrap());
/// assert!(fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("kittycat").unwrap());
/// ```
#[derive(Clone)]
pub struct PositiveLookAhead<'a>(Box<ReadableRe<'a>>);

#[cfg(feature = "re-fancy")]
impl<'a> PositiveLookAhead<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

#[cfg(feature = "re-fancy")]
impl<'a> Display for PositiveLookAhead<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(?={})", self.0)
    }
}

#[cfg(feature = "re-fancy")]
impl_builder_from_iter!(PositiveLookAhead);

#[cfg(feature = "re-fancy")]
/// Regex syntax for a negative lookahead assertion of the regex strings
/// A lookahead matches text but does not consume it in the original parsed text.
///
/// ## Example
///
/// In the following example, 'kitty' is matched but only if the 'cat'
/// does not follow 'kitty'. Note that the match only includes 'kitty' and not 'kittycat'
///
/// ```
/// use readable_regex::solvers::{Concat, NegativeLookAhead};
/// use readable_regex::ReadableRe;
/// use std::fmt::Display;
/// let query = Concat::new([
///     ReadableRe::Raw("kitty"),
///     ReadableRe::NegativeLookAhead(NegativeLookAhead::new(ReadableRe::Raw("cat"))),
///  ]);
/// assert_eq!(
///     query.to_string(),
///     "kitty(?!cat)"
/// );
///
/// assert!(fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("kitty").unwrap());
/// assert!(!fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("kittycat").unwrap());
/// ```
#[derive(Clone)]
pub struct NegativeLookAhead<'a>(Box<ReadableRe<'a>>);

#[cfg(feature = "re-fancy")]
impl<'a> NegativeLookAhead<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

#[cfg(feature = "re-fancy")]
impl<'a> Display for NegativeLookAhead<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(?!{})", self.0)
    }
}

#[cfg(feature = "re-fancy")]
impl_builder_from_iter!(NegativeLookAhead);

#[cfg(feature = "re-fancy")]
/// Regex syntax for a positive lookbehind assertion of the input regexes.
/// A lookbehind matches text but does not consume it in the original parsed text
/// ## Example
///
/// In the following example, 'cat' is matched but only if 'kitty' is before 'cat'.
/// Note that the match only includes 'cat' and not 'kittycat'.
///
/// ```
/// use readable_regex::solvers::{Concat, PositiveLookBehind};
/// use readable_regex::ReadableRe;
/// use std::fmt::Display;
/// let query = Concat::new([
///     ReadableRe::PositiveLookBehind(PositiveLookBehind::new(ReadableRe::Raw("kitty"))),
///     ReadableRe::Raw("cat")
///  ]);
/// assert_eq!(
///     query.to_string(),
///     "(?<=kitty)cat"
/// );
/// assert!(fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("kittycat").unwrap());
/// assert!(!fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("cat").unwrap());
/// ```
#[derive(Clone)]
pub struct PositiveLookBehind<'a>(Box<ReadableRe<'a>>);

#[cfg(feature = "re-fancy")]
impl<'a> PositiveLookBehind<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

#[cfg(feature = "re-fancy")]
impl<'a> Display for PositiveLookBehind<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(?<={})", self.0)
    }
}

#[cfg(feature = "re-fancy")]
impl_builder_from_iter!(PositiveLookBehind);

#[cfg(feature = "re-fancy")]
/// Negative lookbehind assertion of the input regex.
/// A lookbehind matches text but does not consume it in the original parsed text
///
/// ## Example
///
/// In the following example, 'cat' is matched but only if 'kitty' is not
/// before 'cat'. Note that the match only includes 'cat' and not 'kittycat'.
///
/// ```
/// use readable_regex::solvers::{Concat, NegativeLookBehind};
/// use readable_regex::ReadableRe;
/// use std::fmt::Display;
/// let query = Concat::from_iter([
///     ReadableRe::NegativeLookBehind(NegativeLookBehind::new(ReadableRe::Raw("kitty"))),
///     ReadableRe::Raw("cat")
///  ]);
/// assert_eq!(
///     query.to_string(),
///     "(?<!kitty)cat"
/// );
/// assert!(!fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("kittycat").unwrap());
/// assert!(fancy_regex::Regex::new(&query.to_string()).unwrap().is_match("black cat").unwrap());
/// ```
#[derive(Clone)]
pub struct NegativeLookBehind<'a>(Box<ReadableRe<'a>>);

#[cfg(feature = "re-fancy")]
impl<'a> NegativeLookBehind<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

#[cfg(feature = "re-fancy")]
impl<'a> Display for NegativeLookBehind<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(?<!{})", self.0)
    }
}

#[cfg(feature = "re-fancy")]
impl_builder_from_iter!(NegativeLookBehind);

/// Regex syntax for a named group of the input regex.
/// Named groups can be referred to by their name rather than their group number.
///
/// ## Examples
///
/// ```
/// use readable_regex::solvers::NamedGroup;
/// use readable_regex::ReadableRe::Raw;
/// use std::fmt::Display;
/// assert_eq!(
///     &NamedGroup::new("group_name", Raw(r"pattern_to_look_for")).to_string(),
///     "(?P<group_name>pattern_to_look_for)"
/// );
/// assert_eq!(
///     &NamedGroup::new("pobox", Raw(r"PO BOX \d{3:5}")).to_string(),
///     "(?P<pobox>PO BOX \\d{3:5})"
/// );
/// ```
#[derive(Clone)]
pub struct NamedGroup<'a> {
    name: &'a str,
    regexes: Box<ReadableRe<'a>>,
}

impl<'a> NamedGroup<'a> {
    pub fn new(name: &'a str, re: ReadableRe<'a>) -> Self {
        Self {
            name,
            regexes: Box::new(re),
        }
    }
}

impl<'a> Display for NamedGroup<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(?P<{}>{})", self.name, self.regexes)
    }
}

/// Regex syntax for a non-capturing group of the regex input
///
/// Non-capturing groups are not included in the groups field of a Pattern object.
/// They are useful for when you want to group parts of a regex string together without
/// affecting the numbered groups.
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::{Concat, NonCaptureGroup};
/// use readable_regex::ReadableRe;
/// use std::fmt::Display;
/// let query = ReadableRe::NonCaptureGroup(
///     NonCaptureGroup::new(ReadableRe::Raw("pattern_to_look_for"))
/// );
/// assert_eq!(
///     query.to_string(),
///     "(?:pattern_to_look_for)"
/// );
/// ```
#[derive(Clone)]
pub struct NonCaptureGroup<'a>(Box<ReadableRe<'a>>);

impl<'a> NonCaptureGroup<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for NonCaptureGroup<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(?:{})", self.0)
    }
}

impl_builder_from_iter!(NonCaptureGroup);

/// Regex syntax for an optional part of the pattern of the regex strings in tuple_of_regex_strs
///
/// ## Example
/// ```
/// use readable_regex::solvers::Optional;
/// use readable_regex::ReadableRe::{self, Raw};
/// let query = ReadableRe::Optional(Optional::new(Raw("foo")));
/// assert_eq!(
///     query.to_string(),
///     "foo?"
/// );
/// ```
#[derive(Clone)]
pub struct Optional<'a>(Box<ReadableRe<'a>>);

impl<'a> Optional<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for Optional<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}?", self.0)
    }
}

impl_builder_from_iter!(Optional);

/// Regex syntax for the alternation or "or" operator of the patterns in iterator input,
/// and the alternation is placed in a group
///
/// ## Examples
///
/// ```
/// use readable_regex::solvers::Either;
/// use readable_regex::ReadableRe::Raw;
/// assert_eq!(Either::new([Raw("a"), Raw("b"), Raw("c")]).to_string(), "a|b|c")
/// ```
#[derive(Clone)]
pub struct Either<'a>(Concat<'a>);

impl<'a> Either<'a> {
    pub fn new(iter: impl IntoIterator<Item = ReadableRe<'a>>) -> Self {
        Self::from_iter(iter)
    }
}

impl<'a> Display for Either<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<'a> FromIterator<ReadableRe<'a>> for Either<'a> {
    fn from_iter<T: IntoIterator<Item = ReadableRe<'a>>>(iter: T) -> Self {
        let iter = iter
            .into_iter()
            .flat_map(|re| [re, ReadableRe::Raw("|")].into_iter());
        // TODO: substitute with `interleave` when available
        let mut concat = Concat::from_iter(iter);
        concat.0.pop();
        Self(concat)
    }
}

/// Regex syntax for matching an exact number of occurrences of the input regex
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::Exactly;
/// use readable_regex::ReadableRe::Raw;
/// let query = Exactly::new(3, Raw("A"));
/// assert_eq!(query.to_string(), "A{3}")
/// ```
#[derive(Clone)]
pub struct Exactly<'a> {
    quantity: usize,
    re: Box<ReadableRe<'a>>,
}

impl<'a> Exactly<'a> {
    pub fn new(quantity: usize, re: ReadableRe<'a>) -> Self {
        Self {
            quantity,
            re: Box::new(re),
        }
    }
}

impl<'a> Display for Exactly<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}{{{}}}", self.re, self.quantity)
    }
}

/// Regex syntax for matching an between the minimum and maximum number of occurrences of the input regex
/// It accepts unbounded ranges. It also do not discriminate between open/closed ranges, and both
/// works as including both ends.
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::Ranged;
/// use readable_regex::ReadableRe::Raw;
/// let query = Ranged::new(3..5, Raw("abc"));
/// assert_eq!(query.to_string(), "abc{3,5}");
/// let query = Ranged::new(..5, Raw("abc"));
/// assert_eq!(query.to_string(), "abc{,5}");
/// let query = Ranged::new(3.., Raw("abc"));
/// assert_eq!(query.to_string(), "abc{3,}");
/// let query = Ranged::new(.., Raw("abc"));
/// assert_eq!(query.to_string(), "abc{,}");
/// ```
pub struct Ranged<'a> {
    range: (Bound<usize>, Bound<usize>),
    re: Box<ReadableRe<'a>>,
}

impl<'a> Clone for Ranged<'a> {
    fn clone(&self) -> Self {
        Self {
            range: self.range,
            re: self.re.clone(),
        }
    }
}

impl<'a> Ranged<'a> {
    pub fn new<R>(range: R, re: ReadableRe<'a>) -> Self
    where
        R: RangeBounds<usize> + 'static,
    {
        Self {
            range: (range.start_bound().cloned(), range.end_bound().cloned()),
            re: Box::new(re),
        }
    }
}

impl<'a> Display for Ranged<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let (min, max) = match self.range {
            (
                Bound::Included(min) | Bound::Excluded(min),
                Bound::Included(max) | Bound::Excluded(max),
            ) => (min.to_string(), max.to_string()),
            (Bound::Included(min) | Bound::Excluded(min), Bound::Unbounded) => {
                (min.to_string(), "".to_string())
            }
            (Bound::Unbounded, Bound::Included(max) | Bound::Excluded(max)) => {
                ("".to_string(), max.to_string())
            }
            (Bound::Unbounded, Bound::Unbounded) => ("".to_string(), "".to_string()),
        };
        write!(f, "{}{{{},{}}}", self.re, min, max)
    }
}

/// Regex syntax for matching zero or more occurrences of the input regex.
/// This does a greedy match, which tries to make the largest match possible.
///
/// ## Example
///
/// ```
/// use readable_regex::ReadableRe::Raw;
/// use readable_regex::solvers::{ZeroOrMore};
/// let query = ZeroOrMore::new(Raw("abc"));
/// assert_eq!(query.to_string(), "abc*")
/// ```
#[derive(Clone)]
pub struct ZeroOrMore<'a>(Box<ReadableRe<'a>>);

impl<'a> ZeroOrMore<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for ZeroOrMore<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}*", self.0)
    }
}

impl_builder_from_iter!(ZeroOrMore);

/// Regex syntax for matching zero or more occurrences of the input regex.
/// This does a lazy match, which tries to make the smallest match possible.
///
/// ## Example
///
/// ```
/// use readable_regex::ReadableRe::Raw;
/// use readable_regex::solvers::{ZeroOrMoreLazy};
/// let query = ZeroOrMoreLazy::new(Raw("abc"));
/// assert_eq!(query.to_string(), "abc*?")
/// ```
#[derive(Clone)]
pub struct ZeroOrMoreLazy<'a>(Box<ReadableRe<'a>>);

impl<'a> ZeroOrMoreLazy<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for ZeroOrMoreLazy<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}*?", self.0)
    }
}

impl_builder_from_iter!(ZeroOrMoreLazy);

/// Regex syntax for matching one or more occurrences of the input regex.
/// This does a greedy match, which tries to make the largest match possible.
///
/// ## Example
///
/// ```
/// use readable_regex::ReadableRe::Raw;
/// use readable_regex::solvers::{OneOrMore};
/// let query = OneOrMore::new(Raw("abc"));
/// assert_eq!(query.to_string(), "abc+")
/// ```
#[derive(Clone)]
pub struct OneOrMore<'a>(Box<ReadableRe<'a>>);

impl<'a> OneOrMore<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for OneOrMore<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}+", self.0)
    }
}

impl_builder_from_iter!(OneOrMore);

/// Regex syntax for matching one or more occurrences of the input regex.
/// This does a lazy match, which tries to make the smallest match possible.
///
/// ## Example
///
/// ```
/// use readable_regex::ReadableRe::Raw;
/// use readable_regex::solvers::{OneOrMoreLazy};
/// let query = OneOrMoreLazy::new(Raw("abc"));
/// assert_eq!(query.to_string(), "abc+?")
/// ```
#[derive(Clone)]
pub struct OneOrMoreLazy<'a>(Box<ReadableRe<'a>>);

impl<'a> OneOrMoreLazy<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for OneOrMoreLazy<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}+?", self.0)
    }
}

impl_builder_from_iter!(OneOrMoreLazy);

/// Regex syntax for matching the pattern of the input regex at the start of the searched text.
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::StartsWith;
/// use readable_regex::ReadableRe::Raw;
/// let query = StartsWith::new(Raw("abc"));
/// assert_eq!(query.to_string(), "^abc");
/// ```
#[derive(Clone)]
pub struct StartsWith<'a>(Box<ReadableRe<'a>>);

impl<'a> StartsWith<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for StartsWith<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "^{}", self.0)
    }
}

impl_builder_from_iter!(StartsWith);

/// Regex syntax for matching the pattern of the input regex at the end of the searched text.
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::EndsWith;
/// use readable_regex::ReadableRe::Raw;
/// let query = EndsWith::new(Raw("abc"));
/// assert_eq!(query.to_string(), "abc$");
/// ```
#[derive(Clone)]
pub struct EndsWith<'a>(Box<ReadableRe<'a>>);

impl<'a> EndsWith<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

impl<'a> Display for EndsWith<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}$", self.0)
    }
}

impl_builder_from_iter!(EndsWith);

/// Regex syntax for matching the pattern of the input regex at the start and end of the searched text.
/// (That is, the pattern must match the complete searched text.)
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::StartsAndEndsWith;
/// use readable_regex::ReadableRe::Raw;
/// let query =  StartsAndEndsWith::new(Raw("abc"));
/// assert_eq!(query.to_string(), "^abc$");
/// ```
#[derive(Clone)]
pub struct StartsAndEndsWith<'a>(Box<ReadableRe<'a>>);

impl<'a> StartsAndEndsWith<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(ReadableRe::StartsWith(StartsWith::new(
            ReadableRe::EndsWith(EndsWith::new(re)),
        ))))
    }
}

impl<'a> Display for StartsAndEndsWith<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl_builder_from_iter!(StartsAndEndsWith);

/// Regex syntax for a character class including the characters in the input [`IntoIterator`]
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::Chars;
/// let query = Chars::new("abc");
/// assert_eq!(query.to_string(), "[abc]");
/// ```
#[derive(Clone)]
pub struct Chars(String);

impl Chars {
    pub fn new(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl Display for Chars {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}]", self.0)
    }
}

/// Regex syntax for a character class excluding the characters in the input [`IntoIterator`]
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::NotChars;
/// let query = NotChars::new("abc");
/// assert_eq!(query.to_string(), "[^abc]");
/// ```
#[derive(Clone)]
pub struct NotChars(String);

impl NotChars {
    pub fn new(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl Display for NotChars {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "[^{}]", self.0)
    }
}

#[cfg(feature = "re-fancy")]
/// Regex syntax for an atomic group of the input regex
///
/// ## Example
///
/// ```
/// use readable_regex::solvers::AtomicGroup;
/// use readable_regex::ReadableRe::Raw;
/// let query = AtomicGroup::new(Raw("foo"));
/// assert_eq!(query.to_string(), "(?>foo)")
/// ```
#[derive(Clone)]
pub struct AtomicGroup<'a>(Box<ReadableRe<'a>>);

#[cfg(feature = "re-fancy")]
impl<'a> AtomicGroup<'a> {
    pub fn new(re: ReadableRe<'a>) -> Self {
        Self(Box::new(re))
    }
}

#[cfg(feature = "re-fancy")]
impl<'a> Display for AtomicGroup<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(?>{})", self.0)
    }
}

#[cfg(feature = "re-fancy")]
impl_builder_from_iter!(AtomicGroup);