hive-agent: inline todo dispatch arms instead of a sub-match + unreachable!
per mara's review on #2679: replace the separate dispatch_todo sub-match (with its trailing unreachable! arm) with four small handler functions called directly from dispatch's existing match. same behavior, no unreachable! left in the todo path.
This commit is contained in:
parent
2dcbb78b40
commit
6c886d3fa6
1 changed files with 77 additions and 54 deletions
|
|
@ -165,10 +165,26 @@ fn dispatch(
|
||||||
bus: &Bus,
|
bus: &Bus,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
match req {
|
match req {
|
||||||
Request::UpsertTodo { .. }
|
Request::UpsertTodo {
|
||||||
| Request::ClearTodo { .. }
|
subsystem,
|
||||||
| Request::ListTodos { .. }
|
key,
|
||||||
| Request::MarkTodoDone { .. } => dispatch_todo(req, store, wake),
|
summary,
|
||||||
|
source,
|
||||||
|
} => upsert_todo(
|
||||||
|
store,
|
||||||
|
wake,
|
||||||
|
&subsystem,
|
||||||
|
key.as_deref(),
|
||||||
|
&summary,
|
||||||
|
source.as_deref(),
|
||||||
|
),
|
||||||
|
Request::ClearTodo {
|
||||||
|
subsystem,
|
||||||
|
key,
|
||||||
|
all,
|
||||||
|
} => clear_todo(store, &subsystem, key.as_deref(), all),
|
||||||
|
Request::ListTodos { subsystem } => list_todos(store, subsystem.as_deref()),
|
||||||
|
Request::MarkTodoDone { id } => mark_todo_done(store, id),
|
||||||
Request::StoreReminder {
|
Request::StoreReminder {
|
||||||
message,
|
message,
|
||||||
timing,
|
timing,
|
||||||
|
|
@ -221,61 +237,68 @@ fn dispatch(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Todo-family requests (loose-ends v2). `req` is guaranteed by [`dispatch`]
|
/// `UpsertTodo` handler: writes/refreshes a todo row, logs the outcome, and
|
||||||
/// to be one of the four todo variants; any other variant is a caller bug.
|
/// fires `wake` on a new-or-changed upsert so the serve loop runs a turn.
|
||||||
fn dispatch_todo(req: Request, store: &Todos, wake: &Notify) -> Response {
|
fn upsert_todo(
|
||||||
match req {
|
store: &Todos,
|
||||||
Request::UpsertTodo {
|
wake: &Notify,
|
||||||
subsystem,
|
subsystem: &str,
|
||||||
key,
|
key: Option<&str>,
|
||||||
summary,
|
summary: &str,
|
||||||
source,
|
source: Option<&str>,
|
||||||
} => match store.upsert(&subsystem, key.as_deref(), &summary, source.as_deref()) {
|
) -> Response {
|
||||||
Ok((id, changed)) => {
|
match store.upsert(subsystem, key, summary, source) {
|
||||||
tracing::debug!(subsystem = %subsystem, key = ?key, id, changed, "todo upsert");
|
Ok((id, changed)) => {
|
||||||
if changed {
|
tracing::debug!(%subsystem, ?key, id, changed, "todo upsert");
|
||||||
wake.notify_one();
|
if changed {
|
||||||
}
|
wake.notify_one();
|
||||||
Response::Ok
|
|
||||||
}
|
}
|
||||||
Err(e) => err(&e),
|
Response::Ok
|
||||||
},
|
}
|
||||||
Request::ClearTodo {
|
Err(e) => err(&e),
|
||||||
subsystem,
|
}
|
||||||
key,
|
}
|
||||||
all,
|
|
||||||
} => {
|
/// `ClearTodo` handler: drops one keyed todo, or every todo in `subsystem`
|
||||||
let result = if all {
|
/// when `all` is set.
|
||||||
store.clear_subsystem(&subsystem)
|
fn clear_todo(store: &Todos, subsystem: &str, key: Option<&str>, all: bool) -> Response {
|
||||||
} else {
|
let result = if all {
|
||||||
store.clear(&subsystem, key.as_deref())
|
store.clear_subsystem(subsystem)
|
||||||
};
|
} else {
|
||||||
match result {
|
store.clear(subsystem, key)
|
||||||
Ok(count) => {
|
};
|
||||||
tracing::debug!(subsystem = %subsystem, key = ?key, all, count, "todo clear");
|
match result {
|
||||||
Response::Acked {
|
Ok(count) => {
|
||||||
count: u64::try_from(count).unwrap_or(0),
|
tracing::debug!(%subsystem, ?key, all, count, "todo clear");
|
||||||
}
|
Response::Acked {
|
||||||
}
|
count: u64::try_from(count).unwrap_or(0),
|
||||||
Err(e) => err(&e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Request::ListTodos { subsystem } => match store.list(subsystem.as_deref()) {
|
Err(e) => err(&e),
|
||||||
Ok(todos) => Response::LooseEnds {
|
}
|
||||||
loose_ends: todos.into_iter().map(to_loose_end).collect(),
|
}
|
||||||
},
|
|
||||||
Err(e) => err(&e),
|
/// `ListTodos` handler: read-only, so no debug logging — not relevant to
|
||||||
|
/// diagnosing wake behaviour.
|
||||||
|
fn list_todos(store: &Todos, subsystem: Option<&str>) -> Response {
|
||||||
|
match store.list(subsystem) {
|
||||||
|
Ok(todos) => Response::LooseEnds {
|
||||||
|
loose_ends: todos.into_iter().map(to_loose_end).collect(),
|
||||||
},
|
},
|
||||||
Request::MarkTodoDone { id } => match store.mark_done(id) {
|
Err(e) => err(&e),
|
||||||
Ok(count) => {
|
}
|
||||||
tracing::debug!(id, count, "todo mark-done");
|
}
|
||||||
Response::Acked {
|
|
||||||
count: u64::try_from(count).unwrap_or(0),
|
/// `MarkTodoDone` handler: marks a single todo done by id.
|
||||||
}
|
fn mark_todo_done(store: &Todos, id: i64) -> Response {
|
||||||
|
match store.mark_done(id) {
|
||||||
|
Ok(count) => {
|
||||||
|
tracing::debug!(id, count, "todo mark-done");
|
||||||
|
Response::Acked {
|
||||||
|
count: u64::try_from(count).unwrap_or(0),
|
||||||
}
|
}
|
||||||
Err(e) => err(&e),
|
}
|
||||||
},
|
Err(e) => err(&e),
|
||||||
_ => unreachable!("dispatch only routes todo variants here"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue