Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a8742cc4a
|
||
|
|
0749e6419f
|
+41
-3
@@ -2,14 +2,52 @@
|
|||||||
/// extract it into some strings
|
/// extract it into some strings
|
||||||
pub fn delimitize(var: &crate::envy::EnvVar) -> Result<Vec<String>, std::io::Error> {
|
pub fn delimitize(var: &crate::envy::EnvVar) -> Result<Vec<String>, std::io::Error> {
|
||||||
if var.has_delimiter {
|
if var.has_delimiter {
|
||||||
Ok(var
|
let values: Vec<String> = match var
|
||||||
.value
|
.value
|
||||||
.split(var.delimiter)
|
.split(var.delimiter)
|
||||||
.map(|c| c.parse::<String>().unwrap())
|
.map(|c| c.parse::<String>())
|
||||||
.collect())
|
.collect()
|
||||||
|
{
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(std::io::Error::other(err));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(values)
|
||||||
} else {
|
} else {
|
||||||
Err(std::io::Error::other(
|
Err(std::io::Error::other(
|
||||||
"Environment variable does not have a delimiter",
|
"Environment variable does not have a delimiter",
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delimitize() {
|
||||||
|
let envvar = crate::envy::EnvVar {
|
||||||
|
key: "OCEANS".to_string(),
|
||||||
|
value: "Atlantic|Pacific|Indian|Arctic".to_string(),
|
||||||
|
has_delimiter: true,
|
||||||
|
delimiter: '|',
|
||||||
|
};
|
||||||
|
let all_values = vec![
|
||||||
|
"Atlantic".to_string(),
|
||||||
|
"Pacific".to_string(),
|
||||||
|
"Indian".to_string(),
|
||||||
|
"Arctic".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match delimitize(&envvar) {
|
||||||
|
Ok(values) => {
|
||||||
|
assert_eq!(values, all_values, "Failure in delimitizing EnvVar values");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error delimitizing: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user