|
49 | 49 | //! translate from `ConfigValue` and environment variables to the caller's |
50 | 50 | //! desired type. |
51 | 51 |
|
| 52 | +use std::borrow::Cow; |
52 | 53 | use std::cell::{RefCell, RefMut}; |
53 | 54 | use std::collections::hash_map::Entry::{Occupied, Vacant}; |
54 | 55 | use std::collections::{HashMap, HashSet}; |
55 | 56 | use std::env; |
| 57 | +use std::ffi::OsStr; |
56 | 58 | use std::fmt; |
57 | 59 | use std::fs::{self, File}; |
58 | 60 | use std::io::prelude::*; |
@@ -181,6 +183,7 @@ pub struct Config { |
181 | 183 | target_cfgs: LazyCell<Vec<(String, TargetCfgConfig)>>, |
182 | 184 | doc_extern_map: LazyCell<RustdocExternMap>, |
183 | 185 | progress_config: ProgressConfig, |
| 186 | + env_config: LazyCell<EnvConfig>, |
184 | 187 | } |
185 | 188 |
|
186 | 189 | impl Config { |
@@ -264,6 +267,7 @@ impl Config { |
264 | 267 | target_cfgs: LazyCell::new(), |
265 | 268 | doc_extern_map: LazyCell::new(), |
266 | 269 | progress_config: ProgressConfig::default(), |
| 270 | + env_config: LazyCell::new(), |
267 | 271 | } |
268 | 272 | } |
269 | 273 |
|
@@ -1244,6 +1248,11 @@ impl Config { |
1244 | 1248 | &self.progress_config |
1245 | 1249 | } |
1246 | 1250 |
|
| 1251 | + pub fn env_config(&self) -> CargoResult<&EnvConfig> { |
| 1252 | + self.env_config |
| 1253 | + .try_borrow_with(|| self.get::<EnvConfig>("env")) |
| 1254 | + } |
| 1255 | + |
1247 | 1256 | /// This is used to validate the `term` table has valid syntax. |
1248 | 1257 | /// |
1249 | 1258 | /// This is necessary because loading the term settings happens very |
@@ -1953,6 +1962,54 @@ where |
1953 | 1962 | deserializer.deserialize_option(ProgressVisitor) |
1954 | 1963 | } |
1955 | 1964 |
|
| 1965 | +#[derive(Debug, Deserialize)] |
| 1966 | +#[serde(untagged)] |
| 1967 | +enum EnvConfigValueInner { |
| 1968 | + Simple(String), |
| 1969 | + WithOptions { |
| 1970 | + value: String, |
| 1971 | + #[serde(default)] |
| 1972 | + force: bool, |
| 1973 | + #[serde(default)] |
| 1974 | + relative: bool, |
| 1975 | + }, |
| 1976 | +} |
| 1977 | + |
| 1978 | +#[derive(Debug, Deserialize)] |
| 1979 | +#[serde(transparent)] |
| 1980 | +pub struct EnvConfigValue { |
| 1981 | + inner: Value<EnvConfigValueInner>, |
| 1982 | +} |
| 1983 | + |
| 1984 | +impl EnvConfigValue { |
| 1985 | + pub fn is_force(&self) -> bool { |
| 1986 | + match self.inner.val { |
| 1987 | + EnvConfigValueInner::Simple(_) => false, |
| 1988 | + EnvConfigValueInner::WithOptions { force, .. } => force, |
| 1989 | + } |
| 1990 | + } |
| 1991 | + |
| 1992 | + pub fn resolve<'a>(&'a self, config: &Config) -> Cow<'a, OsStr> { |
| 1993 | + match self.inner.val { |
| 1994 | + EnvConfigValueInner::Simple(ref s) => Cow::Borrowed(OsStr::new(s.as_str())), |
| 1995 | + EnvConfigValueInner::WithOptions { |
| 1996 | + ref value, |
| 1997 | + relative, |
| 1998 | + .. |
| 1999 | + } => { |
| 2000 | + if relative { |
| 2001 | + let p = self.inner.definition.root(config).join(&value); |
| 2002 | + Cow::Owned(p.into_os_string()) |
| 2003 | + } else { |
| 2004 | + Cow::Borrowed(OsStr::new(value.as_str())) |
| 2005 | + } |
| 2006 | + } |
| 2007 | + } |
| 2008 | + } |
| 2009 | +} |
| 2010 | + |
| 2011 | +pub type EnvConfig = HashMap<String, EnvConfigValue>; |
| 2012 | + |
1956 | 2013 | /// A type to deserialize a list of strings from a toml file. |
1957 | 2014 | /// |
1958 | 2015 | /// Supports deserializing either a whitespace-separated list of arguments in a |
|
0 commit comments