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
//! [str] related combinators
use crate::{
    take_atom, take_while, Incomplete, InvalidRepetition, LenBytes, Pipe, Repetition,
    Tag, TakeAtom,
};
use std::{error::Error as StdError, str::CharIndices};
use tuplify::PushBack;
use unicode_segmentation::{
    GraphemeIndices, USentenceBoundIndices, UWordBoundIndices, UnicodeSegmentation,
};

impl LenBytes for char {
    fn len_bytes(&self) -> usize { self.len_utf8() }
}

impl LenBytes for &str {
    fn len_bytes(&self) -> usize { self.len() }
}

/// Splits an [str] in chars
pub struct CharAtom<'a>(&'a str, CharIndices<'a>);

impl<'a> From<&'a str> for CharAtom<'a> {
    fn from(value: &'a str) -> Self { CharAtom(value, value.char_indices()) }
}

impl<'a> TakeAtom for CharAtom<'a> {
    type Atom = char;
    type Container = &'a str;

    fn next(&mut self) -> Option<(usize, Self::Atom)> { self.1.next() }

    fn split_at(self, index: usize) -> (Self::Container, Self::Container) {
        (&self.0[index..], &self.0[..index])
    }
}

#[cfg(feature = "unicode")]
/// Splits an [str] in graphemes see: [UnicodeSegmentation::graphemes]
pub struct GraphemeAtom<'a>(&'a str, GraphemeIndices<'a>);

#[cfg(feature = "unicode")]
impl<'a> From<&'a str> for GraphemeAtom<'a> {
    fn from(value: &'a str) -> Self { GraphemeAtom(value, value.grapheme_indices(true)) }
}

#[cfg(feature = "unicode")]
impl<'a> TakeAtom for GraphemeAtom<'a> {
    type Atom = &'a str;
    type Container = &'a str;

    fn next(&mut self) -> Option<(usize, Self::Atom)> { self.1.next() }

    fn split_at(self, index: usize) -> (Self::Container, Self::Container) {
        (&self.0[index..], &self.0[..index])
    }
}

#[cfg(feature = "unicode")]
/// Splits an [str] in words see: [UnicodeSegmentation::unicode_words]
pub struct WordAtom<'a>(&'a str, UWordBoundIndices<'a>);

#[cfg(feature = "unicode")]
impl<'a> From<&'a str> for WordAtom<'a> {
    fn from(value: &'a str) -> Self { WordAtom(value, value.split_word_bound_indices()) }
}

#[cfg(feature = "unicode")]
impl<'a> TakeAtom for WordAtom<'a> {
    type Atom = &'a str;
    type Container = &'a str;

    fn next(&mut self) -> Option<(usize, Self::Atom)> { self.1.next() }

    fn split_at(self, index: usize) -> (Self::Container, Self::Container) {
        (&self.0[index..], &self.0[..index])
    }
}

#[cfg(feature = "unicode")]
/// Splits an [str] in sentences see: [UnicodeSegmentation::unicode_sentences]
pub struct SentenceAtom<'a>(&'a str, USentenceBoundIndices<'a>);

#[cfg(feature = "unicode")]
impl<'a> From<&'a str> for SentenceAtom<'a> {
    fn from(value: &'a str) -> Self {
        SentenceAtom(value, value.split_sentence_bound_indices())
    }
}

#[cfg(feature = "unicode")]
impl<'a> TakeAtom for SentenceAtom<'a> {
    type Atom = &'a str;
    type Container = &'a str;

    fn next(&mut self) -> Option<(usize, Self::Atom)> { self.1.next() }

    fn split_at(self, index: usize) -> (Self::Container, Self::Container) {
        (&self.0[index..], &self.0[..index])
    }
}

/// Takes the given quantity of ascii whitespaces
pub fn whitespaces<'a, E>(
    qty: impl TryInto<Repetition, Error = impl Into<InvalidRepetition>>,
) -> impl Pipe<&'a str, (&'a str,), E>
where
    Incomplete: Into<E>,
    E: StdError,
{
    let qty = qty.try_into().map_err(Into::into).unwrap();
    move |i: &'a str| {
        take_while(|x: char| x.is_ascii_whitespace(), qty).apply(CharAtom::from(i))
    }
}

/// Takes the given quantity of ascii digits
pub fn digits<'a, E>(
    qty: impl TryInto<Repetition, Error = impl Into<InvalidRepetition>>,
) -> impl Pipe<&'a str, (&'a str,), E>
where
    Incomplete: Into<E>,
    E: StdError,
{
    let qty = qty.try_into().map_err(Into::into).unwrap();
    move |i: &'a str| {
        take_while(|x: char| x.is_ascii_digit(), qty).apply(CharAtom::from(i))
    }
}

