use std::fs::File;
use std::io::Read;
use serde_derive::Deserialize;
pub fn parse() -> String {
let mut content = String::new();
let read_file = |file: &mut File| -> String {
let mut text = String::new();
return match file.read_to_string(&mut text) {
_ => text,
};
};
match File::open("itars-core/Cargo.toml") {
Err(_) => {},
Ok(mut file) => content = read_file(&mut file),
};
if content.is_empty() {
match File::open("Cargo.toml") {
Err(_) => {},
Ok(mut file) => content = read_file(&mut file),
};
}
if content.is_empty() {
return String::from(DEFAULT_VERSION);
}
return match toml::from_str::<TomlConfig>(&content[..]) {
Err(_) => String::from(DEFAULT_VERSION),
Ok(config) => config.package.version,
};
}
#[derive(Debug, Deserialize)]
struct TomlConfig {
package: TomlConfigPackage,
}
#[derive(Debug, Deserialize)]
struct TomlConfigPackage {
#[serde(default)]
version: String,
}
static DEFAULT_VERSION: &str = "0.0.0";