tighterror-build 0.0.22

The tighterror code generation library.
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
//! Crate errors.

/**
 * Error category type.
 *
 * See the [categories] module for category constants.
*/

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct TbErrorCategory(_p::R);

impl TbErrorCategory {
    #[inline]
    const fn new(v: _p::R) -> Self {
        Self(v)
    }

    /// Returns the name of the error category.
    #[inline]
    pub fn name(&self) -> &'static str {
        _cn::A[self.0 as usize]
    }
}

impl tighterror::Category for TbErrorCategory {
    type R = _p::R;
    const BITS: usize = _p::CAT_BITS;

    #[inline]
    fn name(&self) -> &'static str {
        self.name()
    }
}

impl core::fmt::Display for TbErrorCategory {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.pad(self.name())
    }
}

impl core::fmt::Debug for TbErrorCategory {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_tuple("TbErrorCategory")
            .field(&_p::Ident(self.name()))
            .finish()
    }
}

/**
 * Error kind type.
 *
 * See the [kinds] module for error kind constants.
*/

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct TbErrorKind(_p::R);

impl TbErrorKind {
    const fn new(cat: TbErrorCategory, variant: _p::R) -> Self {
        Self(cat.0 << _p::VAR_BITS | variant)
    }

    #[inline]
    fn category_value(&self) -> _p::R {
        (self.0 & _p::CAT_MASK) >> _p::VAR_BITS
    }

    #[inline]
    fn variant_value(&self) -> _p::R {
        self.0 & _p::VAR_MASK
    }

    /// Returns the error category.
    #[inline]
    pub fn category(&self) -> TbErrorCategory {
        TbErrorCategory::new(self.category_value())
    }

    /// Returns the error kind name.
    #[inline]
    pub fn name(&self) -> &'static str {
        _n::A[self.category_value() as usize][self.variant_value() as usize]
    }

    #[inline]
    fn display(&self) -> &'static str {
        _d::A[self.category_value() as usize][self.variant_value() as usize]
    }

    /// Returns the error kind value as the underlying Rust type.
    #[inline]
    pub fn value(&self) -> _p::R {
        self.0
    }

    /// Creates an error kind from a raw value of the underlying Rust type.
    #[inline]
    pub fn from_value(value: _p::R) -> Option<Self> {
        let cat = (value & _p::CAT_MASK) >> _p::VAR_BITS;
        let variant = value & _p::VAR_MASK;
        if cat <= _p::CAT_MAX && variant <= _p::VAR_MAXES[cat as usize] {
            Some(Self::new(TbErrorCategory::new(cat), variant))
        } else {
            None
        }
    }
}

impl tighterror::Kind for TbErrorKind {
    type R = _p::R;
    type Category = TbErrorCategory;
    const BITS: usize = _p::KIND_BITS;

    #[inline]
    fn category(&self) -> Self::Category {
        self.category()
    }

    #[inline]
    fn name(&self) -> &'static str {
        self.name()
    }

    #[inline]
    fn value(&self) -> Self::R {
        self.value()
    }

    #[inline]
    fn from_value(value: Self::R) -> Option<Self> {
        Self::from_value(value)
    }
}

impl core::fmt::Display for TbErrorKind {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.pad(self.name())
    }
}

impl core::fmt::Debug for TbErrorKind {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TbErrorKind")
            .field("cat", &_p::Ident(self.category().name()))
            .field("var", &_p::Ident(self.name()))
            .field("val", &self.0)
            .finish()
    }
}

impl<T> core::convert::From<TbErrorKind> for Result<T, TbError> {
    #[inline]
    fn from(v: TbErrorKind) -> Self {
        Err(v.into())
    }
}

/**
 * Error type.
 *
 * See the [kinds] module for error kind constants.
*/

#[derive(Debug)]
#[repr(transparent)]
pub struct TbError(TbErrorKind);

impl TbError {
    /// Returns the error kind.
    #[inline]
    pub fn kind(&self) -> TbErrorKind {
        self.0
    }

