sim-lib-music-serial 0.1.1

Immutable serial plans with stable row and event identity, explicit provenance, and validated partial temporal order.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::collections::BTreeMap;
use std::sync::Arc;

use sim_lib_pitch_serial::RowForm;
use thiserror::Error;

use crate::{
    BuiltInPracticeRule, EventPlacement, OrdinalRef, PlannedSerialEvent, PracticeRule,
    PracticeRuleId, RowInstanceId, SerialEventId, SerialOrigin, SerialPlan, SerialPractice,
    SerialRole, StructuralLicense, VoiceId,
};

/// Failure while composing one inspectable serial deployment plan.
#[derive(Clone, Debug, PartialEq, Eq, Error)]
pub enum SerialDeployError {
    /// The supplied technique id was empty or used an invalid character.
    #[error("{0}")]
    InvalidTechniqueId(String),
    /// The technique attempted to build without any deployers.
    #[error("technique {0} must contain at least one deployer")]
    EmptyTechnique(String),
    /// A deployer named a row instance missing from the deployment input.
    #[error("deployer {deployer} references unknown row {row_id}")]
    UnknownRow {
        /// Stable deployer label.
        deployer: String,
        /// Missing row id.
        row_id: RowInstanceId,
    },
    /// A deployer attempted to reuse an event id already emitted earlier.
    #[error("deployer {deployer} attempted to reuse event id {event_id}")]
    DuplicateEventId {
        /// Stable deployer label.
        deployer: String,
        /// Duplicate event id.
        event_id: SerialEventId,
    },
    /// A deployer requiring one voice per block or form received the wrong count.
    #[error("deployer {deployer} expected {expected} voices but received {actual}")]
    VoiceCountMismatch {
        /// Stable deployer label.
        deployer: String,
        /// Required voice count.
        expected: usize,
        /// Received voice count.
        actual: usize,
    },
    /// Aggregate-rotation block lengths failed to cover exactly one row.
    #[error("aggregate rotation block lengths must sum to 12, received {0}")]
    InvalidRotationCoverage(usize),
    /// The caller requested an interlocking deployment without an interlocking witness.
    #[error("deployer {deployer} requires an interlocking partition witness")]
    NotInterlocking {
        /// Stable deployer label.
        deployer: String,
    },
    /// The requested simultaneous forms were not combinatorial at the block size.
    #[error(
        "rows {source_row_id} and {partner_row_id} are not combinatorial at block size {block_size}"
    )]
    NotCombinatorial {
        /// Source row id.
        source_row_id: RowInstanceId,
        /// Partner row id.
        partner_row_id: RowInstanceId,
        /// Requested contiguous block size.
        block_size: usize,
    },
    /// Building or validating a pitch-serial partition failed.
    #[error("partition build failed: {0}")]
    Partition(String),
    /// Final immutable serial-plan validation failed.
    #[error("serial plan validation failed: {0}")]
    Plan(String),
}

pub(crate) fn validate_technique_id(value: impl Into<String>) -> Result<String, SerialDeployError> {
    let value = value.into();
    if value.trim().is_empty() {
        return Err(SerialDeployError::InvalidTechniqueId(
            "technique id cannot be empty".to_owned(),
        ));
    }
    if value
        .chars()
        .any(|ch| !(ch.is_ascii_alphanumeric() || matches!(ch, '/' | '-' | '_' | '.')))
    {
        return Err(SerialDeployError::InvalidTechniqueId(
            "technique id must use ASCII letters, digits, /, -, _, or .".to_owned(),
        ));
    }
    Ok(value)
}

/// Inspectable parameter attached to one deployer.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SerialDeployerParameter {
    /// Stable parameter name.
    pub name: String,
    /// Stable printable value.
    pub value: String,
}

