1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
use toml; use std::io::Read; use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::fs::File; use errors::*; use itertools::Itertools; use util::vec_opt_iter; #[derive(Deserialize, Debug, Clone)] #[serde(default)] pub struct Config { pub bin: Option<bool>, pub bin_name: String, pub semver_suffix: bool, pub overlay: Option<PathBuf>, pub excludes: Option<Vec<String>>, pub allow_prerelease_deps: bool, pub summary: String, pub source: Option<SourceOverride>, pub packages: Option<HashMap<String, PackageOverride>>, } #[derive(Deserialize, Debug, Clone, Default)] pub struct SourceOverride { section: Option<String>, policy: Option<String>, homepage: Option<String>, vcs_git: Option<String>, vcs_browser: Option<String>, build_depends: Option<Vec<String>>, build_depends_excludes: Option<Vec<String>>, } #[derive(Deserialize, Debug, Clone, Default)] pub struct PackageOverride { section: Option<String>, summary: Option<String>, description: Option<String>, depends: Option<Vec<String>>, } impl Default for Config { fn default() -> Self { Config { bin: None, bin_name: "<default>".to_string(), semver_suffix: false, overlay: None, excludes: None, allow_prerelease_deps: false, summary: "".to_string(), source: None, packages: None, } } } impl Config { pub fn build_bin_package(&self) -> bool { match self.bin { None => !self.semver_suffix, Some(b) => b, } } pub fn overlay_dir(&self, config_path: Option<&Path>) -> Option<PathBuf> { self.overlay.as_ref() .map(|p| config_path.unwrap().parent().unwrap().join(p)) } pub fn is_source_present(&self) -> bool { self.source.is_some() } pub fn is_packages_present(&self) -> bool { self.packages.is_some() } pub fn orig_tar_excludes(&self) -> Option<&Vec<String>> { self.excludes.as_ref() } pub fn policy_version(&self) -> Option<&str> { if let Some(ref s) = self.source { if let Some(ref policy) = s.policy { return Some(policy); } } None } pub fn homepage(&self) -> Option<&str> { if let Some(ref s) = self.source { if let Some(ref homepage) = s.homepage { return Some(homepage); } } None } pub fn build_depends(&self) -> Option<&Vec<String>> { self.source.as_ref().and_then(|s| { s.build_depends.as_ref() }) } pub fn build_depends_excludes(&self) -> Option<&Vec<String>> { self.source.as_ref().and_then(|s| { s.build_depends_excludes.as_ref() }) } pub fn section(&self) -> Option<&str> { if let Some(ref s) = self.source { if let Some(ref section) = s.section { return Some(section); } } None } pub fn package_section(&self, pkgname: &str) -> Option<&str> { self.packages.as_ref().and_then(|pkg| { pkg.get(pkgname).and_then(|package| { package.section.as_ref().map(|s| s.as_str()) }) }) } pub fn package_summary(&self, pkgname: &str) -> Option<(&str, &str)> { self.packages.as_ref().and_then(|pkg| { pkg.get(pkgname).map(|package| { let s = match package.summary { Some(ref s) => s, None => "", }; let d = match package.description { Some(ref d) => d, None => "", }; (s, d) }) }) } pub fn package_depends(&self, pkgname: &str) -> Option<&Vec<String>> { self.packages.as_ref().and_then(|pkg| { pkg.get(pkgname).and_then(|package| { package.depends.as_ref() }) }) } pub fn package_depends_for_feature<'a>( &'a self, feature: &'a str, f_depends: Vec<&'a str>) -> impl Iterator<Item = &str> + 'a { Some(feature).into_iter().chain(f_depends.into_iter()).map(move |f| vec_opt_iter(self.package_depends(&package_key_for_feature(f))) ).flatten().map(|s| s.as_str()) } pub fn vcs_git(&self) -> Option<&str> { if let Some(ref s) = self.source { if let Some(ref vcs_git) = s.vcs_git { return Some(vcs_git); } } None } pub fn vcs_browser(&self) -> Option<&str> { if let Some(ref s) = self.source { if let Some(ref vcs_browser) = s.vcs_browser { return Some(vcs_browser); } } None } } pub fn parse_config(src: &Path) -> Result<Config> { let mut config_file = File::open(src)?; let mut content = String::new(); config_file.read_to_string(&mut content)?; Ok(toml::from_str(&content)?) } pub fn package_key_for_feature(feature: &str) -> String { if feature == "" { PACKAGE_KEY_FOR_LIB.to_string() } else { format!("{}+{}", PACKAGE_KEY_FOR_LIB, feature) } } pub const PACKAGE_KEY_FOR_BIN : &'static str = "bin"; pub const PACKAGE_KEY_FOR_LIB : &'static str = "lib";