Compare commits
2 Commits
b2b9c8b9d9
...
d27bea2c43
| Author | SHA1 | Date | |
|---|---|---|---|
| d27bea2c43 | |||
| 30d8bcc6de |
@@ -27,18 +27,35 @@ impl core::fmt::Display for Error{
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
/// The outer value must be a Table so we can get the sub-table from it.
|
||||
fn get_table<'outer>(outer: &'outer Table, table_name: String) -> Result<&'outer Table> {
|
||||
fn get_table<'outer>(outer: &'outer Table, table_name: impl ToString) -> Result<&'outer Table> {
|
||||
Ok(outer
|
||||
.get(&table_name)
|
||||
.get(&table_name.to_string())
|
||||
.ok_or(Error::NoSuchTable)?
|
||||
.as_table()
|
||||
.ok_or(Error::BadFormat)?)
|
||||
}
|
||||
|
||||
|
||||
/// Similar to `get_property()` but maps the "Error::NoSuchProperty" result to
|
||||
/// Option::None. Some properties aren't specified, and that's okay... sometimes.
|
||||
fn get_maybe_property<'outer> (outer: &'outer Table, property: impl ToString) -> Result<Option<&'outer String>> {
|
||||
let maybe_prop = get_property(outer, property);
|
||||
match maybe_prop {
|
||||
Ok(value) => Ok(Some(value)),
|
||||
Err(e) => {
|
||||
if let Error::NoSuchProperty = e {
|
||||
return Ok(None);
|
||||
} else {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The config properties are individual strings. This gets the named property,
|
||||
/// or an error explaining why it couldn't be fetched.
|
||||
fn get_property<'outer>(outer: &'outer Table, property: String) -> Result<&'outer String> {
|
||||
let maybe_prop = outer.get(&property).ok_or(Error::NoSuchProperty)?;
|
||||
fn get_property<'outer>(outer: &'outer Table, property: impl ToString) -> Result<&'outer String> {
|
||||
let maybe_prop = outer.get(&property.to_string()).ok_or(Error::NoSuchProperty)?;
|
||||
if let Value::String(text) = maybe_prop {
|
||||
Ok(text)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user