1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | |
5 | use std::error; |
6 | use std::fmt; |
7 | use std::fs::File; |
8 | use std::io; |
9 | use std::io::Read; |
10 | use std::path::Path; |
11 | |
12 | #[derive (Debug)] |
13 | /// Possible errors that can occur during Cargo.toml parsing. |
14 | pub enum Error { |
15 | /// Error during reading of Cargo.toml |
16 | Io(io::Error), |
17 | /// Deserialization error |
18 | Toml(toml::de::Error), |
19 | } |
20 | |
21 | impl From<io::Error> for Error { |
22 | fn from(err: io::Error) -> Self { |
23 | Error::Io(err) |
24 | } |
25 | } |
26 | impl From<toml::de::Error> for Error { |
27 | fn from(err: toml::de::Error) -> Self { |
28 | Error::Toml(err) |
29 | } |
30 | } |
31 | |
32 | impl fmt::Display for Error { |
33 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
34 | match self { |
35 | Error::Io(ref err: &Error) => err.fmt(f), |
36 | Error::Toml(ref err: &Error) => err.fmt(f), |
37 | } |
38 | } |
39 | } |
40 | |
41 | impl error::Error for Error { |
42 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
43 | match self { |
44 | Error::Io(ref err: &Error) => Some(err), |
45 | Error::Toml(ref err: &Error) => Some(err), |
46 | } |
47 | } |
48 | } |
49 | |
50 | #[derive (Clone, Deserialize, Debug)] |
51 | pub struct Manifest { |
52 | pub package: Package, |
53 | } |
54 | |
55 | #[derive (Clone, Deserialize, Debug)] |
56 | pub struct Package { |
57 | pub name: String, |
58 | } |
59 | |
60 | /// Parse the Cargo.toml for a given path |
61 | pub fn manifest(manifest_path: &Path) -> Result<Manifest, Error> { |
62 | let mut s: String = String::new(); |
63 | let mut f: File = File::open(manifest_path)?; |
64 | f.read_to_string(&mut s)?; |
65 | |
66 | toml::from_str::<Manifest>(&s).map_err(|x: Error| x.into()) |
67 | } |
68 | |