sim-lib-music-core 0.2.0

Core music object model for notes, rests, scores, rolls, players, and arrangers.
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
use sim_kernel::{Error, Expr, Result, Symbol};

use crate::arranger::{
    Arranger, ArrangerPlacement, FilterRef, PitchRemap, PlacementTransform, PlayableRef,
    StretchPolicy, TracePolicy,
};
use crate::arranger_music_expr::{music_from_expr, music_to_expr};
use crate::{LaneId, LaneTarget, Pitch, PitchClass, Time};

const NS: &str = "music/arranger";

impl Arranger {
    /// Encodes the arrangement as a tagged `music/arranger` expression map.
    pub fn to_expr(&self) -> Expr {
        map(vec![
            ("tag", tag_expr("arranger")),
            (
                "lanes",
                Expr::Vector(
                    self.lanes
                        .iter()
                        .map(|lane| Expr::String(lane.0.clone()))
                        .collect(),
                ),
            ),
            (
                "placements",
                Expr::Vector(
                    self.placements
                        .iter()
                        .map(ArrangerPlacement::to_expr)
                        .collect(),
                ),
            ),
        ])
    }

    /// Decodes an arrangement from a tagged `music/arranger` expression map.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let entries = expr_map(expr, "arranger")?;
        expect_tag(entries, "arranger", "arranger")?;
        let lanes = optional_vector(entries, "lanes")?
            .iter()
            .map(|expr| Ok(LaneId::new(expr_string(expr, "lane")?.to_owned())))
            .collect::<Result<Vec<_>>>()?;
        let placements = expr_vector(lookup_required(entries, "placements")?, "placements")?
            .iter()
            .map(ArrangerPlacement::from_expr)
            .collect::<Result<Vec<_>>>()?;
        Self::new(placements, lanes)
    }
}

impl ArrangerPlacement {
    /// Encodes the placement as a tagged `placement` expression map.
    pub fn to_expr(&self) -> Expr {
        map(vec![
            ("tag", tag_expr("placement")),
            ("id", Expr::Symbol(self.id.clone())),
            ("playable", self.playable.to_expr()),
            ("at", time_expr(self.at)),
            (
                "duration",
                self.duration.map(time_expr).unwrap_or(Expr::Nil),
            ),
            ("lane", Expr::String(self.lane.0.clone())),
            (
                "targets",
                Expr::Vector(self.targets.iter().map(lane_target_expr).collect()),
            ),
            ("stretch", self.stretch.to_expr()),
            (
                "transform",
                Expr::Vector(
                    self.transform
                        .iter()
                        .map(PlacementTransform::to_expr)
                        .collect(),
                ),
            ),
            ("remap-pitch", self.remap_pitch.to_expr()),
            (
                "filter",
                self.filter
                    .as_ref()
                    .map(FilterRef::to_expr)
                    .unwrap_or(Expr::Nil),
            ),
            (
                "seed",
                self.seed
                    .map(|seed| Expr::String(seed.to_string()))
                    .unwrap_or(Expr::Nil),
            ),
            ("trace", Expr::Symbol(self.trace.symbol())),
        ])
    }

    /// Decodes a placement from a tagged `placement` expression map.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let entries = expr_map(expr, "arranger placement")?;
        expect_tag(entries, "placement", "arranger placement")?;
        let duration = match lookup_required(entries, "duration")? {
            Expr::Nil => None,
            expr => Some(time_from_expr(expr)?),
        };
        let placement = Self {
            id: expr_symbol(lookup_required(entries, "id")?, "placement id")?,
            playable: PlayableRef::from_expr(lookup_required(entries, "playable")?)?,
            at: time_from_expr(lookup_required(entries, "at")?)?,
            duration,
            lane: LaneId::new(expr_string(lookup_required(entries, "lane")?, "lane")?.to_owned()),
            targets: expr_vector(lookup_required(entries, "targets")?, "placement targets")?
                .iter()
                .map(lane_target_from_expr)
                .collect::<Result<Vec<_>>>()?,
            stretch: StretchPolicy::from_expr(lookup_required(entries, "stretch")?)?,
            transform: expr_vector(lookup_required(entries, "transform")?, "transform")?
                .iter()
                .map(PlacementTransform::from_expr)
                .collect::<Result<Vec<_>>>()?,
            remap_pitch: PitchRemap::from_expr(lookup_required(entries, "remap-pitch")?)?,
            filter: match lookup_required(entries, "filter")? {
                Expr::Nil => None,
                expr => Some(FilterRef::from_expr(expr)?),
            },
            seed: match lookup_required(entries, "seed")? {
                Expr::Nil => None,
                expr => Some(expr_u64(expr, "placement seed")?),
            },
            trace: TracePolicy::from_expr(lookup_required(entries, "trace")?)?,
        };
        placement.validate()?;
        Ok(placement)
    }
}

impl PlayableRef {
    /// Encodes the reference as a tagged `playable-ref` expression map.
    pub fn to_expr(&self) -> Expr {
        match self {
            Self::Inline(music) => map(vec![
                ("tag", tag_expr("playable-ref")),
                ("kind", tag_expr("inline")),
                ("value", music_to_expr(music)),
            ]),
            Self::Symbol(symbol) => map(vec![
                ("tag", tag_expr("playable-ref")),
                ("kind", tag_expr("symbol")),
                ("value", Expr::Symbol(symbol.clone())),
            ]),
        }
    }

    /// Decodes the reference from a tagged `playable-ref` expression map.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let entries = expr_map(expr, "playable ref")?;
        expect_tag(entries, "playable-ref", "playable ref")?;
        match symbol_name(lookup_required(entries, "kind")?, "playable ref kind")? {
            "inline" => Ok(Self::inline(music_from_expr(lookup_required(
                entries, "value",
            )?)?)),
            "symbol" => Ok(Self::symbol(expr_symbol(
                lookup_required(entries, "value")?,
                "playable symbol",
            )?)),
            _ => Err(Error::Eval("playable ref kind is invalid".to_owned())),
        }
    }
}

impl FilterRef {
    /// Encodes the filter as a tagged `filter` expression map.
    pub fn to_expr(&self) -> Expr {
        map(vec![
            ("tag", tag_expr("filter")),
            ("id", Expr::Symbol(self.id.clone())),
            (
                "keep-lanes",
                Expr::Vector(
                    self.keep_lanes
                        .iter()
                        .map(|lane| Expr::String(lane.0.clone()))
                        .collect(),
                ),
            ),
        ])
    }

    /// Decodes the filter from a tagged `filter` expression map.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let entries = expr_map(expr, "filter")?;
        expect_tag(entries, "filter", "filter")?;
        Ok(Self {
            id: expr_symbol(lookup_required(entries, "id")?, "filter id")?,
            keep_lanes: expr_vector(lookup_required(entries, "keep-lanes")?, "keep lanes")?
                .iter()
                .map(|expr| Ok(LaneId::new(expr_string(expr, "keep lane")?.to_owned())))
                .collect::<Result<Vec<_>>>()?,
        })
    }
}

impl StretchPolicy {
    fn to_expr(&self) -> Expr {
        match self {
            Self::None => map(vec![
                ("tag", tag_expr("stretch")),
                ("kind", tag_expr("none")),
            ]),
            Self::TempoRatio(ratio) => map(vec![
                ("tag", tag_expr("stretch")),
                ("kind", tag_expr("tempo-ratio")),
                ("value", time_expr(*ratio)),
            ]),
            Self::TimeRatio(ratio) => map(vec![
                ("tag", tag_expr("stretch")),
                ("kind", tag_expr("time-ratio")),
                ("value", time_expr(*ratio)),
            ]),
            Self::FitToDuration => map(vec![
                ("tag", tag_expr("stretch")),
                ("kind", tag_expr("fit")),
            ]),
        }
    }

    fn from_expr(expr: &Expr) -> Result<Self> {
        let entries = expr_map(expr, "stretch policy")?;
        expect_tag(entries, "stretch", "stretch policy")?;
        match symbol_name(lookup_required(entries, "kind")?, "stretch kind")? {
            "none" => Ok(Self::None),
            "tempo-ratio" => Ok(Self::TempoRatio(time_from_expr(lookup_required(
                entries, "value",
            )?)?)),
            "time-ratio" => Ok(Self::TimeRatio(time_from_expr(lookup_required(
                entries, "value",
            )?)?)),
            "fit" => Ok(Self::FitToDuration),
            _ => Err(Error::Eval("stretch policy kind is invalid".to_owned())),
        }
    }
}

impl PlacementTransform {
    fn to_expr(&self) -> Expr {
        match self {
            Self::TransposeSemitones(semitones) => {
                value_transform("transpose-semitones", *semitones)
            }
            Self::TransposeOctaves(octaves) => value_transform("transpose-octaves", *octaves),
            Self::InvertAroundPitch(axis) => map(vec![
                ("tag", tag_expr("transform")),
                ("kind", tag_expr("invert-pitch")),
                ("value", pitch_expr(*axis)),
            ]),
            Self::InvertAroundPitchClass(axis) => {
                value_transform("invert-pitch-class", axis.value())
            }
            Self::Retrograde => map(vec![
                ("tag", tag_expr("transform")),
                ("kind", tag_expr("retrograde")),
            ]),
        }
    }

    fn from_expr(expr: &Expr) -> Result<Self> {
        let entries = expr_map(expr, "placement transform")?;
        expect_tag(entries, "transform", "placement transform")?;
        match symbol_name(lookup_required(entries, "kind")?, "transform kind")? {
            "transpose-semitones" => Ok(Self::TransposeSemitones(expr_i32(
                lookup_required(entries, "value")?,
                "transpose semitones",
            )?)),
            "transpose-octaves" => Ok(Self::TransposeOctaves(expr_i16(
                lookup_required(entries, "value")?,
                "transpose octaves",
            )?)),
            "invert-pitch" => Ok(Self::InvertAroundPitch(pitch_from_expr(lookup_required(
                entries, "value",
            )?)?)),
            "invert-pitch-class" => Ok(Self::InvertAroundPitchClass(
                PitchClass::new(expr_u8(lookup_required(entries, "value")?, "pitch class")?)
                    .map_err(|_| Error::Eval("pitch class is invalid".to_owned()))?,
            )),
            "retrograde" => Ok(Self::Retrograde),
            _ => Err(Error::Eval(
                "placement transform kind is invalid".to_owned(),
            )),
        }
    }
}

impl PitchRemap {
    fn to_expr(&self) -> Expr {
        match self {
            Self::None => map(vec![
                ("tag", tag_expr("pitch-remap")),
                ("kind", tag_expr("none")),
            ]),
            Self::Chromatic(semitones) => value_remap("chromatic", *semitones),
            Self::PitchClass { from, to } => map(vec![
                ("tag", tag_expr("pitch-remap")),
                ("kind", tag_expr("pitch-class")),
                ("from", Expr::String(from.value().to_string())),
                ("to", Expr::String(to.value().to_string())),
            ]),
            Self::DrumKey(items) => map(vec![
                ("tag", tag_expr("pitch-remap")),
                ("kind", tag_expr("drum-key")),
                (
                    "items",
                    Expr::Vector(items.iter().map(drum_key_expr).collect()),
                ),
            ]),
            Self::ScaleDegree(symbol) => symbolic_remap_expr("scale-degree", symbol),
            Self::ChordTone(symbol) => symbolic_remap_expr("chord-tone", symbol),
            Self::Tuning(symbol) => symbolic_remap_expr("tuning", symbol),
            Self::Vector(symbol) => symbolic_remap_expr("vector", symbol),
            Self::Matrix(symbol) => symbolic_remap_expr("matrix", symbol),
            Self::Callable(symbol) => symbolic_remap_expr("callable", symbol),
        }
    }

