Add tryfrom for Lookups

This commit is contained in:
2026-05-01 14:52:09 +02:00
parent f8f1165db7
commit e9d6aa2e31
2 changed files with 14 additions and 15 deletions

View File

@@ -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,

View File

@@ -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,