g-string 0.1.3

Stack-allocated string type with generic configurations.
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
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]

mod conversion;
mod equality;
mod error;
mod iterator;
mod macros;
mod mutation;
mod order;
mod query;
pub use query::Pattern;

mod pattern_impl;

#[cfg(feature = "secret")]
mod gsecret;

#[cfg(feature = "secret")]
pub use gsecret::GSecret;

#[cfg(feature = "grapheme")]
pub use unicode_segmentation::{Graphemes, UnicodeSegmentation};

#[cfg(feature = "serde")]
mod serde;

pub use error::GStringError;

use error::Err;

use core::{
    convert::Infallible,
    fmt::{Debug, Display},
    marker::PhantomData,
};

/// Default value of lower bound.
pub const DEFAULT_MIN: usize = 0;

/// Default value of upper bound.
pub const DEFAULT_MAX: usize = 255;

/// Default value is ASCII only or not.
pub const DEFAULT_ASCII_ONLY: bool = false;

/// GString alias without validation.
pub type GStringNV<const MIN: usize, const MAX: usize, const ASCII_ONLY: bool> =
    GString<NoValidation, MIN, MAX, ASCII_ONLY>;

/// Validation trait
///
/// Usually it's implemented by marker type.
pub trait Validator: Clone {
    type Error;

    /// Validate the string.
    fn validate(s: impl AsRef<str>) -> Result<(), Self::Error>;
}

/// Mark validation allowing empty string.
///
/// This will enable `Default` impl and `clear()` method provided that MIN == 0.
pub trait AllowEmpty {}

/// Validator implementation without validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NoValidation;

impl Validator for NoValidation {
    type Error = Infallible;

