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::fs::File; |
6 | use std::io; |
7 | use std::io::Read; |
8 | use std::path::Path; |
9 | |
10 | #[derive (Debug)] |
11 | /// Possible errors that can occur during Cargo.toml parsing. |
12 | pub enum Error { |
13 | /// Error during reading of Cargo.toml |
14 | Io(io::Error), |
15 | /// Deserialization error |
16 | Toml(toml::de::Error), |
17 | } |
18 | |
19 | impl From<io::Error> for Error { |
20 | fn from(err: io::Error) -> Self { |
21 | Error::Io(err) |
22 | } |
23 | } |
24 | impl From<toml::de::Error> for Error { |
25 | fn from(err: toml::de::Error) -> Self { |
26 | Error::Toml(err) |
27 | } |
28 | } |
29 | |
30 | #[derive (Clone, Deserialize, Debug)] |
31 | pub struct Lock { |
32 | pub root: Option<Package>, |
33 | pub package: Option<Vec<Package>>, |
34 | } |
35 | |
36 | #[derive (Clone, Deserialize, Debug)] |
37 | pub struct Package { |
38 | pub name: String, |
39 | pub version: String, |
40 | /// A list of dependencies formatted like "NAME VERSION-OPT REGISTRY-OPT" |
41 | pub dependencies: Option<Vec<String>>, |
42 | } |
43 | |
44 | /// Parse the Cargo.toml for a given path |
45 | pub fn lock(manifest_path: &Path) -> Result<Lock, Error> { |
46 | let mut s: String = String::new(); |
47 | let mut f: File = File::open(manifest_path)?; |
48 | f.read_to_string(&mut s)?; |
49 | |
50 | toml::from_str::<Lock>(&s).map_err(|x: Error| x.into()) |
51 | } |
52 | |