/// Takes the given quantity of ascii hexadecimal digits
pub fn hex_digits<'a, E>(
    qty: impl TryInto<Repetition, Error = impl Into<InvalidRepetition>>,
) -> impl Pipe<&'a str, (&'a str,), E>
where
    Incomplete: Into<E>,
    E: StdError,
{
    let qty = qty.try_into().map_err(Into::into).unwrap();
    move |i: &'a str| {
        take_while(|x: char| x.is_ascii_hexdigit(), qty).apply(CharAtom::from(i))
    }
}

/// Takes the given quantity of ascii octal digits
pub fn oct_digits<'a, E>(
    qty: impl TryInto<Repetition, Error = impl Into<InvalidRepetition>>,
) -> impl Pipe<&'a str, (&'a str,), E>
where
    Incomplete: Into<E>,
    E: StdError,
{
    let qty = qty.try_into().map_err(Into::into).unwrap();
    move |i: &'a str| {
        take_while(|x: char| matches!(x, '0'..='7'), qty).apply(CharAtom::from(i))
    }
}

/// Takes the given quantity of ascii binary digits
pub fn bin_digits<'a, E>(
    qty: impl TryInto<Repetition, Error = impl Into<InvalidRepetition>>,
) -> impl Pipe<&'a str, (&'a str,), E>
where
    Incomplete: Into<E>,
    E: StdError,
{
    let qty = qty.try_into().map_err(Into::into).unwrap();
    move |i: &'a str| {
        take_while(|x: char| matches!(x, '0'..='1'), qty).apply(CharAtom::from(i))
    }
}

/// str tag error containing the expected and found string
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TagStrError(pub String, pub String);

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

impl std::error::Error for TagStrError {}

impl<'a, 'b, E> Tag<&'a str, E> for &'b str
where
    E: StdError,
    Incomplete: Into<E>,
    TagStrError: Into<E>,
{
    type Output = &'a str;

    fn strip_from(&self, input: &'a str) -> Result<(&'a str, (Self::Output,)), E> {
        if let Some(x) = input.strip_prefix(self) {
            Ok((x, (&input[..self.len()],)))
        } else {
            Err(if self.starts_with(input) {
                Incomplete::Size(self.len() - input.len()).into()
            } else {
                let end = if input.len() < self.len() {
                    input.len()
                } else {
                    input.ceil_char_boundary(self.len())
                };
                TagStrError(self.to_string(), input[..end].to_string()).into()
            })
        }
    }
}

/// char tag error containing the expected and found char
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TagCharError(pub char, pub char);

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

impl std::error::Error for TagCharError {}

impl<'a, E> Tag<&'a str, E> for char
where
    E: StdError,
    Incomplete: Into<E>,
    TagCharError: Into<E>,
{
    type Output = char;

    fn strip_from(&self, input: &'a str) -> Result<(&'a str, (Self::Output,)), E> {
        if let Some(x) = input.strip_prefix(*self) {
            Ok((x, (*self,)))
        } else {
            Err(if input.len() < self.len_utf8() {
                Incomplete::Size(self.len_utf8() - input.len()).into()
            } else {
                TagCharError(*self, input.chars().next().unwrap()).into()
            })
        }
    }
}

/// takes `qty` of chars from an input
///
/// ```rust
/// # use pipe_chain::{
/// #   str::chars,
/// #   Incomplete, Pipe,
/// # };
/// assert_eq!(
///     chars::<_, Incomplete>(..5).unwrap().apply("aỹe"),
///     Ok(("", (vec!['a', 'y', '\u{0303}', 'e'],)))
/// );
/// ```
pub fn chars<'a, E, E2>(
    qty: impl TryInto<Repetition, Error = E>,
) -> Result<impl Pipe<&'a str, (Vec<char>,), E2>, E>
where
    Incomplete: Into<E2>,
{
    let qty = qty.try_into()?;
    Ok(move |input| take_atom(qty).unwrap().apply(CharAtom::from(input)))
}

/// takes `qty` of graphemes clusters from an input
///
/// ```rust
/// # use pipe_chain::{
/// #   str::graphemes,
/// #   Incomplete, Pipe,
/// # };
/// assert_eq!(
///     graphemes::<_, Incomplete>(..5).unwrap().apply("aỹe"),
///     Ok(("", (vec!["a", "ỹ", "e"],)))
/// );
/// ```
pub fn graphemes<'a, E, E2>(
    qty: impl TryInto<Repetition, Error = E>,
) -> Result<impl Pipe<&'a str, (Vec<&'a str>,), E2>, E>
where
    Incomplete: Into<E2>,
{
    let qty = qty.try_into()?;
    Ok(move |input| take_atom(qty).unwrap().apply(GraphemeAtom::from(input)))
}

/// takes `qty` of words from an input
///
/// ```rust
/// # use pipe_chain::{
/// #   str::words,
/// #   Incomplete, Pipe,
/// # };
/// assert_eq!(
///     words::<_, Incomplete>(..5)
///         .unwrap()
///         .apply("Pack my box with five dozen liquor jugs."),
///     Ok((" with five dozen liquor jugs.", (vec!["Pack", " ", "my", " ", "box",],)))
/// );
/// ```
pub fn words<'a, E, E2>(
    qty: impl TryInto<Repetition, Error = E>,
) -> Result<impl Pipe<&'a str, (Vec<&'a str>,), E2>, E>
where
    Incomplete: Into<E2>,
{
    let qty = qty.try_into()?;
    Ok(move |input| take_atom(qty).unwrap().apply(WordAtom::from(input)))
}

