Skip to content

Commit 7e70863

Browse files
refactor(client): extract constants and clean up patterns (#10)
- Extract repeated (200..300) status range to SUCCESS_STATUS constant - Remove trailing comma in format string (get_duckling_config) - Extract trim().to_string() pattern to trimmed() helper in auth Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b8c342e commit 7e70863

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

crates/dkdc-md-cli/src/auth.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const ENV_VARS: &[&str] = &[
99
"MOTHERDUCK_API_KEY",
1010
];
1111

12+
/// Trim whitespace and convert to an owned `String`.
13+
fn trimmed(s: &str) -> String {
14+
s.trim().to_string()
15+
}
16+
1217
/// Resolve token: CLI flag takes precedence over env vars.
1318
/// Pass `Some("-")` to read from stdin.
1419
pub fn resolve_token_or(cli_token: Option<&str>) -> Result<String> {
@@ -24,9 +29,9 @@ fn resolve_token_or_with(
2429
if token == "-" {
2530
return read_token_from_reader(stdin);
2631
}
27-
let trimmed = token.trim();
28-
anyhow::ensure!(!trimmed.is_empty(), "--token value must not be empty");
29-
return Ok(trimmed.to_string());
32+
let t = trimmed(token);
33+
anyhow::ensure!(!t.is_empty(), "--token value must not be empty");
34+
return Ok(t);
3035
}
3136
resolve_token_with(env_var)
3237
}
@@ -36,19 +41,19 @@ fn read_token_from_reader(mut reader: impl Read) -> Result<String> {
3641
reader
3742
.read_to_string(&mut buf)
3843
.context("failed to read token from stdin")?;
39-
let trimmed = buf.trim().to_string();
40-
anyhow::ensure!(!trimmed.is_empty(), "stdin was empty; expected a token");
41-
Ok(trimmed)
44+
let t = trimmed(&buf);
45+
anyhow::ensure!(!t.is_empty(), "stdin was empty; expected a token");
46+
Ok(t)
4247
}
4348

4449
fn resolve_token_with(
4550
env_var: impl Fn(&str) -> Result<String, std::env::VarError>,
4651
) -> Result<String> {
4752
for var in ENV_VARS {
4853
if let Ok(val) = env_var(var) {
49-
let trimmed = val.trim().to_string();
50-
if !trimmed.is_empty() {
51-
return Ok(trimmed);
54+
let t = trimmed(&val);
55+
if !t.is_empty() {
56+
return Ok(t);
5257
}
5358
}
5459
}

crates/dkdc-md-cli/src/client.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ureq::{Agent, http};
99
const BASE_URL: &str = "https://api.motherduck.com";
1010
const TIMEOUT: Duration = Duration::from_secs(10);
1111
const USER_AGENT: &str = concat!("dkdc-md-cli/", env!("CARGO_PKG_VERSION"));
12+
const SUCCESS_STATUS: std::ops::Range<u16> = 200..300;
1213

1314
/// Characters that must be percent-encoded in a URL path segment.
1415
const PATH_SEGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'#').add(b'%').add(b'/').add(b'?');
@@ -150,7 +151,7 @@ impl MotherduckClient {
150151
// -- Ducklings --
151152

152153
pub fn get_duckling_config(&self, username: &str) -> Result<Value> {
153-
self.get(&format!("/v1/users/{}/instances", encode_path(username),))
154+
self.get(&format!("/v1/users/{}/instances", encode_path(username)))
154155
}
155156

156157
pub fn set_duckling_config(
@@ -189,15 +190,15 @@ fn handle_response(mut resp: http::Response<ureq::Body>) -> Result<Value> {
189190

190191
fn parse_response(status: u16, text: String) -> Result<Value> {
191192
match serde_json::from_str::<Value>(&text) {
192-
Ok(body) if (200..300).contains(&status) => Ok(body),
193+
Ok(body) if SUCCESS_STATUS.contains(&status) => Ok(body),
193194
Ok(body) => {
194195
let message = body
195196
.get("message")
196197
.and_then(|m| m.as_str())
197198
.unwrap_or(&text);
198199
bail!("API error ({status}): {message}");
199200
}
200-
Err(_) if (200..300).contains(&status) => Ok(Value::String(text)),
201+
Err(_) if SUCCESS_STATUS.contains(&status) => Ok(Value::String(text)),
201202
Err(_) => bail!("API error ({status}): {text}"),
202203
}
203204
}

0 commit comments

Comments
 (0)