Skip to content

Commit

Permalink
Merge #7
Browse files Browse the repository at this point in the history
7: Remove unwrap when env variables are wrong r=TheNeikos a=TheNeikos



Co-authored-by: Marcel Müller <[email protected]>
  • Loading branch information
bors[bot] and TheNeikos authored Feb 4, 2023
2 parents c6cd92f + df46df5 commit 02b3194
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum EnvDeserializationError {
UnsupportedValue,
#[error("Tried to nest values while a simple value was expected")]
InvalidNestedValues,
#[error("Invalid nesting detected for paths ending in: {:?}", .0)]
InvalidEnvNesting(Vec<String>),
}

impl serde::de::Error for EnvDeserializationError {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn from_primitive<T: DeserializeOwned, I: Iterator<Item = (Key, String)>>(
values: I,
) -> Result<T, error::EnvDeserializationError> {
let deserializer =
Value::from_list(values.map(|(key, val)| (key, Value::Simple(val))).collect()).unwrap();
Value::from_list(values.map(|(key, val)| (key, Value::Simple(val))).collect())?;
T::deserialize(deserializer)
}

Expand Down
20 changes: 15 additions & 5 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ pub(crate) enum Value {
const SEPERATOR: &str = "__";

impl Value {
fn insert_at(&mut self, path: &[&str], value: Value) -> Result<(), ()> {
fn insert_at(&mut self, path: &[&str], value: Value) -> Result<(), EnvDeserializationError> {
match self {
Value::Simple(_) => Err(()),
Value::Simple(_) => Err(EnvDeserializationError::InvalidEnvNesting(
path.iter().map(|s| s.to_string()).collect(),
)),
Value::Map(values) => {
let val = if let Some((_key, val)) =
values.iter_mut().find(|(key, _)| key.as_ref() == path[0])
{
match val {
Value::Simple(_) => return Err(()),
Value::Simple(_) => {
return Err(EnvDeserializationError::InvalidEnvNesting(
path.iter().map(|s| s.to_string()).collect(),
))
}
Value::Map(_) => val,
}
} else {
Expand All @@ -37,7 +43,11 @@ impl Value {
val.insert_at(path, value)
} else {
match val {
Value::Simple(_) => return Err(()),
Value::Simple(_) => {
return Err(EnvDeserializationError::InvalidEnvNesting(
path.iter().map(|s| s.to_string()).collect(),
))
}
Value::Map(values) => values.push((Key::new(path[0].to_string()), value)),
}
Ok(())
Expand All @@ -46,7 +56,7 @@ impl Value {
}
}

pub(crate) fn from_list(list: Vec<(Key, Value)>) -> Result<Value, ()> {
pub(crate) fn from_list(list: Vec<(Key, Value)>) -> Result<Value, EnvDeserializationError> {
let mut base = Value::Map(vec![]);

for (key, value) in list {
Expand Down
41 changes: 41 additions & 0 deletions tests/failure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![allow(dead_code)]
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Simple {
test: bool,
}

#[test]
fn wrongly_nested_fields() {
let vars = [("test", "true"), ("test__bar", "true")];

for (key, val) in vars {
std::env::set_var(key, val);
}

let config: Result<Simple, _> = envious::from_env(envious::Prefix::None);

for (key, _) in vars {
std::env::remove_var(key);
}

println!("{:?}", config.unwrap_err());
}

#[test]
fn wrongly_nested_prefixed_fields() {
let vars = [("PRE_test", "true"), ("PRE_test__bar", "true")];

for (key, val) in vars {
std::env::set_var(key, val);
}

let config: Result<Simple, _> = envious::from_env(envious::Prefix::Some("PRE_"));

for (key, _) in vars {
std::env::remove_var(key);
}

println!("{:?}", config.unwrap_err());
}

0 comments on commit 02b3194

Please sign in to comment.