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
//! When serializing or deserializing MTProto goes wrong.

use std::fmt;

use serde::{ser, de};


error_chain! {
    foreign_links {
        Io(::std::io::Error);
        FromUtf8(::std::string::FromUtf8Error);
    }

    errors {
        Ser(kind: SerErrorKind) {
            description("serialization error in serde_mtproto")
            display("serialization error in serde_mtproto: {}", kind)
        }

        De(kind: DeErrorKind) {
            description("deserialization error in serde_mtproto")
            display("deserialization error in serde_mtproto: {}", kind)
        }

        IntegerCast {
            description("error while casting an integer")
            display("error while casting an integer")
        }

        StringTooLong(len: usize) {
            description("string is too long to serialize")
            display("string of length {} is too long to serialize", len)
        }

        ByteSeqTooLong(len: usize) {
            description("byte sequence is too long to serialize")
            display("byte sequence of length {} is too long to serialize", len)
        }

        SeqTooLong(len: usize) {
            description("sequence is too long to serialize")
            display("sequence of length {} is too long to serialize", len)
        }
    }
}


/// Serialization error kinds.
#[derive(Debug)]
pub enum SerErrorKind {
    Msg(String),
    ExcessElements(u32),
    MapsWithUnknownLengthUnsupported,
    NotEnoughElements(u32, u32),
    SeqsWithUnknownLengthUnsupported,
    StringTooLong(usize),
    UnsupportedSerdeType(SerSerdeType),
}

impl fmt::Display for SerErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SerErrorKind::Msg(ref string) => {
                write!(f, "custom string: {}", string)
            },
            SerErrorKind::ExcessElements(len) => {
                write!(f, "excess elements, need no more than {}", len)
            },
            SerErrorKind::MapsWithUnknownLengthUnsupported => {
                write!(f, "maps with ahead-of-time unknown length are not supported")
            },
            SerErrorKind::NotEnoughElements(unexpected_len, expected_len) => {
                write!(f, "not enough elements: have {}, need {}", unexpected_len, expected_len)
            },
            SerErrorKind::SeqsWithUnknownLengthUnsupported => {
                write!(f, "seqs with ahead-of-time unknown length are not supported")
            },
            SerErrorKind::StringTooLong(len) => {
                write!(f, "string of length {} is too long to serialize", len)
            },
            SerErrorKind::UnsupportedSerdeType(ref type_) => {
                write!(f, "{} type is not supported for serialization", type_)
            },
        }
    }
}

/// Serde serialization data types that are not supported by `serde_mtproto`.
#[derive(Debug)]
pub enum SerSerdeType { Char, None, Some, Unit }

impl fmt::Display for SerSerdeType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let repr = match *self {
            SerSerdeType::Char => "char",
            SerSerdeType::None => "none",
            SerSerdeType::Some => "some",
            SerSerdeType::Unit => "unit",
        };

        f.write_str(repr)
    }
}

impl From<SerErrorKind> for Error {
    fn from(kind: SerErrorKind) -> Error {
        ErrorKind::Ser(kind).into()
    }
}


/// Deserialization error kinds.
#[derive(Debug)]
pub enum DeErrorKind {
    Msg(String),
    UnsupportedSerdeType(DeSerdeType),
}

impl fmt::Display for DeErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DeErrorKind::Msg(ref string) => {
                write!(f, "custom string: {}", string)
            },
            DeErrorKind::UnsupportedSerdeType(ref type_) => {
                write!(f, "{} type is not supported for deserialization", type_)
            },
        }
    }
}

/// Serde deserialization data types that are not supported by `serde_mtproto`.
#[derive(Debug)]
pub enum DeSerdeType { Any, Char, Option, Unit, IgnoredAny }

impl fmt::Display for DeSerdeType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let repr = match *self {
            DeSerdeType::Any => "any",
            DeSerdeType::Char => "char",
            DeSerdeType::Option => "option",
            DeSerdeType::Unit => "unit",
            DeSerdeType::IgnoredAny => "ignored_any",
        };

        f.write_str(repr)
    }
}

impl From<DeErrorKind> for Error {
    fn from(kind: DeErrorKind) -> Error {
        ErrorKind::De(kind).into()
    }
}


impl ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Error {
        SerErrorKind::Msg(msg.to_string()).into()
    }
}

impl de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Error {
        DeErrorKind::Msg(msg.to_string()).into()
    }
}