itars-core 0.1.0

Core library for iTARS.
Documentation
use std::fs::File;
use std::io::Read;
use serde_derive::Deserialize;

/// parse [version] from [itars-core/Cargo.toml] file
/// ```!test
/// [package]
/// version = "0.0.0"
/// ```
/// return version string
pub fn parse() -> String {
    let mut content = String::new();

    // read file
    let read_file = |file: &mut File| -> String {
        let mut text = String::new();
        return match file.read_to_string(&mut text) {
            _ => text,
        };
    };

    // open file
    match File::open("itars-core/Cargo.toml") {
        Err(_) => {},
        Ok(mut file) => content = read_file(&mut file),
    };
    
    // open other file
    if content.is_empty() {
        match File::open("Cargo.toml") {
            Err(_) => {},
            Ok(mut file) => content = read_file(&mut file),
        };
    }
    
    // not found file
    if content.is_empty() {
        return String::from(DEFAULT_VERSION);
    }

    // parse
    return match toml::from_str::<TomlConfig>(&content[..]) {
        Err(_) => String::from(DEFAULT_VERSION),
        Ok(config) => config.package.version,
    };
}

/// TomlConfig
#[derive(Debug, Deserialize)]
struct TomlConfig {
    package: TomlConfigPackage,
}

/// TomlConfig - Package
#[derive(Debug, Deserialize)]
struct TomlConfigPackage {
    #[serde(default)]
    version: String,
}

static DEFAULT_VERSION: &str = "0.0.0";