    /// Returns the error origin location.
    #[inline]
    pub fn location(&self) -> tighterror::Location {
        tighterror::Location::undefined()
    }
}

impl tighterror::Error for TbError {
    type R = _p::R;
    type Category = TbErrorCategory;
    type Kind = TbErrorKind;

    #[inline]
    fn kind(&self) -> Self::Kind {
        self.kind()
    }

    #[inline]
    fn location(&self) -> tighterror::Location {
        self.location()
    }
}

impl core::convert::From<TbErrorKind> for TbError {
    #[inline]
    fn from(kind: TbErrorKind) -> Self {
        Self(kind)
    }
}

impl core::fmt::Display for TbError {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.pad(self.kind().display())
    }
}

impl core::cmp::PartialEq for TbError {
    /// Checks equality based on the error kind only.
    #[inline]
    fn eq(&self, other: &TbError) -> bool {
        self.0 == other.0
    }
}

impl<T> core::convert::From<TbError> for core::result::Result<T, TbError> {
    #[inline]
    fn from(err: TbError) -> Self {
        Err(err)
    }
}

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

mod _cn {
    pub const PARSER: &str = "PARSER";
    pub const CODER: &str = "CODER";
    pub static A: [&str; 2] = [PARSER, CODER];
}

mod _n {
    pub(crate) mod parser {
        pub(crate) const BAD_IDENTIFIER_CHARACTERS: &str = "BAD_IDENTIFIER_CHARACTERS";
        pub(crate) const BAD_IDENTIFIER_CASE: &str = "BAD_IDENTIFIER_CASE";
        pub(crate) const BAD_KEYWORD_TYPE: &str = "BAD_KEYWORD_TYPE";
        pub(crate) const BAD_MODULE_IDENTIFIER: &str = "BAD_MODULE_IDENTIFIER";
        pub(crate) const BAD_NAME: &str = "BAD_NAME";
        pub(crate) const BAD_OBJECT_ATTRIBUTE: &str = "BAD_OBJECT_ATTRIBUTE";
        pub(crate) const BAD_SPEC_FILE_EXTENSION: &str = "BAD_SPEC_FILE_EXTENSION";
        pub(crate) const BAD_TOML: &str = "BAD_TOML";
        pub(crate) const BAD_ROOT_LEVEL_KEYWORD: &str = "BAD_ROOT_LEVEL_KEYWORD";
        pub(crate) const BAD_VALUE_TYPE: &str = "BAD_VALUE_TYPE";
        pub(crate) const BAD_YAML: &str = "BAD_YAML";
        pub(crate) const EMPTY_IDENTIFIER: &str = "EMPTY_IDENTIFIER";
        pub(crate) const EMPTY_LIST: &str = "EMPTY_LIST";
        pub(crate) const FAILED_TO_OPEN_SPEC_FILE: &str = "FAILED_TO_OPEN_SPEC_FILE";
        pub(crate) const MISSING_ATTRIBUTE: &str = "MISSING_ATTRIBUTE";
        pub(crate) const MUTUALLY_EXCLUSIVE_KEYWORDS: &str = "MUTUALLY_EXCLUSIVE_KEYWORDS";
        pub(crate) const NON_UNIQUE_NAME: &str = "NON_UNIQUE_NAME";
        pub(crate) const SPEC_FILE_NOT_FOUND: &str = "SPEC_FILE_NOT_FOUND";
        pub(crate) const NAME_COLLISION: &str = "NAME_COLLISION";
        pub static A: [&str; 19] = [
            BAD_IDENTIFIER_CHARACTERS,
            BAD_IDENTIFIER_CASE,
            BAD_KEYWORD_TYPE,
            BAD_MODULE_IDENTIFIER,
            BAD_NAME,
            BAD_OBJECT_ATTRIBUTE,
            BAD_SPEC_FILE_EXTENSION,
            BAD_TOML,
            BAD_ROOT_LEVEL_KEYWORD,
            BAD_VALUE_TYPE,
            BAD_YAML,
            EMPTY_IDENTIFIER,
            EMPTY_LIST,
            FAILED_TO_OPEN_SPEC_FILE,
            MISSING_ATTRIBUTE,
            MUTUALLY_EXCLUSIVE_KEYWORDS,
            NON_UNIQUE_NAME,
            SPEC_FILE_NOT_FOUND,
            NAME_COLLISION,
        ];
    }

