Add tryfrom for Lookups
This commit is contained in:
17
src/main.rs
17
src/main.rs
@@ -85,21 +85,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
} => {
|
||||
inputs = inp;
|
||||
|
||||
let output_plan = if let Some(plan) = plan {
|
||||
Ok(ynab::Lookup::Name(plan))
|
||||
} else if let Some(plan_id) = plan_id {
|
||||
Ok(ynab::Lookup::Id(plan_id))
|
||||
} else {
|
||||
Err("no plan name or id".to_string())
|
||||
}?;
|
||||
|
||||
let output_account = if let Some(account) = account {
|
||||
Ok(ynab::Lookup::Name(account))
|
||||
} else if let Some(account_id) = account_id {
|
||||
Ok(ynab::Lookup::Id(account_id))
|
||||
} else {
|
||||
Err("no account name or id".to_string())
|
||||
}?;
|
||||
let output_plan = ynab::Lookup::try_from((plan, plan_id))?;
|
||||
let output_account = ynab::Lookup::try_from((account, account_id))?;
|
||||
|
||||
Output::Ynab {
|
||||
token,
|
||||
|
||||
12
src/ynab.rs
12
src/ynab.rs
@@ -138,6 +138,18 @@ pub enum Lookup {
|
||||
Id(String),
|
||||
}
|
||||
|
||||
impl TryFrom<(Option<String>, Option<String>)> for Lookup {
|
||||
type Error = String;
|
||||
|
||||
fn try_from((name, id): (Option<String>, Option<String>)) -> Result<Self, Self::Error> {
|
||||
match (name, id) {
|
||||
(Some(name), _) => Ok(Lookup::Name(name)),
|
||||
(_, Some(id)) => Ok(Lookup::Id(id)),
|
||||
_ => Err("must provide name or id".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Ynab {
|
||||
client: YnabClient,
|
||||
plan_id: String,
|
||||
|
||||
Reference in New Issue
Block a user