Skip to content

Commit aeaaea8

Browse files
committed
refactor(hm-config): derive Backend Display via derive_more
1 parent 127b65d commit aeaaea8

3 files changed

Lines changed: 16 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hm-config/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ description = "Layered (project/user/env) configuration + credential storage for
88

99
[dependencies]
1010
hm-util = { workspace = true }
11+
derive_more = { workspace = true }
1112
figment = { workspace = true }
1213
serde = { workspace = true }
1314
toml = "0.8"

crates/hm-config/src/lib.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,21 @@ pub const DEFAULT_API_URL: &str = "https://api.harmont.dev";
2020
/// Closed set parsed at the config boundary so invalid values are rejected at
2121
/// deserialize time instead of mis-dispatching later, and every consumer match
2222
/// is exhaustively checked by the compiler.
23-
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
23+
///
24+
/// The `#[display(...)]` strings are the stable lowercase wire/CLI names and
25+
/// must match the `#[serde(rename_all = "lowercase")]` representation.
26+
#[derive(
27+
Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, derive_more::Display,
28+
)]
2429
#[serde(rename_all = "lowercase")]
2530
pub enum Backend {
2631
#[default]
32+
#[display("docker")]
2733
Docker,
34+
#[display("cloud")]
2835
Cloud,
2936
}
3037

31-
impl Backend {
32-
/// Stable lowercase wire/CLI name (matches the `serde` representation).
33-
#[must_use]
34-
pub const fn as_str(self) -> &'static str {
35-
match self {
36-
Self::Docker => "docker",
37-
Self::Cloud => "cloud",
38-
}
39-
}
40-
}
41-
42-
impl std::fmt::Display for Backend {
43-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44-
f.write_str(self.as_str())
45-
}
46-
}
47-
4838
/// Derive the SPA (dashboard) base URL from the API base.
4939
///
5040
/// The CLI talks to `api.harmont.dev`, but a human clicks through to the
@@ -315,6 +305,12 @@ org = "project-org"
315305
assert_eq!(cfg.preferences.format, "json");
316306
}
317307

308+
#[test]
309+
fn backend_display_matches_wire_strings() {
310+
assert_eq!(Backend::Docker.to_string(), "docker");
311+
assert_eq!(Backend::Cloud.to_string(), "cloud");
312+
}
313+
318314
#[test]
319315
fn backend_defaults_docker_and_parses_and_layers() {
320316
// default

0 commit comments

Comments
 (0)