33 lines
875 B
Rust
33 lines
875 B
Rust
|
use std::process::exit;
|
||
|
pub struct Package {
|
||
|
name: String,
|
||
|
}
|
||
|
|
||
|
impl Package {
|
||
|
pub fn load_from_file(path: String) -> Self {
|
||
|
let contents = match std::fs::read_to_string(path.clone()) {
|
||
|
// If successful return the files text as `contents`.
|
||
|
// `c` is a local variable.
|
||
|
Ok(c) => c,
|
||
|
// Handle the `error` case.
|
||
|
Err(_) => {
|
||
|
// Write `msg` to `stderr`.
|
||
|
eprintln!("Could not read file `{}`", path);
|
||
|
// Exit the program with exit code `1`.
|
||
|
exit(1);
|
||
|
}
|
||
|
};
|
||
|
use toml::Value;
|
||
|
|
||
|
let mut data: Value = toml::from_str(&contents).unwrap();
|
||
|
let name = data
|
||
|
.get("package")
|
||
|
.unwrap()
|
||
|
.get("name")
|
||
|
.unwrap()
|
||
|
.to_string();
|
||
|
|
||
|
Self { name }
|
||
|
}
|
||
|
}
|