Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"crates/soar-config",
"crates/soar-db",
"crates/soar-dl",
"crates/soar-utils",
Expand All @@ -25,6 +26,7 @@ diesel = { version = "2.3.2", features = [
"sqlite"
] }
diesel_migrations = { version = "2.3.0", features = ["sqlite"] }
documented = "0.9.2"
fast-glob = "1.0.0"
miette = { version = "7.6.0", features = ["fancy"] }
percent-encoding = "2.3.2"
Expand All @@ -38,11 +40,14 @@ rusqlite = { version = "0.37.0", features = ["bundled", "rusqlite-macros"] }
serde = { version = "1.0.225", features = ["derive"] }
serde_json = { version = "1.0.145", features = ["indexmap"] }
serial_test = "3.2.0"
soar-config = { path = "crates/soar-config" }
soar-core = { path = "soar-core" }
soar-dl = { path = "crates/soar-dl" }
soar-utils = { path = "crates/soar-utils" }
tempfile = "3.10.1"
thiserror = "2.0.17"
toml = "0.9.8"
toml_edit = "0.23.7"
tracing = { version = "0.1.41", default-features = false }
ureq = { version = "3.1.2", features = ["json"] }
url = "2.5.7"
Expand Down
21 changes: 21 additions & 0 deletions crates/soar-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "soar-config"
version = "0.1.0"
description = "Configuration management for soar package manager"
authors.workspace = true
edition.workspace = true
readme.workspace = true
repository.workspace = true
license.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
documented = { workspace = true }
miette = { workspace = true }
serde = { workspace = true }
soar-utils = { workspace = true }
thiserror = { workspace = true }
toml = { workspace = true }
toml_edit = { workspace = true }
tracing = { workspace = true }
62 changes: 53 additions & 9 deletions soar-core/src/toml.rs → crates/soar-config/src/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use documented::{Documented, DocumentedFields};
use toml_edit::{ArrayOfTables, Decor, Item, RawString, Table};
use tracing::warn;

use crate::error::ConfigError;
use crate::error::{ConfigError, Result};

/// Appends documentation lines as TOML comments to the given `Decor`.
///
Expand Down Expand Up @@ -52,7 +52,7 @@ pub fn append_docs_as_toml_comments(decor: &mut Decor, docs: &str) {
///
/// # Returns
/// Returns `Ok(())` if successful, or a `ConfigError` if a TOML item is unexpectedly `None`.
pub fn annotate_toml_table<T>(table: &mut Table, is_root: bool) -> Result<(), ConfigError>
pub fn annotate_toml_table<T>(table: &mut Table, is_root: bool) -> Result<()>
where
T: Documented + DocumentedFields,
{
Expand All @@ -66,9 +66,7 @@ where
Ok(docs) => {
match value_item {
Item::None => {
return Err(ConfigError::Custom(format!(
"Encountered TomlEditItem::None for key '{key_str}' unexpectedly",
)))
return Err(ConfigError::UnexpectedTomlItem(key_str.into()));
}
Item::Value(_) => append_docs_as_toml_comments(key_mut.leaf_decor_mut(), docs),
Item::Table(sub_table) => {
Expand Down Expand Up @@ -105,14 +103,60 @@ where
///
/// # Returns
/// Returns `Ok(())` if annotation succeeds, or a `ConfigError` if annotation fails on the first table.
pub fn annotate_toml_array_of_tables<T>(array: &mut ArrayOfTables) -> Result<(), ConfigError>
pub fn annotate_toml_array_of_tables<T>(array: &mut ArrayOfTables) -> Result<()>
where
T: Documented + DocumentedFields,
{
if let Some(first_table) = array.iter_mut().next() {
annotate_toml_table::<T>(first_table, false).map_err(|err| {
ConfigError::Custom(format!("Failed to annotate first table in array: {err}"))
})?;
annotate_toml_table::<T>(first_table, false)
.map_err(|err| ConfigError::AnnotateFirstTable(err.to_string()))?;
}
Ok(())
}

#[cfg(test)]
mod tests {
use toml_edit::Decor;

use super::*;
use crate::config::Config;

#[test]
fn test_append_docs_as_toml_comments() {
let mut decor = Decor::new("", "");
append_docs_as_toml_comments(&mut decor, "Test documentation");

let prefix = decor.prefix().and_then(|p| p.as_str()).unwrap();
assert!(prefix.contains("# Test documentation"));
}

#[test]
fn test_append_docs_multiline() {
let mut decor = Decor::new("", "");
append_docs_as_toml_comments(&mut decor, "Line 1\nLine 2\nLine 3");

let prefix = decor.prefix().and_then(|p| p.as_str()).unwrap();
assert!(prefix.contains("# Line 1"));
assert!(prefix.contains("# Line 2"));
assert!(prefix.contains("# Line 3"));
}

#[test]
fn test_append_docs_empty_lines() {
let mut decor = Decor::new("", "");
append_docs_as_toml_comments(&mut decor, "Line 1\n\nLine 2");

let prefix = decor.prefix().and_then(|p| p.as_str()).unwrap();
assert!(prefix.contains("#\n"));
}

#[test]
fn test_annotate_toml_document() {
let config = Config::default_config::<&str>(false, &[]);
let doc = config.to_annotated_document();

assert!(doc.is_ok());
let doc = doc.unwrap();
assert!(doc.to_string().contains("#"));
}
}
Loading
Loading