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
//! Structured Errors

use num_traits::PrimInt as Integer;

use crate::Builtin;
use crate::r8vm::{ArgSpec, RuntimeError, Traceback, TraceFrame};
use crate::nkgc::SymID;
use crate::fmt::LispFmt;
use std::backtrace::Backtrace;
use std::borrow::Cow;
use std::mem::{discriminant, replace};
use std::error;
use std::fmt::{self, Debug, Display, Write};
use std::num::TryFromIntError;
use std::sync::Arc;
use std::sync::mpsc::SendError;

pub type SourceFileName = Option<Cow<'static, str>>;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Source {
    pub file: SourceFileName,
    pub line: u32,
    pub col: u32,
}

impl Source {
    pub fn new(line: u32, col: u32, file: SourceFileName) -> Source {
        Source { file, line, col }
    }

    pub fn none() -> Source {
        Source { file: None, line: 0, col: 0 }
    }

    pub fn with_file(mut self, file: String) -> Source {
        self.file = Some(Cow::from(file));
        self
    }

    pub fn is_none(&self) -> bool {
        self.line == 0
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceRef<'a> {
    pub file: Option<&'a str>,
    pub line: u32,
    pub col: u32,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum OpName {
    OpSym(SymID),
    OpStr(&'static str),
    OpBt(Builtin),
}

impl OpName {
    fn name(&self) -> &str {
        match self {
            OpName::OpStr(s) => s,
            OpName::OpSym(s) => s.as_ref(),
            OpName::OpBt(s) => s.as_str(),
        }
    }
}

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

#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub struct LineCol {
    pub line: u32,
    pub col: u32,
}

impl LineCol {
    pub fn into_source(self, file: SourceFileName) -> Source {
        Source { file, line: self.line, col: self.col }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum Meta {
    Op(OpName),
    OpArgn(u32),
    OpArgName(OpName),
    VarName(OpName),
    SourceFile(Cow<'static, str>),
    Source(LineCol),
    Related(Option<OpName>, Source),
}

#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct MetaSet {
    meta: Vec<Meta>,
}

macro_rules! get_inner_meta {
    ($name:ident, $meta_name:ident, $inner_t:ty) => {
        #[allow(dead_code)]
        fn $name(&self) -> Option<$inner_t> {
            self.meta.iter().find_map(|m| if let Meta::$meta_name(name) = m {
                Some(name)
            } else {
                None
            }).cloned()
        }
    }
}

impl MetaSet {
    /// Add metadata which should replace previous metadata of the same type.
    ///
    /// # Returns
    /// Returns the previous metadata, if it exists.
    fn amend(&mut self, data: Meta) -> Option<Meta> {
        let pos = self.meta.iter()
                           .position(|m| discriminant(m) == discriminant(&data));
        if let Some(idx) = pos {
            Some(replace(&mut self.meta[idx], data))
        } else {
            self.meta.push(data);
            None
        }
    }

    /// Add metadata which should function as a fallback, but should not replace
    /// metadata of the same kind if it exists.
    fn fallback(&mut self, data: Meta) {
        if !self.meta.iter().any(|m| discriminant(m) == discriminant(&data)) {
            self.meta.push(data);
        }
    }

    get_inner_meta!(op, Op, OpName);
    get_inner_meta!(op_argn, OpArgn, u32);
    get_inner_meta!(op_arg_name, OpArgName, OpName);
    get_inner_meta!(src_line_col, Source, LineCol);
    get_inner_meta!(src_file, SourceFile, Cow<'static, str>);
    get_inner_meta!(var_name, VarName, OpName);

    fn src(&self) -> Option<Source> {
        let line_col = self.src_line_col()?;
        let file = self.src_file();
        Some(Source {
            file,
            line: line_col.line,
            col: line_col.col,
        })
    }
}

pub struct FmtArgnOp<'a> {
    pre: &'static str,
    post: &'static str,
    meta: &'a MetaSet,
}

impl fmt::Display for FmtArgnOp<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(op) = self.meta.op() {
            write!(f, "{}", self.pre)?;
            if let Some(argn) = self.meta.op_argn() {
                write!(f, "for argument {argn} of ({op} ...)")?;
            } else {
                write!(f, "in {op}")?;
            }
            write!(f, "{}", self.post)?;
        } else if let Some(var) = self.meta.var_name() {
            write!(f, "{}for special variable `{}'{}",
                   self.pre, var, self.post)?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum SyntaxErrorKind {
    DotAtEndOfList,
    DotAfterDot,
    SpliceAfterDot,
    ModifierBeforeDot,
}

impl fmt::Display for SyntaxErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SyntaxErrorKind::DotAtEndOfList =>
                write!(f, "Dot [.] operator at end of list"),
            SyntaxErrorKind::DotAfterDot =>
                write!(f, "Dot [.] operator immediately after dot [.] operator"),
            SyntaxErrorKind::SpliceAfterDot =>
                write!(f, "Splice operator [,@] after dot operator [.]"),
            SyntaxErrorKind::ModifierBeforeDot =>
                write!(f, "Modifier e.g [,@] [,] [`] or ['] etc. before dot [.] operator e.g (a b ' . c) instead of (a b . 'c)"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ErrorKind {
    SendError { obj_dbg: String },
    STypeError { expect: String, got: String },
    UnexpectedDottedList,
    TypeError { expect: Builtin, got: Builtin },
    TypeNError { expect: Vec<Builtin>, got: Builtin },
    ArgTypeError { expect: Vec<Builtin>, got: Vec<Builtin> },
    IfaceNotImplemented { got: Vec<SymID> },
    EnumError { expect: Vec<SymID>, got: SymID },
    ArgError { expect: ArgSpec, got_num: u32 },
    OutsideContext { op: Builtin, ctx: Builtin },
    SyntaxErrorMsg { msg: String },
    LinkError { dst: String, src: usize },
    ConversionError { from: &'static str,
                      to: &'static str,
                      val: String },
    NotEnough { expect: usize,
                got: usize },
    SomeError { msg: String },
    UndefinedFunction { name: SymID },
    UndefinedVariable { var: SymID },
    ModuleLoadError { lib: SymID },
    ModuleNotFound { lib: SymID },
    Unsupported { op: &'static str },
    Traceback { tb: Box<Traceback> },
    IndexError { idx: usize },
    Exit { status: SymID },
    IOError { kind: std::io::ErrorKind },
    MissingFeature { flag: &'static str },
    CharSpecError { spec: SymID },
    LibError { name: SymID },
    TrailingDelimiter { close: &'static str },
    UnclosedDelimiter { open: &'static str },
    TrailingModifiers { mods: String },
    TrailingEscape,
    NoSuchEscapeChar { chr: char },
    UnterminatedString,
    MacroexpandRecursionLimit { lim: usize },
    SyntaxError(SyntaxErrorKind),
    IDError { id: usize },
    None,
}

impl From<std::io::Error> for Error {
    fn from(v: std::io::Error) -> Self {
        Error::new(ErrorKind::IOError { kind: v.kind() })
    }
}

impl From<ErrorKind> for Error {
    fn from(v: ErrorKind) -> Self {
        Error::new(v)
    }
}

impl From<Traceback> for Error {
    fn from(v: Traceback) -> Self {
        let src = v.err.meta().src();
        let e = Error::new(ErrorKind::Traceback { tb: Box::new(v) });
        if let Some(src) = src {
            e.src(src)
        } else {
            e
        }
    }
}

impl From<String> for Error {
    fn from(msg: String) -> Self {
        Error::new(ErrorKind::SomeError { msg })
    }
}

impl From<&str> for Error {
    fn from(msg: &str) -> Self {
        Error::new(ErrorKind::SomeError { msg: msg.to_string() })
    }
}

impl From<RuntimeError> for Error {
    fn from(v: RuntimeError) -> Self {
        Error::new(ErrorKind::SomeError { msg: v.msg })
    }
}

impl From<Error> for RuntimeError {
    fn from(v: Error) -> Self {
        let msg = format!("{}", &v);
        RuntimeError { line: v.meta().src().map(|src| src.line).unwrap_or(0),
                       msg }
    }
}

impl From<ErrorKind> for RuntimeError {
    fn from(v: ErrorKind) -> Self {
        let v: Error = v.into();
        v.into()
    }
}

/// Structural Error Type
#[derive(Debug, Clone)]
pub struct Error {
    inner: ErrorInner
}

#[derive(Debug, Clone)]
struct ErrorInner {
    meta: MetaSet,
    ty: ErrorKind,
    rust_trace: Arc<Backtrace>,
}

impl Error {
    pub fn new(kind: ErrorKind) -> Error {
        Error {
            inner: (
                ErrorInner { meta: Default::default(),
                             ty: kind,
                             rust_trace: Arc::new(Backtrace::capture()) }
            )
        }
    }

    pub fn is_none(&self) -> bool {
        matches!(self.inner.ty, ErrorKind::None)
    }

    pub fn backtrace(&self) -> Arc<Backtrace> {
        self.inner.rust_trace.clone()
    }

    pub fn kind(&self) -> &ErrorKind {
        &self.inner.ty
    }

    pub fn meta(&self) -> &MetaSet {
        &self.inner.meta
    }
}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        self.inner.meta == other.inner.meta && self.inner.ty == other.inner.ty
    }
}

pub trait JoinIt {
    fn join(self, sep: impl AsRef<str>) -> String;
}

impl<T, K> JoinIt for K where K: Iterator<Item = T>, T: Display {
    fn join(mut self, sep: impl AsRef<str>) -> String {
        let mut s = String::new();
        let Some(p) = self.next() else { return s };
        write!(s, "{p}").unwrap();
        for p in self {
            write!(s, "{}{p}", sep.as_ref()).unwrap();
        }
        s
    }
}

struct OneOf<'a>(&'a [Builtin]);

impl Display for OneOf<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0.len() == 1 {
            write!(f, "{}", self.0[0])
        } else {
            write!(f, "one of {}", self.0.iter().map(|bt| bt.as_str()).join(", "))
        }
    }
}

fn fmt_error(err: &Error, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    use ErrorKind::*;

    fn plurs(num: impl Integer) -> &'static str {
        if num.is_one() {""} else {"s"}
    }

    let meta = err.meta();
    match err.kind() {
        TypeError { expect, got } =>
            write!(f, "Type Error: Expected {} {}but got {}",
                   expect.as_str(),
                   FmtArgnOp { pre: "", post: ", ", meta },
                   got.as_str())?,
        STypeError { expect, got } =>
            write!(f, "Type Error: Expected {} {}but got {}",
                   expect,
                   FmtArgnOp { pre: "", post: ", ", meta },
                   got)?,
        TypeNError { expect, got } =>
            write!(f, "Type Error: Expected {} {}but got {}",
                   OneOf(expect),
                   FmtArgnOp { pre: "", post: ", ", meta },
                   got.as_str())?,
        EnumError { expect, got } =>
            write!(f, "Type Error: Expected {:?} {}but got {}",
                   expect, FmtArgnOp { pre: "", post: ", ", meta }, *got)?,
        UnexpectedDottedList => {
            write!(f, "Type Error: Unexpected dotted list")?;
            if let Some(op) = meta.op() {
                write!(f, " given to {}", op.name())?
            }
        }
        ArgError { expect, got_num } => {
            write!(f, "Argument Error: ")?;
            if let Some(op) = meta.op() {
                write!(f, "{} ", op.name())?
            }
            match expect {
                ArgSpec { nargs, nopt: 0, rest: false, .. } =>
                    write!(f, "expected {} argument{}, but got {}",
                            nargs, plurs(*got_num), got_num)?,
                ArgSpec { nargs, nopt, rest: false, .. } =>
                    write!(f, "expected from {} to {} argument{}, but got {}",
                            nargs, nargs+nopt, plurs(*got_num), got_num)?,
                ArgSpec { nargs, rest: true, .. } =>
                    write!(f, "expected at least {} argument{}, but got {}",
                            nargs, plurs(*got_num), got_num)?,
            }
        }
        IfaceNotImplemented { got } => {
            write!(f, "Operation Not Supported: (")?;
            if let Some(op) = meta.op() {
                write!(f, "{}", op.name())?;
            } else {
                write!(f, "?")?;
            }
            for arg in got.iter() {
                write!(f, " {arg}")?;
            }
            write!(f, ")")?;
        }
        ArgTypeError {  expect, got } => {
            write!(f, "Argument Error: ")?;
            if let Some(op) = meta.op() {
                write!(f, "{} ", op.name())?
            }
            write!(f, "expected ({}) but got ({})",
                   expect.iter().map(|s| s.as_str()).join(" "),
                   got.iter().map(|s| s.as_str()).join(" "))?;
        }
        OutsideContext { op, ctx } =>
            write!(f, "Syntax Error: Operator {op} not allowed outside of {ctx} context")?,
        SyntaxErrorMsg { msg } =>
            write!(f, "Syntax Error: {msg}")?,
        ConversionError { from, to, val } =>
            write!(f, "Conversion Error: Could not convert the {from} value `{val}' into {to}")?,
        NotEnough { expect, got } => {
            write!(f, "Stack Error: ")?;
            if let Some(op) = meta.op() {
                write!(f, "Operation `{}' ", op.name())?
            }
            let s = plurs(*got);
            write!(f, "expected {expect} stack element{s}, but got {got}")?;
        }
        SomeError { msg } => write!(f, "Error: {}", msg)?,
        UndefinedFunction { name } =>
            write!(f, "Undefined Function: Virtual call to undefined function {name}")?,
        UndefinedVariable { var } =>
            write!(f, "Undefined Variable: {var}")?,
        Unsupported { op } =>
            write!(f, "Unsupported operation: {}", op)?,
        ModuleLoadError { lib } =>
            write!(f, "Module Error: Unable to load module {lib}")?,
        ModuleNotFound { lib } =>
            write!(f, "Module Not Found: Could not find {lib}, check sys/load-path")?,
        ErrorKind::Traceback { tb } => {
            writeln!(f, "Traceback:")?;
            for TraceFrame { src, func, args } in tb.frames.iter() {
                write!(f, "  - ({func}")?;
                let mut it = args.iter().peekable();
                if it.peek().is_some() {
                    write!(f, " ")?;
                }
                while let Some(arg) = it.next() {
                    write!(f, "{}", arg.lisp_to_string())?;
                    if it.peek().is_some() {
                        write!(f, " ")?;
                    }
                }
                write!(f, ")")?;
                writeln!(f, " {}", src)?;
            }
            return fmt_error(&tb.err, f)
        },
        ErrorKind::IndexError { idx } =>
            write!(f, "Index Error: No such index {idx}")?,
        ErrorKind::Exit { status } =>
            write!(f, "Exit: {status}")?,
        ErrorKind::IOError { kind } => {
            let err: std::io::Error = (*kind).into();
            write!(f, "IOError: {}", err)?;
        }
        CharSpecError { spec } =>
            write!(f, "Invalid char spec `{spec}', use exactly one character in the symbol")?,
        LibError { name } =>
            write!(f, "Error: {name}")?,
        TrailingDelimiter { close } =>
            write!(f, "Trailing Delimiter: Found trailing `{close}' in input")?,
        UnclosedDelimiter { open } =>
            write!(f, "Unclosed Delimiter: Found `{open}' which was not closed in input")?,
        TrailingModifiers { mods } =>
            write!(f, "Trailing Modifiers: Unexpected end of input at: {mods}")?,
        MacroexpandRecursionLimit { lim } =>
            write!(f, "Macro Recursion Error: Macro expansion was recursive beyond {lim} levels")?,
        None => write!(f, "")?,
        SendError { obj_dbg } =>
            write!(f, "Send Error: {obj_dbg}")?,
        LinkError { dst, src: _ } =>
            write!(f, "Link Error: Symbol not found {dst}")?,
        MissingFeature { flag } =>
            write!(f, "Missing Feature: The {flag} feature was not enabled for this version of SPAIK")?,
        SyntaxError(kind) =>
            write!(f, "Syntax Error: {kind}")?,
        IDError { id } =>
            write!(f, "ID Error: id number {id} was out of range for enum")?,
        UnterminatedString =>
            write!(f, "Syntax Error: Unterminated string")?,
        TrailingEscape =>
            write!(f, "Syntax Error: Trailing escape character")?,
        NoSuchEscapeChar { chr } =>
            write!(f, "Syntax Error: No such escape character {chr:?}")?,
    }

    if let Some(src) = meta.src() {
        write!(f, " {}", src)?;
    }
    Ok(())
}

impl Error {
    pub fn src(mut self, src: Source) -> Error {
        self.inner.meta.amend(Meta::Source(LineCol {
            line: src.line,
            col: src.col
        }));
        if let Some(file) = src.file {
            self.inner.meta.amend(Meta::SourceFile(file));
        }
        self
    }

    pub fn see_also(mut self, what: &'static str, src: Source) -> Self {
        self.inner.meta.amend(Meta::Related(Some(OpName::OpStr(what)), src));
        self
    }

    pub fn see_also_sym(mut self, what: SymID, src: Source) -> Self {
        self.inner.meta.amend(Meta::Related(Some(OpName::OpSym(what)), src));
        self
    }

    pub fn amend(mut self, meta: Meta) -> Self {
        self.inner.meta.amend(meta);
        self
    }

    pub fn fallback(mut self, meta: Meta) -> Self {
        self.inner.meta.fallback(meta);
        self
    }

    pub fn fop(mut self, new_op: SymID) -> Error {
        self.inner.meta.fallback(Meta::Op(OpName::OpSym(new_op)));
        self
    }

    pub fn bop(mut self, new_op: Builtin) -> Error {
        self.inner.meta.amend(Meta::Op(OpName::OpBt(new_op)));
        self
    }

    pub fn op(mut self, new_op: SymID) -> Error {
        self.inner.meta.amend(Meta::Op(OpName::OpSym(new_op)));
        self
    }

    pub fn sop(mut self, new_op: &'static str) -> Error {
        self.inner.meta.amend(Meta::Op(OpName::OpStr(new_op)));
        self
    }

    pub fn argn(mut self, n: u32) -> Error {
        self.inner.meta.amend(Meta::OpArgn(n));
        self
    }

    pub fn cause(&self) -> &Error {
        match self.kind() {
            ErrorKind::Traceback { tb } => {
                &tb.err
            },
            _ => self
        }
    }
}

impl serde::ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::new(ErrorKind::SomeError { msg: msg.to_string() })
    }
}

impl serde::de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::new(ErrorKind::SomeError { msg: msg.to_string() })
    }
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt_error(&Error::new(self.clone()), f)
    }
}

