tokit 0.0.0

Blazing fast parser combinators: parse-while-lexing (zero-copy), deterministic LALR-style parsing, no backtracking. Flexible emitters for fail-fast runtime or greedy compiler diagnostics
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
//! Error collection container that adapts to allocation environments.
//!
//! This module provides the `Errors` type for collecting multiple errors during parsing
//! or validation. The container automatically adapts based on available features:
//!
//! - **no_std (no alloc)**: Uses `ConstGenericArrayDeque<E, 2>` with fixed capacity of 2 errors
//! - **alloc/std**: Uses `Vec<E>` for unlimited error collection
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```rust
//! use tokit::error::Errors;
//!
//! let mut errors = Errors::new();
//! errors.push("First error");
//! errors.push("Second error");
//!
//! assert_eq!(errors.len(), 2);
//! assert!(!errors.is_empty());
//! ```
//!
//! ## Iteration
//!
//! ```rust
//! use tokit::error::Errors;
//!
//! let mut errors = Errors::new();
//! errors.push(1);
//! errors.push(2);
//!
//! let sum: i32 = errors.iter().sum();
//! assert_eq!(sum, 3);
//! ```

use core::fmt::{Debug, Display};

#[cfg(not(any(feature = "alloc", feature = "std")))]
use generic_arraydeque::ConstGenericArrayDeque;

#[cfg(any(feature = "alloc", feature = "std"))]
use std::collections::VecDeque;

/// Default error container for no-alloc environments.
///
/// Uses a stack-allocated `ConstGenericArrayDeque` with capacity for 2 errors.
/// When the capacity is exceeded, additional errors are dropped and
/// [`Errors::overflowed`](Errors::overflowed) becomes `true`.
#[cfg(not(any(feature = "alloc", feature = "std")))]
pub type DefaultContainer<E> = ConstGenericArrayDeque<E, 2>;

/// Default error container for alloc/std environments.
///
/// Uses a heap-allocated `Vec` for unlimited error collection.
#[cfg(any(feature = "alloc", feature = "std"))]
pub type DefaultContainer<E> = VecDeque<E>;

/// A collection of errors that adapts to the allocation environment.
///
/// This type is generic over both the error type `E` and the container `C`.
/// By default:
/// - In no-alloc environments: Uses `ConstGenericArrayDeque<E, 2>` (capacity of 2)
/// - In alloc/std environments: Uses `VecDeque<E>` (unlimited capacity)
///
/// # Type Parameters
///
/// - `E`: The error type to store
/// - `C`: The container type (defaults to environment-appropriate container)
///
/// # Examples
///
/// ## Using Default Container
///
/// ```rust
/// use tokit::error::Errors;
///
/// let mut errors = Errors::new();
/// errors.push("Error 1");
/// errors.push("Error 2");
///
/// assert_eq!(errors.len(), 2);
/// ```
///
/// ## Type Inference
///
/// ```rust
/// use tokit::error::Errors;
///
/// // Type inference works seamlessly
/// let mut errors = Errors::new();
/// errors.push("Error 1");
/// errors.push("Error 2");
///
/// let first: Option<&&str> = errors.front();
/// assert_eq!(first, Some(&"Error 1"));
/// ```
#[derive(
  Debug,
  Clone,
  PartialEq,
  Eq,
  Hash,
  derive_more::Deref,
  derive_more::DerefMut,
  derive_more::AsRef,
  derive_more::AsMut,
)]
pub struct Errors<E, C = DefaultContainer<E>> {
  #[deref]
  #[deref_mut]
  #[as_ref]
  #[as_mut]
  container: C,
  overflowed_flag: bool,
  _phantom: core::marker::PhantomData<E>,
}

// Implementation for no-alloc environments (ConstGenericArrayDeque)
#[cfg(not(any(feature = "alloc", feature = "std")))]
impl<E> Errors<E> {
  /// Creates a new empty error collection.
  ///
  /// In no-alloc environments, this creates a `ConstGenericArrayDeque` with capacity 2.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use tokit::error::Errors;
  ///
  /// let errors: Errors<String> = Errors::new();
  /// assert!(errors.is_empty());
  /// ```
  #[inline]
  pub const fn new() -> Self {
    Self::new_in(DefaultContainer::new())
  }
}