    pub(crate) mod coder {
        pub(crate) const CATEGORY_REQUIRED: &str = "CATEGORY_REQUIRED";
        pub(crate) const ERROR_REQUIRED: &str = "ERROR_REQUIRED";
        pub(crate) const FAILED_TO_PARSE_TOKENS: &str = "FAILED_TO_PARSE_TOKENS";
        pub(crate) const FAILED_TO_READ_OUTPUT_FILE: &str = "FAILED_TO_READ_OUTPUT_FILE";
        pub(crate) const FAILED_TO_WRITE_OUTPUT_FILE: &str = "FAILED_TO_WRITE_OUTPUT_FILE";
        pub(crate) const RUSTFMT_FAILED: &str = "RUSTFMT_FAILED";
        pub(crate) const RUSTFMT_NOT_FOUND: &str = "RUSTFMT_NOT_FOUND";
        pub(crate) const TOO_MANY_BITS: &str = "TOO_MANY_BITS";
        pub(crate) const OUTPUT_PATH_NOT_DIRECTORY: &str = "OUTPUT_PATH_NOT_DIRECTORY";
        pub static A: [&str; 9] = [
            CATEGORY_REQUIRED,
            ERROR_REQUIRED,
            FAILED_TO_PARSE_TOKENS,
            FAILED_TO_READ_OUTPUT_FILE,
            FAILED_TO_WRITE_OUTPUT_FILE,
            RUSTFMT_FAILED,
            RUSTFMT_NOT_FOUND,
            TOO_MANY_BITS,
            OUTPUT_PATH_NOT_DIRECTORY,
        ];
    }

    pub static A: [&[&str]; 2] = [&parser::A, &coder::A];
}

mod _d {
    pub(crate) mod parser {
        pub(crate) const BAD_IDENTIFIER_CHARACTERS: &str =
            "Identifier contains unsupported characters.";
        pub(crate) const BAD_IDENTIFIER_CASE: &str =
            "Identifier is specified in an unsupported case.";
        pub(crate) const BAD_KEYWORD_TYPE: &str = "Specification keyword is not a String.";
        pub(crate) const BAD_MODULE_IDENTIFIER: &str = "Identifier is not valid on module-level.";
        pub(crate) const BAD_NAME: &str = "Invalid name.";
        pub(crate) const BAD_OBJECT_ATTRIBUTE: &str = "An object attribute is invalid.";
        pub(crate) const BAD_SPEC_FILE_EXTENSION: &str =
            "Specification filename extension is not supported or is missing.";
        pub(crate) const BAD_TOML: &str = "TOML deserialization has failed.";
        pub(crate) const BAD_ROOT_LEVEL_KEYWORD: &str =
            "Specification contains an invalid root-level keyword.";
        pub(crate) const BAD_VALUE_TYPE: &str = "Specification value type is invalid.";
        pub(crate) const BAD_YAML: &str = "YAML deserialization has failed.";
        pub(crate) const EMPTY_IDENTIFIER: &str = "An identifier cannot be an empty string.";
        pub(crate) const EMPTY_LIST: &str = "Empty list of objects is not allowed.";
        pub(crate) const FAILED_TO_OPEN_SPEC_FILE: &str = "Specification file couldn't be opened.";
        pub(crate) const MISSING_ATTRIBUTE: &str = "Specification lacks a mandatory attribute.";
        pub(crate) const MUTUALLY_EXCLUSIVE_KEYWORDS: &str =
            "Specification contains mutually exclusive keywords.";
        pub(crate) const NON_UNIQUE_NAME: &str = "A name is not unique.";
        pub(crate) const SPEC_FILE_NOT_FOUND: &str = "Specification file couldn't be found.";
        pub(crate) const NAME_COLLISION: &str = "Collision of names between different items.";
        pub static A: [&str; 19] = [
            BAD_IDENTIFIER_CHARACTERS,
            BAD_IDENTIFIER_CASE,
            BAD_KEYWORD_TYPE,
            BAD_MODULE_IDENTIFIER,
            BAD_NAME,
            BAD_OBJECT_ATTRIBUTE,
            BAD_SPEC_FILE_EXTENSION,
            BAD_TOML,
            BAD_ROOT_LEVEL_KEYWORD,
            BAD_VALUE_TYPE,
            BAD_YAML,
            EMPTY_IDENTIFIER,
            EMPTY_LIST,
            FAILED_TO_OPEN_SPEC_FILE,
            MISSING_ATTRIBUTE,
            MUTUALLY_EXCLUSIVE_KEYWORDS,
            NON_UNIQUE_NAME,
            SPEC_FILE_NOT_FOUND,
            NAME_COLLISION,
        ];
    }