    fn from_expr(expr: &Expr) -> Result<Self> {
        let entries = expr_map(expr, "pitch remap")?;
        expect_tag(entries, "pitch-remap", "pitch remap")?;
        match symbol_name(lookup_required(entries, "kind")?, "pitch remap kind")? {
            "none" => Ok(Self::None),
            "chromatic" => Ok(Self::Chromatic(expr_i32(
                lookup_required(entries, "value")?,
                "chromatic remap",
            )?)),
            "pitch-class" => Ok(Self::PitchClass {
                from: PitchClass::new(expr_u8(lookup_required(entries, "from")?, "from")?)
                    .map_err(|_| Error::Eval("source pitch class is invalid".to_owned()))?,
                to: PitchClass::new(expr_u8(lookup_required(entries, "to")?, "to")?)
                    .map_err(|_| Error::Eval("target pitch class is invalid".to_owned()))?,
            }),
            "drum-key" => expr_vector(lookup_required(entries, "items")?, "drum key items")?
                .iter()
                .map(drum_key_from_expr)
                .collect::<Result<Vec<_>>>()
                .map(Self::DrumKey),
            "scale-degree" => symbolic_remap(entries, Self::ScaleDegree),
            "chord-tone" => symbolic_remap(entries, Self::ChordTone),
            "tuning" => symbolic_remap(entries, Self::Tuning),
            "vector" => symbolic_remap(entries, Self::Vector),
            "matrix" => symbolic_remap(entries, Self::Matrix),
            "callable" => symbolic_remap(entries, Self::Callable),
            _ => Err(Error::Eval("pitch remap kind is invalid".to_owned())),
        }
    }
}

impl TracePolicy {
    /// Returns the `music/arranger` symbol that names this trace policy.
    pub fn symbol(self) -> Symbol {
        match self {
            Self::Off => tag("trace-off"),
            Self::Diagnostics => tag("trace-diagnostics"),
            Self::Full => tag("trace-full"),
        }
    }

    fn from_expr(expr: &Expr) -> Result<Self> {
        match symbol_name(expr, "trace policy")? {
            "trace-off" => Ok(Self::Off),
            "trace-diagnostics" => Ok(Self::Diagnostics),
            "trace-full" => Ok(Self::Full),
            _ => Err(Error::Eval("trace policy is invalid".to_owned())),
        }
    }
}

impl PartialEq for Arranger {
    fn eq(&self, other: &Self) -> bool {
        self.to_expr().canonical_eq(&other.to_expr())
    }
}

impl Eq for Arranger {}

impl PartialEq for ArrangerPlacement {
    fn eq(&self, other: &Self) -> bool {
        self.to_expr().canonical_eq(&other.to_expr())
    }
}

impl Eq for ArrangerPlacement {}

impl PartialEq for PlayableRef {
    fn eq(&self, other: &Self) -> bool {
        self.to_expr().canonical_eq(&other.to_expr())
    }
}

impl Eq for PlayableRef {}

fn time_expr(time: Time) -> Expr {
    map(vec![
        ("numer", Expr::String(time.numer().to_string())),
        ("denom", Expr::String(time.denom().to_string())),
    ])
}

fn time_from_expr(expr: &Expr) -> Result<Time> {
    let entries = expr_map(expr, "time")?;
    let denominator = expr_i64(lookup_required(entries, "denom")?, "time denominator")?;
    if denominator == 0 {
        return Err(Error::Eval("time denominator cannot be zero".to_owned()));
    }
    Ok(Time::new(
        expr_i64(lookup_required(entries, "numer")?, "time numerator")?,
        denominator,
    ))
}

fn pitch_expr(pitch: Pitch) -> Expr {
    Expr::String(
        pitch
            .to_midi()
            .map(|midi| format!("midi:{midi}"))
            .unwrap_or_else(|| format!("semitone:{}", pitch.semitone())),
    )
}

fn pitch_from_expr(expr: &Expr) -> Result<Pitch> {
    let value = expr_string(expr, "pitch")?;
    if let Some(midi) = value.strip_prefix("midi:") {
        return Ok(Pitch::from_midi(
            midi.parse::<u8>()
                .map_err(|_| Error::Eval("MIDI pitch is invalid".to_owned()))?,
        ));
    }
    if let Some(semitone) = value.strip_prefix("semitone:") {
        return Ok(Pitch::from_semitone(semitone.parse::<i32>().map_err(
            |_| Error::Eval("semitone pitch is invalid".to_owned()),
        )?));
    }
    crate::parse_pitch(value).map_err(|_| Error::Eval("pitch is invalid".to_owned()))
}

fn lane_target_expr(target: &LaneTarget) -> Expr {
    match target {
        LaneTarget::Instrument(symbol) => target_expr("instrument", symbol),
        LaneTarget::Stream(symbol) => target_expr("stream", symbol),
        LaneTarget::Control(symbol) => target_expr("control", symbol),
        LaneTarget::None => map(vec![("kind", tag_expr("none"))]),
    }
}

fn lane_target_from_expr(expr: &Expr) -> Result<LaneTarget> {
    let entries = expr_map(expr, "lane target")?;
    match symbol_name(lookup_required(entries, "kind")?, "lane target kind")? {
        "instrument" => Ok(LaneTarget::Instrument(expr_symbol(
            lookup_required(entries, "symbol")?,
            "target symbol",
        )?)),
        "stream" => Ok(LaneTarget::Stream(expr_symbol(
            lookup_required(entries, "symbol")?,
            "target symbol",
        )?)),
        "control" => Ok(LaneTarget::Control(expr_symbol(
            lookup_required(entries, "symbol")?,
            "target symbol",
        )?)),
        "none" => Ok(LaneTarget::None),
        _ => Err(Error::Eval("lane target kind is invalid".to_owned())),
    }
}

fn value_transform<T: ToString>(kind: &'static str, value: T) -> Expr {
    map(vec![
        ("tag", tag_expr("transform")),
        ("kind", tag_expr(kind)),
        ("value", Expr::String(value.to_string())),
    ])
}

fn value_remap<T: ToString>(kind: &'static str, value: T) -> Expr {
    map(vec![
        ("tag", tag_expr("pitch-remap")),
        ("kind", tag_expr(kind)),
        ("value", Expr::String(value.to_string())),
    ])
}

fn drum_key_expr((from, to): &(u8, u8)) -> Expr {
    map(vec![
        ("from", Expr::String(from.to_string())),
        ("to", Expr::String(to.to_string())),
    ])
}

fn drum_key_from_expr(expr: &Expr) -> Result<(u8, u8)> {
    let item = expr_map(expr, "drum key item")?;
    Ok((
        expr_u8(lookup_required(item, "from")?, "drum key from")?,
        expr_u8(lookup_required(item, "to")?, "drum key to")?,
    ))
}

fn symbolic_remap_expr(kind: &'static str, symbol: &Symbol) -> Expr {
    map(vec![
        ("tag", tag_expr("pitch-remap")),
        ("kind", tag_expr(kind)),
        ("symbol", Expr::Symbol(symbol.clone())),
    ])
}

fn symbolic_remap(
    entries: &[(Expr, Expr)],
    build: impl FnOnce(Symbol) -> PitchRemap,
) -> Result<PitchRemap> {
    Ok(build(expr_symbol(
        lookup_required(entries, "symbol")?,
        "pitch remap symbol",
    )?))
}

fn target_expr(kind: &'static str, symbol: &Symbol) -> Expr {
    map(vec![
        ("kind", tag_expr(kind)),
        ("symbol", Expr::Symbol(symbol.clone())),
    ])
}

fn map(entries: Vec<(&'static str, Expr)>) -> Expr {
    Expr::Map(
        entries
            .into_iter()
            .map(|(key, value)| (field(key), value))
            .collect(),
    )
}

fn field(name: &'static str) -> Expr {
    sim_value::build::qsym(NS, name)
}

fn tag(name: &'static str) -> Symbol {
    Symbol::qualified(NS, name)
}

fn tag_expr(name: &'static str) -> Expr {
    Expr::Symbol(tag(name))
}

fn expr_map<'a>(expr: &'a Expr, context: &str) -> Result<&'a [(Expr, Expr)]> {
    match expr {
        Expr::Map(entries) => Ok(entries),
        _ => Err(Error::Eval(format!("{context} must be a map"))),
    }
}

fn expr_vector<'a>(expr: &'a Expr, context: &str) -> Result<&'a [Expr]> {
    match expr {
        Expr::Vector(items) => Ok(items),
        _ => Err(Error::Eval(format!("{context} must be a vector"))),
    }
}

fn optional_vector<'a>(entries: &'a [(Expr, Expr)], name: &str) -> Result<&'a [Expr]> {
    match lookup(entries, name) {
        Some(expr) => expr_vector(expr, name),
        None => Ok(&[]),
    }
}

fn lookup_required<'a>(entries: &'a [(Expr, Expr)], name: &str) -> Result<&'a Expr> {
    lookup(entries, name).ok_or_else(|| Error::Eval(format!("arranger field is missing: {name}")))
}

fn lookup<'a>(entries: &'a [(Expr, Expr)], name: &str) -> Option<&'a Expr> {
    entries.iter().find_map(|(key, value)| match key {
        Expr::Symbol(symbol)
            if symbol.namespace.as_deref() == Some(NS) && symbol.name.as_ref() == name =>
        {
            Some(value)
        }
        _ => None,
    })
}

fn expect_tag(entries: &[(Expr, Expr)], name: &'static str, context: &str) -> Result<()> {
    match lookup(entries, "tag") {
        Some(Expr::Symbol(symbol))
            if symbol.namespace.as_deref() == Some(NS) && symbol.name.as_ref() == name =>
        {
            Ok(())
        }
        Some(_) => Err(Error::Eval(format!("{context} tag is invalid"))),
        None => Err(Error::Eval(format!("{context} tag is missing"))),
    }
}

fn symbol_name<'a>(expr: &'a Expr, context: &str) -> Result<&'a str> {
    match expr {
        Expr::Symbol(symbol) if symbol.namespace.as_deref() == Some(NS) => Ok(symbol.name.as_ref()),
        _ => Err(Error::Eval(format!("{context} must be an arranger symbol"))),
    }
}

fn expr_symbol(expr: &Expr, context: &str) -> Result<Symbol> {
    match expr {
        Expr::Symbol(symbol) => Ok(symbol.clone()),
        _ => Err(Error::Eval(format!("{context} must be a symbol"))),
    }
}

fn expr_string<'a>(expr: &'a Expr, context: &str) -> Result<&'a str> {
    match expr {
        Expr::String(value) => Ok(value),
        _ => Err(Error::Eval(format!("{context} must be a string"))),
    }
}

fn expr_i16(expr: &Expr, context: &str) -> Result<i16> {
    parse_number(expr, context)
}

fn expr_i32(expr: &Expr, context: &str) -> Result<i32> {
    parse_number(expr, context)
}

fn expr_i64(expr: &Expr, context: &str) -> Result<i64> {
    parse_number(expr, context)
}

fn expr_u8(expr: &Expr, context: &str) -> Result<u8> {
    parse_number(expr, context)
}

fn expr_u64(expr: &Expr, context: &str) -> Result<u64> {
    parse_number(expr, context)
}

fn parse_number<T>(expr: &Expr, context: &str) -> Result<T>
where
    T: std::str::FromStr,
{
    let text = match expr {
        Expr::String(value) => value.as_str(),
        Expr::Number(value) => value.canonical.as_str(),
        _ => return Err(Error::Eval(format!("{context} must be a number"))),
    };
    text.parse()
        .map_err(|_| Error::Eval(format!("{context} is invalid")))
}