feat(M9.1): complete transactional profile mutations
Test: cargo test -p fader-config --all-targets --all-features Test: cargo clippy -p fader-config --all-targets --all-features -- -D warnings
This commit is contained in:
parent
3452fee880
commit
b5d7ac9c5f
|
|
@ -86,26 +86,27 @@ impl ProfileDocument {
|
|||
let mut table = Table::new();
|
||||
table["id"] = value(id);
|
||||
table["label"] = value(label);
|
||||
let mut selector_values = Array::new();
|
||||
for selector in selectors {
|
||||
let mut inline = InlineTable::new();
|
||||
inline.insert("field", Value::from(selector.field.as_str()));
|
||||
match &selector.pattern {
|
||||
SelectorPattern::Exact(pattern) => {
|
||||
inline.insert("exact", Value::from(pattern.as_str()));
|
||||
}
|
||||
SelectorPattern::Regex(pattern) => {
|
||||
inline.insert("regex", Value::from(pattern.as_str()));
|
||||
}
|
||||
}
|
||||
selector_values.push(Value::InlineTable(inline));
|
||||
}
|
||||
table["selectors"] = value(selector_values);
|
||||
table["selectors"] = value(selector_array(selectors));
|
||||
applications.push(table);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Replaces an application's editable fields while retaining table comments.
|
||||
pub fn set_application(
|
||||
&mut self,
|
||||
id: &str,
|
||||
label: &str,
|
||||
selectors: &[SelectorDraft],
|
||||
) -> Result<(), ConfigError> {
|
||||
self.transaction(|document| {
|
||||
let table = find_table_mut(document, "applications", id)?;
|
||||
table["label"] = value(label);
|
||||
table["selectors"] = value(selector_array(selectors));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Removes an application only when no remaining object references it.
|
||||
pub fn remove_application(&mut self, id: &str) -> Result<(), ConfigError> {
|
||||
self.transaction(|document| {
|
||||
|
|
@ -141,17 +142,34 @@ impl ProfileDocument {
|
|||
let mut table = Table::new();
|
||||
table["id"] = value(id);
|
||||
table["label"] = value(label);
|
||||
let mut member_values = Array::new();
|
||||
for member in members {
|
||||
member_values.push(*member);
|
||||
}
|
||||
table["members"] = value(member_values);
|
||||
table["members"] = value(string_array(members));
|
||||
table["volume_mode"] = value("absolute");
|
||||
groups.push(table);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Replaces a group's label and absolute member set.
|
||||
pub fn set_group(
|
||||
&mut self,
|
||||
id: &str,
|
||||
label: &str,
|
||||
members: &[&str],
|
||||
) -> Result<(), ConfigError> {
|
||||
self.transaction(|document| {
|
||||
let table = find_table_mut(document, "groups", id)?;
|
||||
table["label"] = value(label);
|
||||
table["members"] = value(string_array(members));
|
||||
table["volume_mode"] = value("absolute");
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Removes an unreferenced group.
|
||||
pub fn remove_group(&mut self, id: &str) -> Result<(), ConfigError> {
|
||||
self.remove_table("groups", id)
|
||||
}
|
||||
|
||||
/// Adds a logical fader and validates its target.
|
||||
pub fn add_fader(
|
||||
&mut self,
|
||||
|
|
@ -168,15 +186,7 @@ impl ProfileDocument {
|
|||
.ok_or_else(|| ConfigError::validation("faders", "missing fader array"))?;
|
||||
let mut table = Table::new();
|
||||
table["id"] = value(id);
|
||||
let mut target = InlineTable::new();
|
||||
let kind = match target_kind {
|
||||
TargetKind::Application => "application",
|
||||
TargetKind::Group => "group",
|
||||
TargetKind::Output => "output",
|
||||
};
|
||||
target.insert("kind", Value::from(kind));
|
||||
target.insert("id", Value::from(target_id));
|
||||
table["target"] = value(target);
|
||||
table["target"] = value(target_table(target_kind, target_id));
|
||||
table["pickup"] = value(pickup);
|
||||
table["pickup_tolerance"] = value(pickup_tolerance);
|
||||
faders.push(table);
|
||||
|
|
@ -184,6 +194,29 @@ impl ProfileDocument {
|
|||
})
|
||||
}
|
||||
|
||||
/// Replaces a fader's target and pickup policy.
|
||||
pub fn set_fader(
|
||||
&mut self,
|
||||
id: &str,
|
||||
target_kind: TargetKind,
|
||||
target_id: &str,
|
||||
pickup: bool,
|
||||
pickup_tolerance: f64,
|
||||
) -> Result<(), ConfigError> {
|
||||
self.transaction(|document| {
|
||||
let table = find_table_mut(document, "faders", id)?;
|
||||
table["target"] = value(target_table(target_kind, target_id));
|
||||
table["pickup"] = value(pickup);
|
||||
table["pickup_tolerance"] = value(pickup_tolerance);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Removes a fader only when no binding references it.
|
||||
pub fn remove_fader(&mut self, id: &str) -> Result<(), ConfigError> {
|
||||
self.remove_table("faders", id)
|
||||
}
|
||||
|
||||
/// Returns the current source without consuming the draft.
|
||||
#[must_use]
|
||||
pub fn source(&self) -> String {
|
||||
|
|
@ -206,6 +239,23 @@ impl ProfileDocument {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_table(&mut self, section: &str, id: &str) -> Result<(), ConfigError> {
|
||||
self.transaction(|document| {
|
||||
let tables = document
|
||||
.get_mut(section)
|
||||
.and_then(Item::as_array_of_tables_mut)
|
||||
.ok_or_else(|| ConfigError::validation(section, "missing table array"))?;
|
||||
let index = tables
|
||||
.iter()
|
||||
.position(|table| table.get("id").and_then(Item::as_str) == Some(id))
|
||||
.ok_or_else(|| {
|
||||
ConfigError::validation(section, format!("unknown {section} entry {id:?}"))
|
||||
})?;
|
||||
tables.remove(index);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns edited TOML only if the complete result remains valid.
|
||||
pub fn finish(self) -> Result<String, ConfigError> {
|
||||
let output = self.document.to_string();
|
||||
|
|
@ -213,3 +263,62 @@ impl ProfileDocument {
|
|||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
fn find_table_mut<'a>(
|
||||
document: &'a mut DocumentMut,
|
||||
section: &str,
|
||||
id: &str,
|
||||
) -> Result<&'a mut Table, ConfigError> {
|
||||
document
|
||||
.get_mut(section)
|
||||
.and_then(Item::as_array_of_tables_mut)
|
||||
.and_then(|tables| {
|
||||
tables.iter_mut().find(|table| {
|
||||
table
|
||||
.get("id")
|
||||
.and_then(Item::as_str)
|
||||
.is_some_and(|candidate| candidate == id)
|
||||
})
|
||||
})
|
||||
.ok_or_else(|| ConfigError::validation(section, format!("unknown {section} entry {id:?}")))
|
||||
}
|
||||
|
||||
fn selector_array(selectors: &[SelectorDraft]) -> Array {
|
||||
let mut values = Array::new();
|
||||
for selector in selectors {
|
||||
let mut inline = InlineTable::new();
|
||||
inline.insert("field", Value::from(selector.field.as_str()));
|
||||
match &selector.pattern {
|
||||
SelectorPattern::Exact(pattern) => {
|
||||
inline.insert("exact", Value::from(pattern.as_str()));
|
||||
}
|
||||
SelectorPattern::Regex(pattern) => {
|
||||
inline.insert("regex", Value::from(pattern.as_str()));
|
||||
}
|
||||
}
|
||||
values.push(Value::InlineTable(inline));
|
||||
}
|
||||
values
|
||||
}
|
||||
|
||||
fn string_array(values: &[&str]) -> Array {
|
||||
let mut array = Array::new();
|
||||
for item in values {
|
||||
array.push(*item);
|
||||
}
|
||||
array
|
||||
}
|
||||
|
||||
fn target_table(kind: TargetKind, id: &str) -> InlineTable {
|
||||
let mut target = InlineTable::new();
|
||||
target.insert(
|
||||
"kind",
|
||||
Value::from(match kind {
|
||||
TargetKind::Application => "application",
|
||||
TargetKind::Group => "group",
|
||||
TargetKind::Output => "output",
|
||||
}),
|
||||
);
|
||||
target.insert("id", Value::from(id));
|
||||
target
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,3 +72,38 @@ fn invalid_edits_and_referenced_deletes_roll_back_the_draft() {
|
|||
assert!(referenced.to_string().contains("unknown application"));
|
||||
assert_eq!(draft.source(), original);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_applications_groups_and_faders_can_be_edited_and_deleted() {
|
||||
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
|
||||
draft
|
||||
.set_application(
|
||||
"browser",
|
||||
"Web",
|
||||
&[SelectorDraft {
|
||||
field: SelectorField::ProcessBinary,
|
||||
pattern: SelectorPattern::Exact("waterfox-bin".to_owned()),
|
||||
}],
|
||||
)
|
||||
.unwrap();
|
||||
draft.set_group("media", "Players", &["browser"]).unwrap();
|
||||
draft
|
||||
.set_fader(
|
||||
"media-fader",
|
||||
TargetKind::Application,
|
||||
"browser",
|
||||
false,
|
||||
0.05,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let profile = draft.preview().unwrap();
|
||||
assert_eq!(profile.applications()[1].label(), "Web");
|
||||
assert_eq!(profile.groups()[0].members(), &["browser"]);
|
||||
assert_eq!(profile.faders()[0].target_kind(), TargetKind::Application);
|
||||
assert!(!profile.faders()[0].pickup());
|
||||
|
||||
draft.remove_group("media").unwrap();
|
||||
draft.remove_fader("media-fader").unwrap_err();
|
||||
assert!(draft.source().contains("media-fader"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue