convert hand-written enum as_str matches to strum derives workspace-wide
This commit is contained in:
parent
0d6c003fa8
commit
299add158f
14 changed files with 70 additions and 113 deletions
|
|
@ -47,6 +47,7 @@ async-nats.workspace = true
|
|||
reqwest = { workspace = true, features = ["blocking"] }
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
strum.workspace = true
|
||||
# A library, so its errors are a matchable enum rather than an opaque
|
||||
# `anyhow::Error`. The binaries that consume this keep anyhow; `?` converts.
|
||||
thiserror.workspace = true
|
||||
|
|
|
|||
|
|
@ -84,8 +84,11 @@ pub struct AgentWanted {
|
|||
/// than part of a declaration it only half understands. Adding a state means
|
||||
/// adding a variant here and shipping it to both ends — which is the intended
|
||||
/// workflow, not an obstacle to route around with a catch-all variant.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#[derive(
|
||||
Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, strum::IntoStaticStr,
|
||||
)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum AgentState {
|
||||
/// Exists on the hive and is running.
|
||||
Up,
|
||||
|
|
@ -112,16 +115,13 @@ pub enum AgentState {
|
|||
impl AgentState {
|
||||
/// The wire spelling, for a reader that renders rather than decodes.
|
||||
///
|
||||
/// Kept beside the enum so it cannot drift from the `rename_all` above;
|
||||
/// a test pins the two together.
|
||||
/// Derived (`#[strum(serialize_all = "snake_case")]`) from the same
|
||||
/// convention as the `serde(rename_all)` above, rather than a
|
||||
/// hand-written match kept in sync with it by hand; a test still pins
|
||||
/// the two together.
|
||||
#[must_use]
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
AgentState::Up => "up",
|
||||
AgentState::Offline => "offline",
|
||||
AgentState::Paused => "paused",
|
||||
AgentState::Destroyed => "destroyed",
|
||||
}
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue