I'd like to clean the match branches of this code up because they seem very verbose and have a lot of duplication between them, but I can't figure out how to make the compiler not complain. Any ideas or am I stuck?
let mut pool = &context.pool;
let name = food_input.name;
let food = match &food_input.id {
Some(id)=>{
let the_id = Uuid::parse_str(&id)?;
sqlx::query_as!(
Food,
r#"
INSERT INTO foods ( id, name )
VALUES ( $1, $2 )
RETURNING id, name, food_group_id
"#,
the_id,
name
).fetch_one(&mut pool).await?
}
None=>{
sqlx::query_as!(
Food,
r#"
INSERT INTO foods ( name )
VALUES ( $1 )
RETURNING id, name, food_group_id
"#,
name
).fetch_one(&mut pool).await?
}
};