    pub(crate) mod coder {
        pub(crate) const CATEGORY_REQUIRED: &str = "At least one category must be defined.";
        pub(crate) const ERROR_REQUIRED: &str = "At least one error must be defined.";
        pub(crate) const FAILED_TO_PARSE_TOKENS: &str = "Generated code tokens couldn't be parsed.";
        pub(crate) const FAILED_TO_READ_OUTPUT_FILE: &str = "Output file couldn't be read.";
        pub(crate) const FAILED_TO_WRITE_OUTPUT_FILE: &str = "Output file couldn't be written.";
        pub(crate) const RUSTFMT_FAILED: &str = "Rustfmt tool exited with an error.";
        pub(crate) const RUSTFMT_NOT_FOUND: &str = "Rustfmt tool isn't found.";
        pub(crate) const TOO_MANY_BITS: &str =
            "The number of required bits exceeds the largest supported type u64.";
        pub(crate) const OUTPUT_PATH_NOT_DIRECTORY: &str = "Output path is not a directory.";
        pub static A: [&str; 9] = [
            CATEGORY_REQUIRED,
            ERROR_REQUIRED,
            FAILED_TO_PARSE_TOKENS,
            FAILED_TO_READ_OUTPUT_FILE,
            FAILED_TO_WRITE_OUTPUT_FILE,
            RUSTFMT_FAILED,
            RUSTFMT_NOT_FOUND,
            TOO_MANY_BITS,
            OUTPUT_PATH_NOT_DIRECTORY,
        ];
    }

    pub static A: [&[&str]; 2] = [&parser::A, &coder::A];
}

mod _p {
    pub type R = u8;
    pub const KIND_BITS: usize = 6;
    pub const CAT_BITS: usize = 1;
    pub const CAT_MAX: R = 1;
    pub const VAR_MASK: R = 31;
    pub static VAR_MAXES: [R; 2] = [18, 8];
    pub const CAT_MASK: R = 32;
    pub const VAR_BITS: usize = 5;
    const _: () = assert!(KIND_BITS <= R::BITS as usize);
    const _: () = assert!(CAT_BITS <= usize::BITS as usize);
    pub(super) struct Ident<'a>(pub(super) &'a str);

    impl<'a> core::fmt::Debug for Ident<'a> {
        #[inline]
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            f.pad(self.0)
        }
    }
}

/// Error category constants.
pub mod category {
    use super::TbErrorCategory as C;

    /// Parser errors category.
    pub const PARSER: C = C::new(0);

    /// Coder errors category.
    pub const CODER: C = C::new(1);
}

/// Error kind constants.
pub mod kind {
    use super::category as c;
    use super::TbErrorKind as EK;

    /// Parser category error kind constants.
    pub mod parser {
        use super::c;
        use super::EK;

