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
// Copyright 2017 sacn Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use core::str::Utf8Error;
use core::fmt;

#[cfg(feature = "std")]
use std::error::Error;

use uuid::ParseError as UUidParseError;

/// Error for parsing of sACN network packets.
#[derive(Debug)]
pub enum ParseError<'a> {
    Uuid(UUidParseError),
    Utf8(Utf8Error),
    PduInvalidLength(usize),
    PduInvalidFlags(u8),
    PduVectorNotSupported(u32),
    OtherInvalidData(&'a str),
}

impl<'a> fmt::Display for ParseError<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ParseError::Uuid(ref err) => write!(f, "UUID parsing error: {}", err),
            ParseError::Utf8(ref err) => write!(f, "UTF8 error: {}", err),
            ParseError::PduInvalidLength(len) => write!(f, "Length {} is invalid", len),
            ParseError::PduInvalidFlags(flags) => write!(f, "Flags {} are invalid", flags),
            ParseError::PduVectorNotSupported(vec) => write!(f, "Vector {} not supported", vec),
            ParseError::OtherInvalidData(ref msg) => write!(f, "Invalid data: {}", msg),
        }
    }
}

#[cfg(feature = "std")]
impl<'a> Error for ParseError<'a> {
    fn description(&self) -> &str {
        match *self {
            ParseError::Uuid(ref err) => err.description(),
            ParseError::Utf8(ref err) => err.description(),
            ParseError::PduInvalidLength(_) => "PDU invalid length",
            ParseError::PduInvalidFlags(_) => "PDU invalid flags",
            ParseError::PduVectorNotSupported(_) => "PDU vector not supported",
            ParseError::OtherInvalidData(ref msg) => msg,
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            ParseError::Uuid(ref err) => Some(err),
            ParseError::Utf8(ref err) => Some(err),
            ParseError::PduInvalidLength(_) => None,
            ParseError::PduInvalidFlags(_) => None,
            ParseError::PduVectorNotSupported(_) => None,
            ParseError::OtherInvalidData(_) => None,
        }
    }
}

impl<'a> From<UUidParseError> for ParseError<'a> {
    fn from(err: UUidParseError) -> ParseError<'a> {
        ParseError::Uuid(err)
    }
}

impl<'a> From<Utf8Error> for ParseError<'a> {
    fn from(err: Utf8Error) -> ParseError<'a> {
        ParseError::Utf8(err)
    }
}