/// Public category of one serial deployer.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SerialDeployerKind {
    /// Present one complete row in sequence.
    CompleteHorizontalStatement,
    /// Present a caller-declared partition sequentially.
    MotivicPartition,
    /// Present selected blocks vertically as chordal events.
    VerticalBlocks,
    /// Require an interlocking partition witness before deployment.
    InterlockingPartition,
    /// Distribute each block between melody and accompaniment.
    MelodyAccompanimentDistribution,
    /// Rotate the aggregate before reblocking it.
    AggregateRotation,
    /// Present several forms simultaneously by aligned blocks.
    SimultaneousForms,
}

/// Inspectable public description of one deployment component.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SerialDeployerSpec {
    /// Public deployer category.
    pub kind: SerialDeployerKind,
    /// Stable deployer label.
    pub label: String,
    /// Human-facing expected fact or usage.
    pub expected_fact: String,
    /// Inspectable deployer parameters.
    pub parameters: Vec<SerialDeployerParameter>,
}

#[derive(Clone)]
pub(crate) enum SerialDeployerInner {
    Horizontal(super::components::HorizontalStatementSpec),
    Motivic(super::components::MotivicPartitionSpec),
    Vertical(super::components::VerticalBlocksSpec),
    Interlocking(super::components::InterlockingPartitionSpec),
    MelodyAccompaniment(super::components::MelodyAccompanimentSpec),
    AggregateRotation(super::components::AggregateRotationSpec),
    SimultaneousForms(super::components::SimultaneousFormsSpec),
}

/// One reusable inspectable deployment component.
#[derive(Clone)]
pub struct SerialDeployer {
    pub(crate) inner: SerialDeployerInner,
}

impl SerialDeployer {
    pub(crate) fn spec(&self) -> SerialDeployerSpec {
        match &self.inner {
            SerialDeployerInner::Horizontal(spec) => SerialDeployerSpec {
                kind: SerialDeployerKind::CompleteHorizontalStatement,
                label: spec.event_id.as_str().to_owned(),
                expected_fact: "one complete row sounds in sequence".to_owned(),
                parameters: vec![
                    param("row", spec.row_id.as_str()),
                    param("voice", &spec.voice.to_string()),
                    param("reading", spec.license.reading_id.as_str()),
                ],
            },
            SerialDeployerInner::Motivic(spec) => SerialDeployerSpec {
                kind: SerialDeployerKind::MotivicPartition,
                label: spec.event_prefix.clone(),
                expected_fact: "partition blocks sound sequentially as inspectable motives"
                    .to_owned(),
                parameters: vec![
                    param("row", spec.row_id.as_str()),
                    param("blocks", &spec.partition.block_count().to_string()),
                    param("reading", spec.license.reading_id.as_str()),
                ],
            },
            SerialDeployerInner::Vertical(spec) => SerialDeployerSpec {
                kind: SerialDeployerKind::VerticalBlocks,
                label: spec.event_prefix.clone(),
                expected_fact: "selected blocks sound as chordal vertical events".to_owned(),
                parameters: vec![
                    param("row", spec.row_id.as_str()),
                    param("blocks", &format!("{:?}", spec.selected_blocks)),
                    param("reading", spec.license.reading_id.as_str()),
                ],
            },
            SerialDeployerInner::Interlocking(spec) => SerialDeployerSpec {
                kind: SerialDeployerKind::InterlockingPartition,
                label: spec.event_prefix.clone(),
                expected_fact: "interlocking block evidence licenses sequential partition exchange"
                    .to_owned(),
                parameters: vec![
                    param("row", spec.row_id.as_str()),
                    param("blocks", &spec.partition.block_count().to_string()),
                    param("reading", spec.license.reading_id.as_str()),
                ],
            },
            SerialDeployerInner::MelodyAccompaniment(spec) => SerialDeployerSpec {
                kind: SerialDeployerKind::MelodyAccompanimentDistribution,
                label: spec.event_prefix.clone(),
                expected_fact: "each block splits into melody lead and accompaniment residue"
                    .to_owned(),
                parameters: vec![
                    param("row", spec.row_id.as_str()),
                    param("blocks", &spec.partition.block_count().to_string()),
                    param("reading", spec.license.reading_id.as_str()),
                ],
            },
            SerialDeployerInner::AggregateRotation(spec) => SerialDeployerSpec {
                kind: SerialDeployerKind::AggregateRotation,
                label: spec.event_prefix.clone(),
                expected_fact: "a rotated aggregate is reblocked without losing row coverage"
                    .to_owned(),
                parameters: vec![
                    param("row", spec.row_id.as_str()),
                    param("rotation", &spec.rotation.to_string()),
                    param("reading", spec.license.reading_id.as_str()),
                ],
            },
            SerialDeployerInner::SimultaneousForms(spec) => SerialDeployerSpec {
                kind: SerialDeployerKind::SimultaneousForms,
                label: spec.event_prefix.clone(),
                expected_fact: "simultaneous form blocks preserve each form identity and alignment"
                    .to_owned(),
                parameters: vec![
                    param(
                        "forms",
                        &spec
                            .row_ids
                            .iter()
                            .map(RowInstanceId::as_str)
                            .collect::<Vec<_>>()
                            .join(","),
                    ),
                    param("block-size", &spec.block_size.to_string()),
                    param("reading", spec.license.reading_id.as_str()),
                ],
            },
        }
    }