// Implementation for alloc/std environments (Vec)
#[cfg(any(feature = "alloc", feature = "std"))]
impl<E> Errors<E> {
  /// Creates a new empty error collection.
  ///
  /// In alloc/std environments, this creates an empty `Vec`.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use tokit::error::Errors;
  ///
  /// let errors: Errors<String> = Errors::new();
  /// assert!(errors.is_empty());
  /// ```
  #[inline]
  pub const fn new() -> Self {
    Self::new_in(VecDeque::new())
  }

  /// Returns the number of errors the collection can hold without reallocating.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use tokit::error::Errors;
  ///
  /// let errors: Errors<String> = Errors::with_capacity(10);
  /// assert_eq!(errors.capacity(), 10);
  /// ```
  #[inline]
  pub fn capacity(&self) -> usize {
    self.container.capacity()
  }

  /// Reserves capacity for at least `additional` more errors.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use tokit::error::Errors;
  ///
  /// let mut errors: Errors<String> = Errors::new();
  /// errors.reserve(10);
  /// assert!(errors.capacity() >= 10);
  /// ```
  #[inline]
  pub fn reserve(&mut self, additional: usize) {
    self.container.reserve(additional);
  }
}

impl<E, Container> Errors<E, Container>
where
  Container: super::ErrorContainer<E>,
{
  /// Pushes an error into the collection, marking `overflowed` if it doesn't fit.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn push(&mut self, error: E) {
    let _ = self.try_push(error);
  }

  /// Attempts to push an error, returning it back if capacity is exhausted.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn try_push(&mut self, error: E) -> Result<(), E> {
    match super::ErrorContainer::try_push(&mut self.container, error) {
      Ok(()) => Ok(()),
      Err(err) => {
        self.overflowed_flag = true;
        Err(err)
      }
    }
  }

  /// Returns `true` if any error has been dropped because of limited capacity.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn overflowed(&self) -> bool {
    self.overflowed_flag
  }

  /// Reports the remaining capacity when the backing container is bounded.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn remaining_capacity(&self) -> Option<usize> {
    super::ErrorContainer::remaining_capacity(&self.container)
  }

  /// Returns `true` when a bounded container cannot accept more errors.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn is_full(&self) -> bool {
    matches!(self.remaining_capacity(), Some(0))
  }

  /// Creates a new empty error collection with the specified capacity.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use tokit::error::Errors;
  ///
  /// let errors: Errors<String> = Errors::with_capacity(5);
  /// assert_eq!(errors.len(), 0);
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_capacity(capacity: usize) -> Self {
    Self::new_in(Container::with_capacity(capacity))
  }
}

impl<E, Container> Errors<E, Container> {
  #[inline]
  const fn new_in(container: Container) -> Self {
    Self {
      container,
      overflowed_flag: false,
      _phantom: core::marker::PhantomData,
    }
  }
}

// Default trait implementations
impl<E, Container> Default for Errors<E, Container>
where
  Container: Default,
{
  #[inline]
  fn default() -> Self {
    Self::new_in(Container::default())
  }
}

// AsRef and AsMut implementations
impl<E, C> AsRef<[E]> for Errors<E, C>
where
  C: AsRef<[E]>,
{
  #[inline]
  fn as_ref(&self) -> &[E] {
    self.container.as_ref()
  }
}

impl<E, C> AsMut<[E]> for Errors<E, C>
where
  C: AsMut<[E]>,
{
  #[inline]
  fn as_mut(&mut self) -> &mut [E] {
    self.container.as_mut()
  }
}

// Display implementation for better error reporting
impl<E, C> Display for Errors<E, C>
where
  E: Display,
  C: AsRef<[E]>,
{
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    let errors = self.container.as_ref();

    if errors.is_empty() {
      return Ok(());
    }

    if errors.len() == 1 {
      write!(f, "{}", errors[0])
    } else {
      writeln!(f, "{} errors:", errors.len())?;
      for (i, error) in errors.iter().enumerate() {
        write!(f, "  {}. {}", i + 1, error)?;
        if i < errors.len() - 1 {
          writeln!(f)?;
        }
      }
      Ok(())
    }
  }
}