impl fmt::Display for Source {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[")?;
        if self.line == 0 {
            write!(f, "unknown")?;
        } else {
            if let Some(file) = &self.file {
                write!(f, "{} ", file)?;
            }
            write!(f, "{}:{}", self.line, self.col)?;
        }
        write!(f, "]")
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt_error(self, f)
    }
}

impl error::Error for ErrorKind {}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        Some(self.kind())
    }
}

impl<T> From<SendError<T>> for Error where T: Debug {
    fn from(err: SendError<T>) -> Self {
        Error::new(ErrorKind::SendError {
            obj_dbg: format!("{:?}", err.0)
        })
    }
}

impl From<TryFromIntError> for Error {
    fn from(err: TryFromIntError) -> Self {
        Error::new(ErrorKind::SomeError { msg: err.to_string() })
    }
}

impl From<std::convert::Infallible> for Error {
    fn from(_: std::convert::Infallible) -> Self {
        unreachable!();
    }
}

macro_rules! err {
    ($kind:ident, $($init:tt)* ) => {
        Err((crate::error::ErrorKind::$kind { $($init)* }).into())
    };
}

macro_rules! bail {
    ($kind:ident $($init:tt)*) => {
        return Err((crate::error::ErrorKind::$kind  $($init)* ).into())
    };
}

macro_rules! error {
    ($kind:ident, $($init:tt)* ) => {
        crate::error::Error::new(crate::error::ErrorKind::$kind { $($init)* })
    };
}

macro_rules! err_src {
    ($src:expr, $kind:ident, $($init:tt)* ) => {
        Err(crate::error::Error::new(
            (crate::error::ErrorKind::$kind { $($init)* })
        ).src($src))
    };
}

macro_rules! error_src {
    ($src:expr, $kind:ident, $($init:tt)* ) => {
        crate::error::Error::new(
            (crate::error::ErrorKind::$kind { $($init)* }),
        ).src($src)
    };
}