/// takes `qty` of sentences from an input
/// /// takes `qty` of words from an input
///
/// ```rust
/// # use pipe_chain::{
/// #   str::{sentences, words},
/// #   Incomplete, Pipe,
/// # };
/// assert_eq!(
///     sentences::<_, Incomplete>(..2)
///         .unwrap()
///         .apply("Sentence 1. Sentence 2. Sentence 3. Sentence 4"),
///     Ok(("Sentence 3. Sentence 4", (vec!["Sentence 1. ", "Sentence 2. "],)))
/// );
/// assert_eq!(
///     sentences::<_, Incomplete>(..)
///         .unwrap()
///         .apply("Sentence 1. Sentence 2. Sentence 3. Sentence 4"),
///     Ok(("", (vec!["Sentence 1. ", "Sentence 2. ", "Sentence 3. ", "Sentence 4"],)))
/// );
/// ```
pub fn sentences<'a, E, E2>(
    qty: impl TryInto<Repetition, Error = E>,
) -> Result<impl Pipe<&'a str, (Vec<&'a str>,), E2>, E>
where
    Incomplete: Into<E2>,
{
    let qty = qty.try_into()?;
    Ok(move |input| take_atom(qty).unwrap().apply(SentenceAtom::from(input)))
}

/// Returns the consumed input instead of the pipe result
/// ```rust
/// # use fatal_error::FatalError;
/// # use pipe_chain::{Pipe, AndExt, AndThenExt, tag, str::{consumed, TagStrError}, Incomplete};
/// # #[derive(Debug, PartialEq, Eq)]
/// # enum Error {
/// #     Incomplete(Incomplete),
/// #     Tag(TagStrError)
/// # }
/// #
/// # impl std::fmt::Display for Error {
/// #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// #         write!(f, "{self:?}")
/// #     }
/// # }
/// #
/// # impl From<Incomplete> for Error {
/// #     fn from(value: Incomplete) -> Self { Error::Incomplete(value) }
/// # }
/// # impl From<TagStrError> for Error {
/// #     fn from(value: TagStrError) -> Self { Error::Tag(value) }
/// # }
/// #
/// # impl std::error::Error for Error {}
/// assert_eq!(
///     consumed(tag::<Error, _, _>("foo").and(tag("bar"))).apply("foobarbaz"),
///     Ok(("baz", ("foobar",)))
/// );
/// ```
pub fn consumed<'a, O, E>(
    mut p: impl Pipe<&'a str, O, E>,
) -> impl Pipe<&'a str, (&'a str,), E> {
    move |x: &'a str| {
        let (i, _) = p.apply(x)?;
        Ok((i, (&x[..x.len() - i.len()],)))
    }
}

/// Get the consumed offset in bytes in addition to the output of the given [Pipe]
/// ```rust
/// # use pipe_chain::{
/// #     str::{consumed, with_offset, TagStrError},
/// #     tag, AndExt, Incomplete, Pipe,
/// # };
/// # use fatal_error::FatalError;
/// #
/// # #[derive(Debug, PartialEq, Eq)]
/// # enum Error {
/// #     Incomplete(Incomplete),
/// #     Tag(TagStrError),
/// # }
/// #
/// # impl std::fmt::Display for Error {
/// #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// #         write!(f, "{self:?}")
/// #     }
/// # }
/// #
/// # impl From<Incomplete> for Error {
/// #     fn from(value: Incomplete) -> Self { Error::Incomplete(value) }
/// # }
/// #
/// # impl From<TagStrError> for Error {
/// #     fn from(value: TagStrError) -> Self { Error::Tag(value) }
/// # }
/// #
/// # impl std::error::Error for Error {}
/// assert_eq!(
///     with_offset(tag::<Error, _, _>("foo").and(tag("bar"))).apply("foobarbaz"),
///     Ok(("baz", ("foo", "bar", 6)))
/// );
/// ```
pub fn with_offset<'a, O: PushBack<usize>, E>(
    mut p: impl Pipe<&'a str, O, E>,
) -> impl Pipe<&'a str, O::Output, E> {
    move |x: &'a str| {
        let (i, o) = p.apply(x)?;
        Ok((i, (o.push_back(x.len() - i.len()))))
    }
}

#[cfg(test)]
mod test {

    use crate::{str::sentences, Incomplete, Pipe};

    #[test]
    fn test_unicode() {
        assert_eq!(
            sentences::<_, Incomplete>(..)
                .unwrap()
                .apply("Pack my box with five dozen liquor jugs."),
            Ok(("", (vec!["Pack my box with five dozen liquor jugs.",],)))
        );
    }
}