derive_getters/
faultmsg.rs

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
//! Error type.
use std::fmt;

#[derive(Debug)]
#[allow(dead_code)]
pub enum StructIs {
    Unnamed,
    Enum,
    Union,
    Unit,
}

impl fmt::Display for StructIs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Unnamed => write!(f, "an unnamed struct"),
            Self::Enum => write!(f, "an enum"),
            Self::Union => write!(f, "a union"),
            Self::Unit => write!(f, "a unit struct"),
        }
    }
}

// Almost an error type! But `syn` already has an error type so this just fills the
// `T: Display` part to avoid strings littering the source.
#[derive(Debug)]
#[allow(dead_code)]
pub enum Problem {
    NotNamedStruct(StructIs),
    UnnamedField,
    UnitStruct,
    InnerAttribute,
    EmptyAttribute,
    NoGrouping,
    NonParensGrouping,
    EmptyGrouping,
    TokensFollowSkip,
    TokensFollowCopy,
    TokensFollowNewName,
    InvalidAttribute,
    BotchedDocComment,
}

impl fmt::Display for Problem {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::NotNamedStruct(is) => {
                write!(f, "type must be a named struct, not {}", is)
            }
            Self::UnnamedField => write!(f, "struct fields must be named"),
            Self::UnitStruct => write!(f, "unit struct has nothing to dissolve"),
            Self::InnerAttribute => {
                write!(f, "attribute is an outer not inner attribute")
            }
            Self::EmptyAttribute => write!(f, "attribute has no tokens"),
            Self::NoGrouping => write!(f, "attribute tokens must be grouped"),
            Self::NonParensGrouping => {
                write!(f, "attribute tokens must be within parenthesis")
            }
            Self::EmptyGrouping => {
                write!(f, "no attribute tokens within parenthesis grouping")
            }
            Self::TokensFollowSkip => {
                write!(f, "tokens are not meant to follow skip attribute")
            }
            Self::TokensFollowCopy => {
                write!(f, "tokens are not meant to follow copy attribute")
            }
            Self::TokensFollowNewName => {
                write!(f, "no further tokens must follow new name")
            }
            Self::InvalidAttribute => {
                write!(f, "invalid attribute")
            }
            Self::BotchedDocComment => {
                write!(f, "Doc comment is botched")
            }
        }
    }
}