    #[inline]
    fn validate(_: impl AsRef<str>) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl Validator for () {
    type Error = Infallible;

    #[inline]
    fn validate(_: impl AsRef<str>) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl AllowEmpty for NoValidation {}

impl AllowEmpty for () {}

/// `GString` contains stack-allocated, Copy, bounded string along with ASCII toggle and embedded validation logic.
///
/// # Generic parameters:
/// - `V: Validator`: default to [`NoValidation`], it will validate the string upon creation and deserialization.
/// - `const MIN: usize`: default to [`DEFAULT_MIN`], in bytes, determine minimum length.
/// - `const MAX: usize`: default to [`DEFAULT_MAX`], in bytes, determine maximum length.
/// - `const ASCII_ONLY: bool`: default to [`DEFAULT_ASCII_ONLY`], determine whether GString may contains arbitrary or ASCII only UTF-8 encoded string.
///
/// # Usage
/// You can use this type in several ways:
/// - Defaulted to default generic params: `let a: GString = GString::try_new("anjay!!").unwrap()`.
/// - Defaulted to default generic params with `try_default(...)`: `let a = GString::try_default("anjay!!").unwrap()`.
/// - Using type aliases. You can declare some aliases matching the behavior you want: `type Username = GString<UsernameValidation, 3, 20, true>`.
/// - Declaring each generic params from left to right. Declaration must be from left to right with omission allowed on right-most params: `let a = GString::<(), 2>::try_new("anjay!!").unwrap()`.
///
/// # Examples
/// ```rust
/// use g_string::{GString, Validator, GStringError};
///
/// let a: GString = GString::try_new("anjay!!").unwrap();
/// assert_eq!(a, "anjay!!");
///
/// let a = GString::try_default("anjay!!").unwrap();
/// assert_eq!(a, "anjay!!");
///
/// #[derive(Debug, Clone)]
/// struct UsernameValidation;
///
/// impl Validator for UsernameValidation {
///     type Error = GStringError<&'static str>;
///
///     fn validate(_: impl AsRef<str>) -> Result<(), Self::Error> {
///         // some validation logics here...
///         Ok(())
///     }   
/// }
///
/// type Username = GString<UsernameValidation, 3, 20, true>;
/// let a = Username::try_new("wanjay!!🤣");
/// assert!(a.is_err()); // because '🤣' is not ASCII.
///
/// let a = GString::<(), 2, 4>::try_new("anjay!!");
/// assert!(a.is_err()); // MAX is 4.
/// ```
#[derive(Copy, Clone, Eq)]
pub struct GString<
    V: Validator = NoValidation,
    const MIN: usize = DEFAULT_MIN,
    const MAX: usize = DEFAULT_MAX,
    const ASCII_ONLY: bool = DEFAULT_ASCII_ONLY,
> {
    buf: [u8; MAX],
    len: usize,
    _validator: PhantomData<V>,
}

impl GString {
    /// Construct with default generic params.
    ///
    /// # Examples
    /// ```rust
    /// use g_string::GString;
    ///
    /// let a = GString::try_default("anjay!!").unwrap();
    /// assert_eq!(a, "anjay!!");
    /// ```
    #[inline]
    pub fn try_default<S>(s: S) -> Result<Self, GStringError<Infallible>>
    where
        S: AsRef<str>,
    {
        Self::try_new(s)
    }
}

impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool>
    GString<V, MIN, MAX, ASCII_ONLY>
{
    /// Construct new GString. All invariants from generic params will be imposed here.
    ///
    /// # Examples
    /// ```rust
    /// use g_string::GString;
    ///
    /// let a: GString = GString::try_new("anjay!!").unwrap();
    /// assert_eq!(a, "anjay!!");
    /// ```
    #[inline]
    pub fn try_new<S>(s: S) -> Result<Self, GStringError<V::Error>>
    where
        S: AsRef<str>,
    {
        let gstring = Self::stack_allocate(s.as_ref())?;

        gstring.check_bounds()?;
        gstring.check_ascii()?;

        gstring.validate()
    }

    // Allocate `s` in stack without validations(yet).
    const fn stack_allocate(s: &str) -> Result<Self, Err> {
        let bytes = s.as_bytes();
        let len = bytes.len();

        if len > MAX {
            return Err(Err::TooLong(MAX));
        }

        let mut buf = [0u8; MAX];
        let mut i = 0;
        while i < len {
            buf[i] = bytes[i];
            i += 1;
        }

        Ok(Self {
            buf,
            len,
            _validator: PhantomData,
        })
    }

    // Check upper and lower bounds.
    #[inline]
    const fn check_bounds(&self) -> Result<(), Err> {
        const {
            assert!(MIN <= MAX, "MIN cannot be bigger than MAX");
        }
        if self.len < MIN {
            return Err(Err::TooShort(MIN));
        }
        if self.len > MAX {
            return Err(Err::TooLong(MAX));
        }

        Ok(())
    }

    // Check whether ASCII only met.
    #[inline]
    const fn check_ascii(&self) -> Result<(), Err> {
        if ASCII_ONLY {
            let mut i = 0;
            while i < self.len {
                // If a byte is >= 128, it's a multi-byte UTF-8 character (not ASCII)
                if self.buf[i] >= 128 {
                    return Err(Err::NotAscii);
                }
                i += 1;
            }
        }

        Ok(())
    }

    // Execute validation logic.
    #[inline(always)]
    pub fn validate(self) -> Result<Self, GStringError<V::Error>> {
        V::validate(&self).map_err(GStringError::Validation)?;
        Ok(self)
    }

    /// Calls a closure with a reference to the contained string.
    ///
    /// Returns `self` unchanged.
    ///
    /// This behaves similarly to `Option::inspect` and
    /// `Result::inspect`.
    #[inline]
    pub fn inspect<F>(self, func: F) -> Self
    where
        F: FnOnce(&str),
    {
        func(self.as_str());
        self
    }

    /// Transforms this string into another validated string.
    ///
    /// The transformed value is validated using the destination
    /// validator and bounds before being returned.
    ///
    /// This behaves similarly to `Option::map` and `Result::map`.
    #[inline]
    pub fn map<UV, const UMIN: usize, const UMAX: usize, const UASCII_ONLY: bool, F, U>(
        self,
        func: F,
    ) -> Result<GString<UV, UMIN, UMAX, UASCII_ONLY>, GStringError<UV::Error>>
    where
        UV: Validator,
        F: FnOnce(&str) -> U,
        U: AsRef<str>,
    {
        GString::<UV, UMIN, UMAX, UASCII_ONLY>::try_new(func(self.as_str()).as_ref())
    }

    /// Chains another fallible validated transformation.
    ///
    /// This behaves similarly to `Option::and_then` and
    /// `Result::and_then`.
    #[inline]
    pub fn and_then<UV, const UMIN: usize, const UMAX: usize, const UASCII_ONLY: bool, F>(
        self,
        func: F,
    ) -> Result<GString<UV, UMIN, UMAX, UASCII_ONLY>, GStringError<UV::Error>>
    where
        UV: Validator,
        F: FnOnce(&str) -> Result<GString<UV, UMIN, UMAX, UASCII_ONLY>, GStringError<UV::Error>>,
    {
        func(self.as_str())
    }
}

/// GString from const generic constructor.
///
/// It must be validated before getting actual `GString`.
#[derive(Debug)]
pub struct InValidatedGString<
    V: Validator,
    const MIN: usize,
    const MAX: usize,
    const ASCII_ONLY: bool,
>(GString<V, MIN, MAX, ASCII_ONLY>);

impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool>
    InValidatedGString<V, MIN, MAX, ASCII_ONLY>
{
    /// Validate into GString.
    #[inline(always)]
    pub fn validate(self) -> Result<GString<V, MIN, MAX, ASCII_ONLY>, GStringError<V::Error>> {
        self.0.validate()
    }
}

macro_rules! errpanic {
    ($expr:expr) => {
        match $expr {
            Ok(v) => v,
            Err(Err::TooShort(_)) => {
                panic!("minimum length below MIN")
            }
            Err(Err::TooLong(_)) => {
                panic!("maximum length exceeds MAX")
            }
            Err(Err::NotAscii) => {
                panic!("only ASCII characters are allowed")
            }
        }
    };
}

impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool>
    GString<V, MIN, MAX, ASCII_ONLY>
{
    /// Construct GString in const context returning invalidated string.
    /// Validate the return to get GString.
    #[allow(clippy::new_ret_no_self)]
    pub const fn new(s: &str) -> InValidatedGString<V, MIN, MAX, ASCII_ONLY> {
        let ret = errpanic!(Self::stack_allocate(s));
        errpanic!(ret.check_bounds());
        errpanic!(ret.check_ascii());
        InValidatedGString(ret)
    }
}

impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool> Display
    for GString<V, MIN, MAX, ASCII_ONLY>
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool> Debug
    for GString<V, MIN, MAX, ASCII_ONLY>
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "GString(\"{}\", MIN={}, MAX={}, ASCII_ONLY={})",
            self.as_str(),
            MIN,
            MAX,
            ASCII_ONLY
        )
    }
}

impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool>
    core::borrow::Borrow<str> for GString<V, MIN, MAX, ASCII_ONLY>
{
    #[inline]
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool> core::hash::Hash
    for GString<V, MIN, MAX, ASCII_ONLY>
{
    #[inline]
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.as_str().hash(state)
    }
}

impl<V: Validator + AllowEmpty, const MAX: usize, const ASCII_ONLY: bool> Default
    for GString<V, 0, MAX, ASCII_ONLY>
{
    #[inline]
    fn default() -> Self {
        Self {
            buf: [0u8; MAX],
            len: 0,
            _validator: PhantomData,
        }
    }
}

#[cfg(feature = "secret")]
impl<V: Validator, const MIN: usize, const MAX: usize, const ASCII_ONLY: bool>
    GString<V, MIN, MAX, ASCII_ONLY>
{
    pub fn zeroize(&mut self) {
        use zeroize::Zeroize;

        self.buf.zeroize();
        self.len.zeroize();
    }
}