    pub(crate) fn apply(
        &self,
        rows: &BTreeMap<RowInstanceId, RowForm>,
        builder: &mut PlanBuilder,
    ) -> Result<(), SerialDeployError> {
        match &self.inner {
            SerialDeployerInner::Horizontal(spec) => spec.apply(rows, builder),
            SerialDeployerInner::Motivic(spec) => spec.apply(rows, builder),
            SerialDeployerInner::Vertical(spec) => spec.apply(rows, builder),
            SerialDeployerInner::Interlocking(spec) => spec.apply(rows, builder),
            SerialDeployerInner::MelodyAccompaniment(spec) => spec.apply(rows, builder),
            SerialDeployerInner::AggregateRotation(spec) => spec.apply(rows, builder),
            SerialDeployerInner::SimultaneousForms(spec) => spec.apply(rows, builder),
        }
    }
}

pub(crate) fn param(name: &str, value: &str) -> SerialDeployerParameter {
    SerialDeployerParameter {
        name: name.to_owned(),
        value: value.to_owned(),
    }
}

#[derive(Clone, Debug)]
pub(crate) struct PlanBuilder {
    pub(crate) events: BTreeMap<SerialEventId, PlannedSerialEvent>,
    pub(crate) precedence: Vec<(SerialEventId, SerialEventId)>,
}

impl PlanBuilder {
    pub(crate) fn add_event(
        &mut self,
        deployer: &str,
        event: PlannedSerialEvent,
    ) -> Result<(), SerialDeployError> {
        if self.events.contains_key(&event.id) {
            return Err(SerialDeployError::DuplicateEventId {
                deployer: deployer.to_owned(),
                event_id: event.id,
            });
        }
        self.events.insert(event.id.clone(), event);
        Ok(())
    }

    pub(crate) fn add_precedence(&mut self, before: &SerialEventId, after: &SerialEventId) {
        self.precedence.push((before.clone(), after.clone()));
    }
}

/// Inspectable deployment plan composed from ordinary practice rules and deployers.
#[derive(Clone)]
pub struct TechniquePlan {
    id: String,
    rules: Vec<Arc<dyn PracticeRule>>,
    deployers: Vec<SerialDeployer>,
}

impl TechniquePlan {
    /// Starts a builder for one inspectable deployment technique.
    pub fn builder(id: impl Into<String>) -> Result<TechniquePlanBuilder, SerialDeployError> {
        Ok(TechniquePlanBuilder {
            id: validate_technique_id(id)?,
            rules: Vec::new(),
            deployers: Vec::new(),
        })
    }

