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
use std::convert::TryFrom;
use std::fmt;
use std::fs::File;
use std::io::{BufReader, Error as IoError, Read};
use std::path::Path;
use thiserror::Error;
use super::core::PackIndexFile;
use super::parser;
use nom::{self, error::ErrorKind, Err as NomErr};
#[derive(Debug, Error)]
pub enum LoadError {
FileOpen(#[source] IoError),
Read(#[source] IoError),
Incomplete,
ParseError(ErrorKind),
ParseFailure(ErrorKind),
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LoadError::FileOpen(_) => write!(f, "Failed to open file"),
LoadError::Read(_) => write!(f, "Failed to read file"),
LoadError::Incomplete => write!(f, "Unexpected EOF"),
LoadError::ParseError(e) => write!(f, "File not recognized: {:?}", e),
LoadError::ParseFailure(e) => write!(f, "File corrupt: {:?}", e),
}
}
}
type LoadResult<T> = Result<T, LoadError>;
impl From<NomErr<nom::error::Error<&[u8]>>> for LoadError {
fn from(e: NomErr<nom::error::Error<&[u8]>>) -> LoadError {
match e {
NomErr::Incomplete(_) => LoadError::Incomplete,
NomErr::Error(e) => LoadError::ParseError(e.code),
NomErr::Failure(e) => LoadError::ParseFailure(e.code),
}
}
}
impl TryFrom<&Path> for PackIndexFile {
type Error = LoadError;
fn try_from(filename: &Path) -> LoadResult<PackIndexFile> {
let file = File::open(filename).map_err(LoadError::FileOpen)?;
PackIndexFile::try_from(file)
}
}
impl TryFrom<&str> for PackIndexFile {
type Error = LoadError;
fn try_from(filename: &str) -> LoadResult<PackIndexFile> {
PackIndexFile::try_from(Path::new(filename))
}
}
impl TryFrom<File> for PackIndexFile {
type Error = LoadError;
fn try_from(file: File) -> LoadResult<PackIndexFile> {
let mut buf = BufReader::new(file);
let mut bytes: Vec<u8> = Vec::new();
buf.read_to_end(&mut bytes).map_err(LoadError::Read)?;
let (_rest, pki_file) = parser::parse_pki_file(&bytes)?;
Ok(pki_file)
}
}