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
use std::fmt::{self, Write};
use self::super::parse;
use std::error::Error;
use lazysort::Sorted;


/// Generic error type, encompassing more precise errors.
///
/// # Examples
///
/// ```
/// # use hrx::{HrxArchive, HrxError};
/// # use hrx::parse::ParseError;
/// # use std::str::FromStr;
/// assert_eq!(HrxArchive::from_str("Not an actual archive, missing a boundary"),
///            Err(HrxError::NoBoundary));
///
/// let err = HrxArchive::from_str("<====>no space before").unwrap_err();
/// assert_eq!(err, HrxError::Parse(ParseError {
///     line: 1,
///     column: 7,
///     offset: 6,
///     expected: vec![" ", "\n"].into_iter().collect(),
/// }));
/// assert_eq!(err.to_string(), r#"Parse failed at 1:7 [position 6]: expected "\n", or " "."#);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HrxError {
    /// No valid HRX boundary found
    NoBoundary,
    /// An error occured during parsing
    Parse(parse::ParseError),
    /// Some `body`s were made to contain the archive boundary. Deserialising the archive wouldn't work as expected
    BodyContainsBoundary(Vec<ErroneousBodyPath>),
    /// Two entries share the same path
    DuplicateEntry(String),
    /// An entry attempted to use a file as a directory
    FileAsDirectory(String, String),
}

/// A path to a `body` which contains an invalid sequence
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum ErroneousBodyPath {
    /// The root archive comment
    RootComment,
    /// A comment to the entry with the specified path
    EntryComment(String),
    /// The data of the entry with the specified path
    EntryData(String),
}


impl From<parse::ParseError> for HrxError {
    fn from(pe: parse::ParseError) -> HrxError {
        HrxError::Parse(pe)
    }
}

impl From<ErroneousBodyPath> for HrxError {
   fn from(bcb: ErroneousBodyPath) -> HrxError {
       HrxError::BodyContainsBoundary(vec![bcb])
   }
}

impl<V: Into<Vec<ErroneousBodyPath>>> From<V> for HrxError {
    fn from(paths: V) -> HrxError {
        HrxError::BodyContainsBoundary(paths.into())
    }
}

impl fmt::Display for HrxError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &HrxError::NoBoundary => fmt.write_str("No boundary found")?,
            &HrxError::Parse(ref pe) => {
                write!(fmt, "Parse failed at {}:{} [position {}]: expected ", pe.line, pe.column, pe.offset)?;

                for (i, x) in pe.expected.iter().sorted().enumerate() {
                    if i != 0 {
                        fmt.write_str(", ")?;

                        if i == pe.expected.len() - 1 {
                            fmt.write_str("or ")?;
                        }
                    }

                    if x.len() == 1 {
                        write!(fmt, "{:?}", x)?;
                    } else {
                        fmt.write_str(x)?;
                    }
                }

                fmt.write_str(".")?;
            }
            &HrxError::BodyContainsBoundary(ref paths) => {
                fn first_char(fmt: &mut fmt::Formatter, c: char, first: bool, last: bool) -> fmt::Result {
                    if first {
                        write!(fmt, "{}", c.to_uppercase())?
                    } else {
                        fmt.write_str(", ")?;
                        if last {
                            fmt.write_str("and ")?;
                        }
                        fmt.write_char(c)?;
                    }

                    Ok(())
                }


                if !paths.is_empty() {
                    for (i, path) in paths.iter().enumerate() {
                        let first = i == 0;
                        let last = i == paths.len();
                        match path {
                            ErroneousBodyPath::RootComment => {
                                first_char(fmt, 'r', first, last)?;
                                fmt.write_str("oot archive comment")?
                            }
                            ErroneousBodyPath::EntryComment(ref pp) => {
                                first_char(fmt, 'c', first, last)?;
                                write!(fmt, "omment for \"{}\" entry", pp)?
                            }
                            ErroneousBodyPath::EntryData(ref pp) => {
                                first_char(fmt, 'd', first, last)?;
                                write!(fmt, "ata of \"{}\" entry", pp)?
                            }
                        }
                    }
                    fmt.write_str(" contain")?;
                    if paths.len() != 1 {
                        fmt.write_char('s')?;
                    }
                    fmt.write_str(" the archive boundary, making resulting archive not redeserialisable.")?;
                } else {
                    fmt.write_str("No paths specified.")?;
                }
            }
            &HrxError::DuplicateEntry(ref path) => {
                fmt.write_str("Duplicate entry: ")?;
                fmt.write_str(&path)?;
            }
            &HrxError::FileAsDirectory(ref file, ref who) => {
                fmt.write_str(&who)?;
                fmt.write_str(" attempted to use the file at ")?;
                fmt.write_str(&file)?;
                fmt.write_str(" as a directrory.")?;
            }
        }

        Ok(())
    }
}

impl Error for HrxError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            &HrxError::Parse(ref pe) => Some(pe),
            _ => None,
        }
    }
}