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
use std::fmt;
use std::fmt::{Formatter, Display};

/// This represents an error from the library.
#[derive(Debug)]
pub enum Error {
    /// A wrapped error from the Syn library, caused when
    /// parsing the source Rust code.
    SynError(syn::Error),
    /// Represents a feature of the source Rust code that cannot
    /// be converted to C#. The first item is the error message,
    /// and the second is the identifier in the source Rust
    /// code that caused the error.
    UnsupportedError(String, Option<syn::Ident>)
}

pub(crate) type Result<T> = std::result::Result<T, Error>;

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Error::SynError(err) => {
                write!(f, "Couldn't parse Rust code: {}", err)
            },
            Error::UnsupportedError(reason, maybe_ident) => {
                let loc = if let Some(ident) = maybe_ident {
                    format!(" while processing symbol \"{}\"", ident.to_string())
                } else {
                    String::from("")
                };
                write!(f, "Unable to export C# code{} because {}", loc, reason)
            }
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::SynError(ref err) => {
                Some(err)
            },
            Error::UnsupportedError(_, _) => {
                None
            }
        }
    }
}

pub(crate) fn add_ident<T>(result: Result<T>, ident: &syn::Ident) -> Result<T> {
    match result {
        Err(Error::UnsupportedError(reason, None)) => {
            Err(Error::UnsupportedError(reason, Some(ident.clone())))
        },
        _ => result
    }
}