        /// Identifier contains unsupported characters.
        pub const BAD_IDENTIFIER_CHARACTERS: EK = EK::new(c::PARSER, 0);

        /// Identifier is specified in an unsupported case.
        pub const BAD_IDENTIFIER_CASE: EK = EK::new(c::PARSER, 1);

        /// Specification keyword is not a String.
        pub const BAD_KEYWORD_TYPE: EK = EK::new(c::PARSER, 2);

        /// Identifier is not valid on module-level.
        pub const BAD_MODULE_IDENTIFIER: EK = EK::new(c::PARSER, 3);

        /// Invalid name.
        pub const BAD_NAME: EK = EK::new(c::PARSER, 4);

        /// An object attribute is invalid.
        pub const BAD_OBJECT_ATTRIBUTE: EK = EK::new(c::PARSER, 5);

        /// Specification filename extension is not supported or is missing.
        pub const BAD_SPEC_FILE_EXTENSION: EK = EK::new(c::PARSER, 6);

        /// TOML deserialization has failed.
        pub const BAD_TOML: EK = EK::new(c::PARSER, 7);

        /// Specification contains an invalid root-level keyword.
        pub const BAD_ROOT_LEVEL_KEYWORD: EK = EK::new(c::PARSER, 8);

        /// Specification value type is invalid.
        pub const BAD_VALUE_TYPE: EK = EK::new(c::PARSER, 9);

        /// YAML deserialization has failed.
        pub const BAD_YAML: EK = EK::new(c::PARSER, 10);

        /// An identifier cannot be an empty string.
        pub const EMPTY_IDENTIFIER: EK = EK::new(c::PARSER, 11);

        /// Empty list of objects is not allowed.
        pub const EMPTY_LIST: EK = EK::new(c::PARSER, 12);

        /// Specification file couldn't be opened.
        pub const FAILED_TO_OPEN_SPEC_FILE: EK = EK::new(c::PARSER, 13);

        /// Specification lacks a mandatory attribute.
        pub const MISSING_ATTRIBUTE: EK = EK::new(c::PARSER, 14);

        /// Specification contains mutually exclusive keywords.
        pub const MUTUALLY_EXCLUSIVE_KEYWORDS: EK = EK::new(c::PARSER, 15);

        /// A name is not unique.
        pub const NON_UNIQUE_NAME: EK = EK::new(c::PARSER, 16);

        /// Specification file couldn't be found.
        pub const SPEC_FILE_NOT_FOUND: EK = EK::new(c::PARSER, 17);

        /// Collision of names between different items.
        pub const NAME_COLLISION: EK = EK::new(c::PARSER, 18);
    }

    /// Coder category error kind constants.
    pub mod coder {
        use super::c;
        use super::EK;

        /// At least one category must be defined.
        pub const CATEGORY_REQUIRED: EK = EK::new(c::CODER, 0);

        /// At least one error must be defined.
        pub const ERROR_REQUIRED: EK = EK::new(c::CODER, 1);

        /// Generated code tokens couldn't be parsed.
        pub const FAILED_TO_PARSE_TOKENS: EK = EK::new(c::CODER, 2);

        /// Output file couldn't be read.
        pub const FAILED_TO_READ_OUTPUT_FILE: EK = EK::new(c::CODER, 3);

        /// Output file couldn't be written.
        pub const FAILED_TO_WRITE_OUTPUT_FILE: EK = EK::new(c::CODER, 4);

        /// Rustfmt tool exited with an error.
        pub const RUSTFMT_FAILED: EK = EK::new(c::CODER, 5);

        /// Rustfmt tool isn't found.
        pub const RUSTFMT_NOT_FOUND: EK = EK::new(c::CODER, 6);

        /// The number of required bits exceeds the largest supported type u64.
        pub const TOO_MANY_BITS: EK = EK::new(c::CODER, 7);

        /// Output path is not a directory.
        pub const OUTPUT_PATH_NOT_DIRECTORY: EK = EK::new(c::CODER, 8);
    }
}