Elffuss BraidHub

utopia-ia/elffuss-braid public

356f55034aff · 3.6 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! The repository's own settings: the optional file `.braid/config`.
5//!
6//! The file holds `key = value` lines (`key: value` works too). Blank lines,
7//! lines that start with `#` or `;`, and section headers in square brackets
8//! are skipped, and so are keys braid does not know. A value may be quoted
9//! with `"`. The keys are:
10//!
11//! - `name` (or `user.name`): the name braid gives the changesets it makes;
12//! - `email` (or `user.email`): their email address.
13//!
14//! Nothing requires the file. Without an email, the changesets braid makes
15//! itself, such as the merges of `git sync`, get an `example.org` address.
16
17use std::fs;
18use std::io;
19
20use crate::{DIR, Error, Repo};
21
22/// Name of the configuration file inside [`DIR`].
23pub const CONFIG_FILE: &str = "config";
24
25/// What `.braid/config` sets.
26#[derive(Clone, Debug, Default, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct Config {
29 /// The name for changesets braid makes.
30 pub name: Option<String>,
31 /// The email address for changesets braid makes.
32 pub email: Option<String>,
33}
34
35impl Config {
36 /// Reads the text of a configuration file. Lines it does not understand
37 /// are skipped, and an empty value counts as no value.
38 pub fn parse(text: &str) -> Config {
39 let mut config = Config::default();
40 for line in text.lines() {
41 let line = line.trim();
42 if line.is_empty() || line.starts_with(['#', ';', '[']) {
43 continue;
44 }
45 let Some((key, value)) = line.split_once(['=', ':']) else {
46 continue;
47 };
48 let value = value.trim();
49 let value = value
50 .strip_prefix('"')
51 .and_then(|inner| inner.strip_suffix('"'))
52 .unwrap_or(value);
53 let value = (!value.is_empty()).then(|| value.to_owned());
54 match key.trim() {
55 "name" | "user.name" => config.name = value,
56 "email" | "user.email" => config.email = value,
57 _ => {}
58 }
59 }
60 config
61 }
62}
63
64impl Repo {
65 /// The settings in `.braid/config`; all unset when there is no such file.
66 pub fn config(&self) -> Result<Config, Error> {
67 match fs::read_to_string(self.root.join(DIR).join(CONFIG_FILE)) {
68 Ok(text) => Ok(Config::parse(&text)),
69 Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(Config::default()),
70 Err(err) => Err(err.into()),
71 }
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn parsing() {
81 let config = Config::parse(
82 "# who braid is\n[user]\nname = Ada Lovelace\n email: \"ada@example.org\"\n\
83 colour = blue\nnot a setting\n",
84 );
85 assert_eq!(config.name.as_deref(), Some("Ada Lovelace"));
86 assert_eq!(config.email.as_deref(), Some("ada@example.org"));
87 let config = Config::parse("user.name=Grace\nuser.email =\n; done\n");
88 assert_eq!(config.name.as_deref(), Some("Grace"));
89 assert_eq!(config.email, None);
90 assert_eq!(Config::parse(""), Config::default());
91 }
92
93 #[test]
94 fn the_file_is_optional() {
95 let dir = tempfile::tempdir().unwrap();
96 let repo = Repo::init(dir.path()).unwrap();
97 assert_eq!(repo.config().unwrap(), Config::default());
98 fs::write(
99 dir.path().join(DIR).join(CONFIG_FILE),
100 "email = bot@example.org\n",
101 )
102 .unwrap();
103 assert_eq!(
104 repo.config().unwrap().email.as_deref(),
105 Some("bot@example.org")
106 );
107 }
108}