    /// Returns the stable technique id.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the inspectable practice rule specifications.
    pub fn rule_specs(&self) -> Vec<crate::PracticeRuleSpec> {
        self.practice().rule_specs()
    }

    /// Returns the inspectable deployer specifications.
    pub fn deployer_specs(&self) -> Vec<SerialDeployerSpec> {
        self.deployers.iter().map(SerialDeployer::spec).collect()
    }

    /// Returns the inspectable practice carried alongside the deployers.
    pub fn practice(&self) -> SerialPractice {
        SerialPractice::new(
            crate::PracticeId::new(self.id.clone()).expect("validated technique id"),
            self.rules.clone(),
        )
    }

    /// Deploys the supplied row instances into one validated immutable serial plan.
    pub fn deploy(
        &self,
        rows: BTreeMap<RowInstanceId, RowForm>,
    ) -> Result<SerialPlan, SerialDeployError> {
        let mut builder = PlanBuilder {
            events: BTreeMap::new(),
            precedence: Vec::new(),
        };
        for deployer in &self.deployers {
            deployer.apply(&rows, &mut builder)?;
        }
        SerialPlan::try_new(rows, builder.events, builder.precedence)
            .map_err(|error| SerialDeployError::Plan(error.to_string()))
    }
}

/// Builder for one inspectable technique plan.
pub struct TechniquePlanBuilder {
    id: String,
    rules: Vec<Arc<dyn PracticeRule>>,
    deployers: Vec<SerialDeployer>,
}

impl TechniquePlanBuilder {
    /// Adds one inspectable practice rule.
    pub fn rule(mut self, rule: Arc<dyn PracticeRule>) -> Self {
        self.rules.push(rule);
        self
    }

    /// Adds one inspectable deployment component.
    pub fn deployer(mut self, deployer: SerialDeployer) -> Self {
        self.deployers.push(deployer);
        self
    }

    /// Finishes the technique plan after validating its minimum structure.
    pub fn build(self) -> Result<TechniquePlan, SerialDeployError> {
        if self.deployers.is_empty() {
            return Err(SerialDeployError::EmptyTechnique(self.id));
        }
        Ok(TechniquePlan {
            id: self.id,
            rules: self.rules,
            deployers: self.deployers,
        })
    }
}

/// Convenience helper for the built-in strict aggregate rule.
pub fn strict_aggregate() -> Arc<dyn PracticeRule> {
    Arc::new(BuiltInPracticeRule::aggregate(
        PracticeRuleId::new("rule/aggregate").expect("static rule id"),
    ))
}

pub(crate) fn require_row(
    deployer: &str,
    rows: &BTreeMap<RowInstanceId, RowForm>,
    row_id: &RowInstanceId,
) -> Result<(), SerialDeployError> {
    if rows.contains_key(row_id) {
        Ok(())
    } else {
        Err(SerialDeployError::UnknownRow {
            deployer: deployer.to_owned(),
            row_id: row_id.clone(),
        })
    }
}

pub(crate) fn require_voices(
    deployer: &str,
    expected: usize,
    actual: usize,
) -> Result<(), SerialDeployError> {
    if expected == actual {
        Ok(())
    } else {
        Err(SerialDeployError::VoiceCountMismatch {
            deployer: deployer.to_owned(),
            expected,
            actual,
        })
    }
}

pub(crate) fn structural_event(
    event_id: SerialEventId,
    ordinals: &[u8],
    row_id: RowInstanceId,
    voice: VoiceId,
    rationale: String,
    license: StructuralLicense,
    placement: EventPlacement,
) -> PlannedSerialEvent {
    PlannedSerialEvent {
        id: event_id,
        ordinals: ordinals
            .iter()
            .copied()
            .map(|ordinal| OrdinalRef::new(row_id.clone(), usize::from(ordinal)))
            .collect(),
        role: SerialRole::Structural,
        origin: SerialOrigin::Structural { rationale },
        voice,
        placement,
        parents: Vec::new(),
        licenses: vec![license],
    }
}