Use safe defaults when settings are missing

This commit is contained in:
mdecimus
2024-03-30 18:12:40 +01:00
parent cb4d2f15ae
commit 35562bb9fd
120 changed files with 11732 additions and 2069 deletions

View File

@@ -207,17 +207,18 @@ impl<'x, 'y> TomlParser<'x, 'y> {
#[allow(clippy::while_let_on_iterator)]
fn key(&mut self, mut key: String, in_curly: bool) -> Result<(String, char)> {
let start_key_len = key.len();
while let Some(ch) = self.iter.next() {
match ch {
'=' => {
if !key.is_empty() {
if start_key_len != key.len() {
return Ok((key, ch));
} else {
return Err(format!("Empty key at line: {}", self.line));
}
}
',' | '}' if in_curly => {
if !key.is_empty() {
if start_key_len != key.len() {
return Ok((key, ch));
} else {
return Err(format!("Empty key at line: {}", self.line));
@@ -236,7 +237,7 @@ impl<'x, 'y> TomlParser<'x, 'y> {
}
'\n' => {
return Err(format!(
"Unexpected end of line at line: {}",
"Unexpected end of line while parsing quoted key at line: {}",
self.line
));
}
@@ -249,7 +250,14 @@ impl<'x, 'y> TomlParser<'x, 'y> {
}
' ' | '\t' | '\r' => (),
'\n' => {
return Err(format!("Unexpected end of line at line: {}", self.line));
if start_key_len == key.len() {
self.line += 1;
} else {
return Err(format!(
"Unexpected end of line while parsing key {:?} at line: {}",
key, self.line
));
}
}
_ => {
return Err(format!(

View File

@@ -60,13 +60,7 @@ impl Config {
let key = key.as_key();
let value = match self.keys.get(&key) {
Some(value) => value.as_str(),
None => {
self.warnings.insert(
key.clone(),
ConfigWarning::AppliedDefault(default.to_string()),
);
default
}
None => default,
};
match T::parse_value(value) {
Ok(value) => Some(value),
@@ -80,16 +74,13 @@ impl Config {
pub fn property_or_else<T: ParseValue>(
&mut self,
key: impl AsKey,
default: impl AsKey,
or_else: impl AsKey,
default: &str,
) -> Option<T> {
let key = key.as_key();
let value = match self.value_or_else(key.as_str(), default.clone()) {
let value = match self.value_or_else(key.as_str(), or_else.clone()) {
Some(value) => value,
None => {
self.warnings
.insert(default.as_key(), ConfigWarning::Missing);
return None;
}
None => default,
};
match T::parse_value(value) {
@@ -216,10 +207,10 @@ impl Config {
}
}
pub fn value_or_else(&self, key: impl AsKey, default: impl AsKey) -> Option<&str> {
pub fn value_or_else(&self, key: impl AsKey, or_else: impl AsKey) -> Option<&str> {
self.keys
.get(&key.as_key())
.or_else(|| self.keys.get(&default.as_key()))
.or_else(|| self.keys.get(&or_else.as_key()))
.map(|s| s.as_str())
}
@@ -260,17 +251,6 @@ impl Config {
self.keys.remove(key)
}
pub fn value_or_warn(&mut self, key: impl AsKey) -> Option<&str> {
let key = key.as_key();
match self.keys.get(&key) {
Some(value) => Some(value.as_str()),
None => {
self.warnings.insert(key, ConfigWarning::Missing);
None
}
}
}
pub fn new_parse_error(&mut self, key: impl AsKey, details: impl Into<String>) {
self.errors
.insert(key.as_key(), ConfigError::Parse(details.into()));
@@ -523,6 +503,12 @@ impl ParseValue for Rate {
}
}
impl ParseValue for () {
fn parse_value(_: &str) -> super::Result<Self> {
Ok(())
}
}
pub trait AsKey: Clone {
fn as_key(&self) -> String;
fn as_prefix(&self) -> String;

View File

@@ -66,11 +66,18 @@ impl From<&str> for PublicSuffix {
impl PublicSuffix {
#[allow(unused_variables)]
pub async fn parse(config: &mut Config, key: &str) -> PublicSuffix {
let values = config
let mut values = config
.values(key)
.map(|(_, s)| s.to_string())
.collect::<Vec<_>>();
let has_values = !values.is_empty();
if values.is_empty() {
values = vec![
"https://publicsuffix.org/list/public_suffix_list.dat".to_string(),
"https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat"
.to_string(),
]
}
for (idx, value) in values.into_iter().enumerate() {
let bytes = if value.starts_with("https://") || value.starts_with("http://") {
let result = match reqwest::get(&value).await {
@@ -157,14 +164,7 @@ impl PublicSuffix {
}
#[cfg(not(feature = "test_mode"))]
config.new_build_error(
key,
if has_values {
"Failed to parse public suffixes from any source."
} else {
"No public suffixes list was specified."
},
);
config.new_build_error(key, "Failed to parse public suffixes from any source.");
PublicSuffix::default()
}