Skip to main content

wip_git/
config.rs

1use crate::git;
2
3/// Resolve the current user slug: `wip.user` config, falling back to `user.name`.
4pub fn user() -> Result<String, String> {
5    // Try wip.user first, then fall back to user.name
6    if let Ok(user) = git::git_stdout(&["config", "wip.user"])
7        && !user.is_empty()
8    {
9        return Ok(user);
10    }
11
12    let name = git::git_stdout(&["config", "user.name"])
13        .map_err(|_| "could not determine user: set git user.name or wip.user".to_string())?;
14
15    if name.is_empty() {
16        return Err("could not determine user: set git user.name or wip.user".to_string());
17    }
18
19    Ok(slugify(&name))
20}
21
22/// Return the configured expiry threshold (`wip.expire`), defaulting to `"30d"`.
23pub fn default_expire() -> String {
24    git::git_stdout(&["config", "wip.expire"]).unwrap_or_else(|_| "30d".to_string())
25}
26
27fn slugify(s: &str) -> String {
28    s.to_lowercase()
29        .chars()
30        .map(|c| {
31            if c.is_alphanumeric() || c == '-' {
32                c
33            } else {
34                '-'
35            }
36        })
37        .collect::<String>()
38        .trim_matches('-')
39        .to_string()
40}