feat(M9.2): validate learned binding drafts
Test: cargo test -p fader-config --test editor Test: cargo clippy -p fader-config --all-targets --all-features -- -D warnings
This commit is contained in:
parent
5daa06100b
commit
9941a79231
|
|
@ -2,7 +2,7 @@ use std::str::FromStr;
|
|||
|
||||
use toml_edit::{Array, DocumentMut, InlineTable, Item, Table, Value, value};
|
||||
|
||||
use crate::{ConfigError, Profile, SelectorField, TargetKind};
|
||||
use crate::{Action, ConfigError, MessageKind, Profile, SelectorField, TargetKind};
|
||||
|
||||
/// Match expression used when creating an application selector.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
|
@ -218,6 +218,47 @@ impl ProfileDocument {
|
|||
self.remove_table("faders", id)
|
||||
}
|
||||
|
||||
/// Adds one validated surface control assignment.
|
||||
pub fn add_binding(
|
||||
&mut self,
|
||||
surface: &str,
|
||||
fader: &str,
|
||||
action: Action,
|
||||
kind: MessageKind,
|
||||
channel: u16,
|
||||
number: u16,
|
||||
) -> Result<(), ConfigError> {
|
||||
self.transaction(|document| {
|
||||
let bindings = document
|
||||
.get_mut("bindings")
|
||||
.and_then(Item::as_array_of_tables_mut)
|
||||
.ok_or_else(|| ConfigError::validation("bindings", "missing binding array"))?;
|
||||
let mut table = Table::new();
|
||||
table["surface"] = value(surface);
|
||||
table["fader"] = value(fader);
|
||||
table["action"] = value(match action {
|
||||
Action::Volume => "volume",
|
||||
Action::ToggleMute => "toggle_mute",
|
||||
});
|
||||
if action == Action::ToggleMute {
|
||||
table["edge"] = value("press");
|
||||
}
|
||||
let mut message = InlineTable::new();
|
||||
message.insert(
|
||||
"kind",
|
||||
Value::from(match kind {
|
||||
MessageKind::Cc => "cc",
|
||||
MessageKind::Note => "note",
|
||||
}),
|
||||
);
|
||||
message.insert("channel", Value::from(i64::from(channel)));
|
||||
message.insert("number", Value::from(i64::from(number)));
|
||||
table["message"] = value(message);
|
||||
bindings.push(table);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the current source without consuming the draft.
|
||||
#[must_use]
|
||||
pub fn source(&self) -> String {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
use fader_config::{ProfileDocument, SelectorDraft, SelectorField, SelectorPattern, TargetKind};
|
||||
use fader_config::{
|
||||
Action, MessageKind, ProfileDocument, SelectorDraft, SelectorField, SelectorPattern, TargetKind,
|
||||
};
|
||||
|
||||
const PROFILE: &str = include_str!("../../examples/profiles/desktop.toml");
|
||||
|
||||
|
|
@ -107,3 +109,31 @@ fn existing_applications_groups_and_faders_can_be_edited_and_deleted() {
|
|||
draft.remove_fader("media-fader").unwrap_err();
|
||||
assert!(draft.source().contains("media-fader"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn draft_adds_bindings_and_rejects_duplicate_physical_controls() {
|
||||
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
|
||||
draft
|
||||
.add_binding(
|
||||
"desk-left",
|
||||
"media-fader",
|
||||
Action::Volume,
|
||||
MessageKind::Cc,
|
||||
1,
|
||||
49,
|
||||
)
|
||||
.unwrap();
|
||||
let before_duplicate = draft.source();
|
||||
let error = draft
|
||||
.add_binding(
|
||||
"desk-left",
|
||||
"media-fader",
|
||||
Action::ToggleMute,
|
||||
MessageKind::Cc,
|
||||
1,
|
||||
49,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(error.to_string().contains("duplicate physical binding"));
|
||||
assert_eq!(draft.source(), before_duplicate);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue