Util fn's can use anything that impl's ToString
I don't want to remember to construct a `String` every single time I want to call this function with a string literal. So I won't. Make the functions generic over anything that implements the ToString trait.
This commit is contained in:
@@ -27,9 +27,9 @@ impl core::fmt::Display for Error{
|
|||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
/// The outer value must be a Table so we can get the sub-table from it.
|
/// 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
|
Ok(outer
|
||||||
.get(&table_name)
|
.get(&table_name.to_string())
|
||||||
.ok_or(Error::NoSuchTable)?
|
.ok_or(Error::NoSuchTable)?
|
||||||
.as_table()
|
.as_table()
|
||||||
.ok_or(Error::BadFormat)?)
|
.ok_or(Error::BadFormat)?)
|
||||||
@@ -37,8 +37,8 @@ fn get_table<'outer>(outer: &'outer Table, table_name: String) -> Result<&'outer
|
|||||||
|
|
||||||
/// The config properties are individual strings. This gets the named property,
|
/// The config properties are individual strings. This gets the named property,
|
||||||
/// or an error explaining why it couldn't be fetched.
|
/// or an error explaining why it couldn't be fetched.
|
||||||
fn get_property<'outer>(outer: &'outer Table, property: String) -> Result<&'outer String> {
|
fn get_property<'outer>(outer: &'outer Table, property: impl ToString) -> Result<&'outer String> {
|
||||||
let maybe_prop = outer.get(&property).ok_or(Error::NoSuchProperty)?;
|
let maybe_prop = outer.get(&property.to_string()).ok_or(Error::NoSuchProperty)?;
|
||||||
if let Value::String(text) = maybe_prop {
|
if let Value::String(text) = maybe_prop {
|
||||||
Ok(text)
|
Ok(text)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user