fidl_codec/
error.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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use nom::error::{ErrorKind, ParseError};
use serde_json::error::Error as SerdeError;
use std::result;
use std::str::Utf8Error;
use thiserror::Error;

/// A result for codec operations.
pub type Result<T> = result::Result<T, Error>;

/// Error type used by the codec.
#[derive(Debug, Error)]
pub enum Error {
    #[error("Error loading from FIDL JSON: {0}")]
    LibraryError(String),
    #[error("Could not parse FIDL JSON: {0}")]
    LibraryParseError(SerdeError),
    #[error("Encoding Error: {0}")]
    EncodeError(String),
    #[error("Decoding Error: {0}")]
    DecodeError(String),
    #[error("Value Error: {0}")]
    ValueError(String),
    #[error("UTF-8 Decoding error: {0}")]
    Utf8Error(Utf8Error),
    #[error("RecursionLimitExceeded")]
    RecursionLimitExceeded,
}

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

impl From<Error> for nom::Err<Error> {
    fn from(error: Error) -> Self {
        nom::Err::Failure(error)
    }
}

impl From<nom::Err<Error>> for Error {
    fn from(error: nom::Err<Error>) -> Self {
        match error {
            nom::Err::Incomplete(_) => Error::DecodeError("<Parsing Incomplete>".to_owned()),
            nom::Err::Error(x) | nom::Err::Failure(x) => x.into(),
        }
    }
}

impl ParseError<&[u8]> for Error {
    fn from_error_kind(_: &[u8], kind: ErrorKind) -> Self {
        Error::DecodeError(format!("Nom encountered an error (kind: {})", kind.description()))
    }

    fn append(_: &[u8], kind: ErrorKind, other: Self) -> Self {
        Error::DecodeError(format!(
            "Nom encountered an error (kind: {}) caused by: {}",
            kind.description(),
            other
        ))
    }
}