liftinstall/src/config.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2018-08-03 11:52:31 +00:00
//! config.rs
//!
//! Contains Config structures, as well as means of serialising them.
2018-01-27 03:27:41 +00:00
use toml;
use toml::de::Error as TomlError;
use serde_json::{self, Error as SerdeError};
2018-01-29 12:28:14 +00:00
use sources::get_by_name;
use sources::types::Release;
2018-01-29 12:28:14 +00:00
/// Description of the source of a package.
2018-01-29 12:28:14 +00:00
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct PackageSource {
pub name: String,
#[serde(rename = "match")]
pub match_regex: String,
pub config: toml::Value,
}
2018-01-27 03:27:41 +00:00
/// Describes a overview of a individual package.
2018-01-29 12:28:14 +00:00
#[derive(Debug, Deserialize, Serialize, Clone)]
2018-01-27 03:27:41 +00:00
pub struct PackageDescription {
2018-01-27 11:58:56 +00:00
pub name: String,
pub description: String,
pub default: Option<bool>,
pub source: PackageSource,
2018-01-27 03:27:41 +00:00
}
/// Describes the application itself.
2018-01-29 12:28:14 +00:00
#[derive(Debug, Deserialize, Serialize, Clone)]
2018-01-27 03:27:41 +00:00
pub struct GeneralConfig {
2018-01-27 11:58:56 +00:00
pub name: String,
pub installing_message: String,
2018-01-27 03:27:41 +00:00
}
2018-01-29 12:28:14 +00:00
#[derive(Debug, Deserialize, Serialize, Clone)]
2018-01-27 03:27:41 +00:00
pub struct Config {
2018-01-27 11:58:56 +00:00
pub general: GeneralConfig,
pub packages: Vec<PackageDescription>,
2018-01-27 03:27:41 +00:00
}
impl Config {
/// Serialises as a JSON string.
pub fn to_json_str(&self) -> Result<String, SerdeError> {
serde_json::to_string(self)
}
/// Builds a configuration from a specified TOML string.
2018-01-27 11:58:56 +00:00
pub fn from_toml_str(contents: &str) -> Result<Self, TomlError> {
2018-01-27 03:27:41 +00:00
toml::from_str(contents)
}
}
2018-01-29 12:28:14 +00:00
impl PackageSource {
/// Fetches releases for a given package
pub fn get_current_releases(&self) -> Result<Vec<Release>, String> {
let package_handler = match get_by_name(&self.name) {
Some(v) => v,
_ => return Err(format!("Handler {} not found", self.name)),
2018-01-29 12:28:14 +00:00
};
package_handler.get_current_releases(&self.config)
}
}