artifactize_core/config/
settings.rs1use std::{collections::BTreeMap, ffi::OsStr, path::Path};
5
6use super::{
7 ConfigError, MAX_DECLARED_ITEMS, Profile, ProfileAliasName,
8 declaration::reject_non_json_values,
9 discovery::{DEFINITION_DIR, read_regular_text},
10 identifier,
11 location::Location,
12 validation::text,
13};
14
15pub const SETTINGS_FILE: &str = "settings.toml";
16
17#[derive(Debug, Clone, Default)]
19pub struct Settings {
20 pub profiles: BTreeMap<ProfileAliasName, Profile>,
21}
22
23pub fn parse_settings(source: &str) -> Result<Settings, String> {
25 let table = toml::de::DeTable::parse(source).map_err(|mut error| {
26 error.set_input(Some(source));
27 error.to_string()
28 })?;
29 let document = toml::Spanned::new(table.span(), toml::de::DeValue::Table(table.into_inner()));
30 let location = Location::root(source, &document);
31 for (key, value) in document.get_ref().as_table().unwrap() {
32 reject_non_json_values(value, key.get_ref().as_ref(), source, |offset| offset)?;
33 }
34 let mut profiles = BTreeMap::new();
35 for (key, _) in location.table()? {
36 let field = key.get_ref().as_ref();
37 match field {
38 "profiles" => {
39 profiles = parse_profiles(&location.child("profiles"))?;
40 }
41 other => {
42 return Err(location
43 .key(other)
44 .error(format!("unknown field `{other}`, expected `profiles`")));
45 }
46 }
47 }
48 Ok(Settings { profiles })
49}
50
51fn parse_profiles(
52 location: &Location<'_, '_>,
53) -> Result<BTreeMap<ProfileAliasName, Profile>, String> {
54 let table = location.table()?;
55 if table.len() > MAX_DECLARED_ITEMS {
56 return Err(location.error("profiles must contain at most 64 named profile aliases."));
57 }
58 let mut profiles = BTreeMap::new();
59 for (key, value) in table {
60 let name = key.get_ref().as_ref();
61 location
62 .key(name)
63 .check(identifier(name, "Profile alias name"))?;
64 let alias_name: ProfileAliasName = location.key(name).check(name.parse())?;
65 let alias_location = location.child(name);
66 if let Some(kind) = value.get_ref().get("kind")
67 && let toml::de::DeValue::String(kind) = kind.get_ref()
68 && kind.as_ref() != "agent"
69 {
70 return Err(alias_location
71 .child("kind")
72 .error("Profile aliases support only agent profiles (kind = \"agent\")."));
73 }
74 if let Some(model) = value.get_ref().get("model")
75 && let toml::de::DeValue::String(model) = model.get_ref()
76 {
77 alias_location
78 .child("model")
79 .check(text(model, "Agent profile model"))?;
80 }
81 let profile: Profile = alias_location.deserialize()?;
82 if !matches!(profile, Profile::Agent { .. }) {
83 return Err(alias_location
84 .child("kind")
85 .error("Profile aliases support only agent profiles (kind = \"agent\")."));
86 }
87 profile.validate(&alias_location)?;
88 profiles.insert(alias_name, profile);
89 }
90 Ok(profiles)
91}
92
93pub(super) fn read(root: &Path) -> Result<Settings, ConfigError> {
98 let definitions = root.join(DEFINITION_DIR);
99 if !crate::platform::paths::reusable_directory(root, DEFINITION_DIR)
100 .map_err(|error| ConfigError::new(&definitions, error))?
101 {
102 return Ok(Settings::default());
103 }
104 let opened = crate::platform::open_directory(&definitions)
105 .map_err(|error| ConfigError::new(&definitions, error))?;
106 let entries = crate::platform::read_dir(&opened)
107 .map_err(|error| ConfigError::new(&definitions, error))?;
108 for entry in entries {
109 let entry = entry.map_err(|error| ConfigError::new(&definitions, error))?;
110 if entry.file_name() != OsStr::new(SETTINGS_FILE) {
111 continue;
112 }
113 let path = definitions.join(SETTINGS_FILE);
114 let kind = entry
115 .file_type()
116 .map_err(|error| ConfigError::new(&path, error))?;
117 if kind != crate::platform::FileKind::File {
118 return Err(ConfigError::new(
119 &path,
120 "line 1, column 1: Settings file must be a regular file, not a link, directory or special file.",
121 ));
122 }
123 let source = read_regular_text(&path).map_err(|error| ConfigError::new(&path, error))?;
124 return parse_settings(&source).map_err(|error| ConfigError::new(&path, error));
125 }
126 Ok(Settings::default())
127}
128
129#[cfg(test)]
130mod tests;