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
use std::io::Error as IOError;

use failure::Fail;

/// An error type for errors while generating config struct modules.
///
/// Errors can either occur during IO (when reading or creating files) or during
/// the generation itself.
#[derive(Debug, Fail)]
pub enum Error {
    #[fail(display = "Generation error: {}", _0)]
    Generation(#[cause] GenerationError),

    #[fail(display = "IO error: {}", _0)]
    IO(#[cause] IOError),
}

/// An error occurring during code generation.
#[derive(Debug, Fail)]
pub enum GenerationError {
    /// Occurs when the config format can't be determined from the
    /// filename extension of the input file.
    #[fail(
        display = "Unknown input format: `{}`. (Maybe you need to enable the right feature?)",
        _0
    )]
    UnknownInputFormat(String),

    /// Occurs when encountering a field in the config which is not a
    /// valid name for a struct field.
    #[fail(display = "Invalid field name: `{}`.", _0)]
    InvalidFieldName(String),

    /// Occurs when encountering a field in the config which is not a
    /// valid name for an enum variant.
    #[fail(display = "Invalid variant name: `{}`.", _0)]
    InvalidVariantName(String),

    /// Occurs when an array in the config file contains multiple different types
    /// of data, which cannot be represented in a Rust struct.
    #[fail(
        display = "Array under key `{}` has elements of different types. Arrays must be homogenous.",
        _0
    )]
    HeterogenousArray(String),

    /// Occurs when generating from source and not a file, if attempting to also
    /// generate dynamic loading functions.
    ///
    /// Because no input filepath was given, it's impossible to generate a function
    /// which loads from that file.
    #[fail(
        display = "Cannot generate dynamic loading functions without a filename.
(Generate struct from a file, set generate_load_fns: false, or set dynamic_loading: DynamicLoading::Never to fix.)"
    )]
    MissingFilePath,

    /// Occurs when the config file could not be correctly parsed.
    #[fail(display = "Deserialization failed: {}", _0)]
    DeserializationFailed(String),

    /// Occurs when invalid options were provided.
    #[fail(display = "Invalid options error: {}", _0)]
    StructOptions(#[cause] OptionsError),
}

/// An error type for when a [`StructOptions`](struct.StructOptions.html) value
/// failed validation.
#[derive(Debug, Fail)]
pub enum OptionsError {
    /// Occurs when the provided `struct_name` is not a valid Rust identifier.
    #[fail(display = "Invalid name for a struct: `{}`.", _0)]
    InvalidStructName(String),

    /// Occurs when the provided `const_name` is not a valid Rust identifier.
    #[fail(display = "Invalid name for a const: `{}`.", _0)]
    InvalidConstName(String),
}

impl From<GenerationError> for Error {
    fn from(error: GenerationError) -> Self {
        Error::Generation(error)
    }
}

impl From<IOError> for Error {
    fn from(error: IOError) -> Self {
        Error::IO(error)
    }
}

impl From<OptionsError> for GenerationError {
    fn from(error: OptionsError) -> Self {
        GenerationError::StructOptions(error)
    }
}