impl<'a, E, Container> IntoIterator for &'a Errors<E, Container>
where
  &'a Container: IntoIterator<Item = &'a E>,
{
  type Item = &'a E;
  type IntoIter = <&'a Container as IntoIterator>::IntoIter;

  #[inline]
  fn into_iter(self) -> Self::IntoIter {
    (&self.container).into_iter()
  }
}

impl<E, Container> IntoIterator for Errors<E, Container>
where
  Container: IntoIterator<Item = E>,
{
  type Item = E;
  type IntoIter = Container::IntoIter;

  #[inline]
  fn into_iter(self) -> Self::IntoIter {
    self.container.into_iter()
  }
}

impl<E, Container> FromIterator<E> for Errors<E, Container>
where
  Container: FromIterator<E>,
{
  #[inline]
  fn from_iter<I: IntoIterator<Item = E>>(iter: I) -> Self {
    Self::from_container(Container::from_iter(iter))
  }
}

impl<E, C> From<E> for Errors<E, C>
where
  C: FromIterator<E>,
{
  #[inline]
  fn from(error: E) -> Self {
    Self::from_iter(core::iter::once(error))
  }
}

impl<E, C> Errors<E, C> {
  /// Creates an `Errors` instance from an existing container.
  ///
  /// ## Examples
  ///
  /// ```rust
  /// # #[cfg(any(feature = "alloc", feature = "std"))] {
  /// use tokit::error::{Errors, DefaultContainer};
  ///
  /// let errors = Errors::<&str, DefaultContainer<_>>::from_container(["Error 1", "Error 2"].into_iter().collect());
  /// assert_eq!(errors.len(), 2);
  /// # }
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn from_container(container: C) -> Self {
    Self::new_in(container)
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use generic_arraydeque::ConstGenericArrayDeque;

  #[test]
  fn test_new() {
    let _: Errors<&str> = Errors::new();
  }

  #[test]
  fn test_push_and_len() {
    let mut errors = Errors::new();
    errors.push("Error 1");
    assert_eq!(errors.len(), 1);
    errors.push("Error 2");
    assert_eq!(errors.len(), 2);
  }

  #[test]
  fn test_clear() {
    let mut errors = Errors::new();
    errors.push("Error");
    errors.clear();
    assert!(errors.is_empty());
  }

  #[test]
  fn test_iteration() {
    let mut errors = Errors::new();
    errors.push(1);
    errors.push(2);

    let sum: i32 = errors.iter().sum();
    assert_eq!(sum, 3);
  }

  #[test]
  fn test_overflow_tracking() {
    type SmallErrors<'a> = Errors<&'a str, ConstGenericArrayDeque<&'a str, 1>>;
    let mut errors: SmallErrors<'_> = Errors::from_container(ConstGenericArrayDeque::<_, 1>::new());

    assert!(!errors.overflowed());
    errors.push("first");
    assert_eq!(errors.len(), 1);
    assert_eq!(errors.remaining_capacity(), Some(0));
    assert!(errors.is_full());

    errors.push("second");
    assert!(errors.overflowed());
    assert_eq!(errors.len(), 1);
  }

  #[test]
  fn test_try_push_reports_error() {
    type SmallErrors<'a> = Errors<&'a str, ConstGenericArrayDeque<&'a str, 1>>;
    let mut errors: SmallErrors<'_> = Errors::from_container(ConstGenericArrayDeque::<_, 1>::new());

    assert!(errors.try_push("first").is_ok());
    assert!(errors.try_push("second").is_err());
    assert!(errors.overflowed());
  }

  #[cfg(any(feature = "alloc", feature = "std"))]
  #[test]
  fn test_with_capacity() {
    let errors: Errors<&str> = Errors::with_capacity(10);
    assert_eq!(errors.capacity(), 10);
    assert!(errors.is_empty());
  }

  #[cfg(any(feature = "alloc", feature = "std"))]
  #[test]
  fn test_pop() {
    use crate::error::ErrorContainer;

    let mut errors = Errors::new();
    errors.push(1);
    errors.push(2);

    assert_eq!(errors.pop(), Some(1));
    assert_eq!(errors.pop(), Some(2));
    assert_eq!(errors.pop(), None);
  }
}