conformal_core 0.3.7

Code shared between wrappers in conformal framework.
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
//! This holds utilities for serializing the state of a set of parameters.
//!
//! Our serialization model allows backwards compatibility, but _not_ forwards compatibility.
//! This means that a new version of the plug-in will be able to load the state of an old version,
//! but an old version will not be able to load the state of a new version.
//!
//! # Data model changes
//!
//! The data format is designed to allow certain changes without explicit migrations
//!
//! - Adding a parameter (given the parameter's default setting matches the old behavior).
//!   - Note that the parameter's unique id must have never been used before
//! - Removing a parameter.
//! - Changing the default value of a parameter.
//!
//! If your parameter is _not_ automatable, some additional changes are allowed without
//! explicit migrations:
//!
//! - Re-ordering enum values.
//! - Increasing the allowed range of a numeric parameter.
//! - Adding a new enum values to the end of the list.
//!
//! Other changes will need explicit migrations (to be supported after #19).
//!
//! ## Automatable parameter restrictions
//!
//! If your parameter is automatable, the following changes are _not_ allowed,
//! even with explicit migrations. These restrictions are due to the vst3 format's
//! data model for automation.
//!
//! - Re-ordering or removing existing enum values.
//! - Changing the range of a numeric parameter.
//! - Adding a value to an enum parameter.
//! - Changing the type of a parameter.
//!
//! If you need to make one of these changes, you should remove the parameter and
//! create a new parameter with a new ID instead.

use std::collections::HashMap;

use conformal_component::parameters::{TypeSpecificInfoRef, Value as ParameterValue};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
enum Value {
    Numeric(f32),
    Enum(String),
    Switch(bool),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Snapshot {
    values: HashMap<String, Value>,
}

/// This contains metadata needed to serialize a parameter.
///
/// Note in particular this does not contain the range or default
/// of the parameter - this ensures that increasing the range or
/// changing the default value of a parameter does not require
/// a migration!
pub enum WriteInfoRef<I> {
    Numeric {},
    Enum { values: I },
    Switch {},
}

impl<'a, S: AsRef<str>> From<TypeSpecificInfoRef<'a, S>> for WriteInfoRef<&'a [S]> {
    fn from(info: TypeSpecificInfoRef<'a, S>) -> Self {
        match info {
            TypeSpecificInfoRef::Numeric { .. } => Self::Numeric {},
            TypeSpecificInfoRef::Enum { values, .. } => Self::Enum { values },
            TypeSpecificInfoRef::Switch { .. } => Self::Switch {},
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ReadInfoRef<I> {
    Numeric {
        default: f32,
        valid_range: std::ops::RangeInclusive<f32>,
    },
    Enum {
        default: u32,
        values: I,
    },
    Switch {
        default: bool,
    },
}

impl<'a, S: AsRef<str>> From<TypeSpecificInfoRef<'a, S>> for ReadInfoRef<std::slice::Iter<'a, S>> {
    fn from(info: TypeSpecificInfoRef<'a, S>) -> Self {
        match info {
            TypeSpecificInfoRef::Numeric {
                default,
                valid_range,
                ..
            } => Self::Numeric {
                default,
                valid_range,
            },
            TypeSpecificInfoRef::Enum {
                default, values, ..
            } => Self::Enum {
                default,
                values: values.iter(),
            },
            TypeSpecificInfoRef::Switch { default, .. } => Self::Switch { default },
        }
    }
}

impl super::Snapshot {
    /// Convert a snapshot to a serialized snapshot.
    ///
    /// This will allocate.
    ///
    /// Note that this will only fail if there is an inconsistency between the snapshot and the
    /// provided info.
    pub fn into_serialize<'a, I: IntoIterator<Item = &'a str>>(
        self,
        lookup: impl Fn(&str) -> Option<WriteInfoRef<I>>,
    ) -> Option<Snapshot> {
        let mut values = HashMap::new();
        values.reserve(self.values.len());
        for (id, value) in self.values {
            let info = lookup(id.as_str())?;
            let serialized_value = match (info, value) {
                (WriteInfoRef::Numeric {}, ParameterValue::Numeric(value)) => {
                    Some(Value::Numeric(value))
                }
                (WriteInfoRef::Enum { .. }, ParameterValue::Enum(value)) => {
                    Some(Value::Enum(value))
                }
                (WriteInfoRef::Switch {}, ParameterValue::Switch(value)) => {
                    Some(Value::Switch(value))
                }
                _ => None,
            }?;

            values.insert(id.clone(), serialized_value);
        }

        Some(Snapshot { values })
    }

    pub fn into_serialize_no_enum(
        self,
        lookup: impl Fn(&str) -> Option<WriteInfoRef<std::iter::Empty<&'static str>>>,
    ) -> Option<Snapshot> {
        self.into_serialize(lookup)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum SnapshotCorruptionError {
    /// Changing the type of a parameter requires a migration, so it's an error
    /// if we try to load a snapshot that has a different type for a parameter.
    IncompatibleType(String),
}

#[derive(Debug, Clone, PartialEq)]
pub enum DeserializationError {
    /// The snapshot was saved with a newer version of the plug-in, so it's not
    /// compatible with this version.
    VersionTooNew(),

    Corrupted(SnapshotCorruptionError),
}

impl Snapshot {
    /// Convert a serialized snapshot to a snapshot.
    ///
    /// # Panics
    ///
    /// Panics if `all_params` is corrupted (e.g., has a default value for the enum that
    /// is outside the values range)
    ///
    /// # Errors
    ///
    /// Will return `DeserializationError::VersionTooNew` error if the serialized snapshot has
    /// parameters that are out of range,, or `DeserializationError::Corrupted` if any parameters
    /// were the wrong type.
    pub fn into_snapshot<'a, I: IntoIterator<Item = &'a str> + Clone>(
        mut self,
        all_params: impl IntoIterator<Item = (&'a str, ReadInfoRef<I>)>,
    ) -> Result<super::Snapshot, DeserializationError> {
        let mut values = HashMap::new();
        for (id, info) in all_params {
            let serialized_value = self.values.get_mut(id);
            let value = match (info, serialized_value) {
                (ReadInfoRef::Numeric { valid_range, .. }, Some(Value::Numeric(value))) => {
                    if valid_range.contains(value) {
                        Ok(ParameterValue::Numeric(*value))
                    } else {
                        Err(DeserializationError::VersionTooNew())
                    }
                }
                (ReadInfoRef::Numeric { default, .. }, None) => {
                    Ok(ParameterValue::Numeric(default))
                }
                (ReadInfoRef::Enum { values, .. }, Some(Value::Enum(value))) => {
                    if values.into_iter().any(|v| v == value.as_str()) {
                        Ok(ParameterValue::Enum(std::mem::take(value)))
                    } else {
                        Err(DeserializationError::VersionTooNew())
                    }
                }
                (ReadInfoRef::Enum { default, values }, None) => Ok(ParameterValue::Enum(
                    values
                        .clone()
                        .into_iter()
                        .nth(default as usize)
                        .unwrap()
                        .to_string(),
                )),
                (ReadInfoRef::Switch { .. }, Some(Value::Switch(value))) => {
                    Ok(ParameterValue::Switch(*value))
                }
                (ReadInfoRef::Switch { default, .. }, None) => Ok(ParameterValue::Switch(default)),
                // Note that changing parameter types requires a migration, so
                // if the type in the snapshot doesn't match the type in the info,
                // it's invalid.
                _ => Err(DeserializationError::Corrupted(
                    SnapshotCorruptionError::IncompatibleType(id.to_string()),
                )),
            }?;

            values.insert(id.to_owned(), value);
        }

        Ok(super::Snapshot { values })
    }

    #[cfg(test)]
    fn into_snapshot_no_enums<'a>(
        self,
        all_params: impl IntoIterator<Item = (&'a str, ReadInfoRef<std::iter::Empty<&'a str>>)>,
    ) -> Result<super::Snapshot, DeserializationError> {
        self.into_snapshot(all_params)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::parameters::Snapshot;
    use conformal_component::parameters::Value;

    fn to_hash<'a, T, I: IntoIterator<Item = (&'a str, T)>>(i: I) -> HashMap<String, T> {
        i.into_iter().map(|(k, v)| (k.to_string(), v)).collect()
    }

    #[test]
    fn defends_against_missing_info_in_serialize() {
        let snapshot = Snapshot {
            values: to_hash([("numeric", Value::Numeric(0.0))]),
        };
        let lookup = |_: &_| None;
        assert!(snapshot.into_serialize_no_enum(lookup).is_none());
    }

    #[test]
    fn defends_against_wrong_type_serialize() {
        let snapshot = Snapshot {
            values: to_hash([("numeric", Value::Numeric(0.0))]),
        };
        let lookup = |id: &_| match id {
            "0" => Some(super::WriteInfoRef::Enum {
                values: ["a", "b", "c"],
            }),
            _ => None,
        };
        let serialized = snapshot.into_serialize(lookup);
        assert!(serialized.is_none());
    }

    #[test]
    fn roundtrip() {
        let snapshot = Snapshot {
            values: to_hash([
                ("numeric", Value::Numeric(0.0)),
                ("enum", Value::Enum("b".to_string())),
                ("switch", Value::Switch(true)),
            ]),
        };
        let lookup = |id: &_| match id {
            "numeric" => Some(super::WriteInfoRef::Numeric {}),
            "enum" => Some(super::WriteInfoRef::Enum {
                values: ["a", "b", "c"],
            }),
            "switch" => Some(super::WriteInfoRef::Switch {}),
            _ => None,
        };
        let serialized = snapshot.clone().into_serialize(lookup);
        assert!(serialized.is_some());

        let deserialized = serialized.unwrap().into_snapshot([
            (
                "numeric",
                super::ReadInfoRef::Numeric {
                    default: 0.0,
                    valid_range: 0.0..=1.0,
                },
            ),
            (
                "enum",
                super::ReadInfoRef::Enum {
                    default: 0,
                    values: ["a", "b", "c"],
                },
            ),
            ("switch", super::ReadInfoRef::Switch { default: true }),
        ]);
        assert!(deserialized.is_ok());

        assert_eq!(snapshot, deserialized.unwrap());
    }

    #[test]
    fn adding_parameters_without_migration() {
        let snapshot = Snapshot { values: [].into() };
        let lookup = |_: &_| None;
        let serialized = snapshot.into_serialize_no_enum(&lookup);
        assert!(serialized.is_some());

        let deserialized = serialized.unwrap().into_snapshot([
            (
                "numeric",
                super::ReadInfoRef::Numeric {
                    default: 0.0,
                    valid_range: 0.0..=1.0,
                },
            ),
            (
                "enum",
                super::ReadInfoRef::Enum {
                    default: 0,
                    values: ["a", "b", "c"],
                },
            ),
            ("switch", super::ReadInfoRef::Switch { default: true }),
        ]);
        assert!(deserialized.is_ok());

        assert_eq!(
            deserialized.unwrap(),
            Snapshot {
                values: to_hash([
                    ("numeric", Value::Numeric(0.0)),
                    ("enum", Value::Enum("a".to_string())),
                    ("switch", Value::Switch(true)),
                ])
            }
        );
    }

    #[test]
    fn changing_parameter_type_without_migration_fails() {
        let snapshot = Snapshot {
            values: to_hash([("numeric", Value::Numeric(0.0))]),
        };
        let lookup = |id: &_| match id {
            "numeric" => Some(super::WriteInfoRef::Numeric {}),
            _ => None,
        };
        let serialized = snapshot.into_serialize_no_enum(lookup);
        assert!(serialized.is_some());

        let deserialized = serialized.unwrap().into_snapshot([(
            "numeric",
            super::ReadInfoRef::Enum {
                default: 0,
                values: ["a", "b", "c"],
            },
        )]);
        assert!(deserialized.is_err());
    }

    #[test]
    fn add_new_enum_parameter_without_migration() {
        let snapshot = Snapshot {
            values: to_hash([("enum", Value::Enum("b".to_string()))]),
        };
        let lookup = |_: &_| {
            Some(super::WriteInfoRef::Enum {
                values: ["a", "b", "c"],
            })
        };
        let serialized = snapshot.into_serialize(&lookup);
        assert!(serialized.is_some());

        let deserialized = serialized.unwrap().into_snapshot([(
            "enum",
            super::ReadInfoRef::Enum {
                default: 0,
                values: ["a", "b", "c", "d"],
            },
        )]);
        assert!(deserialized.is_ok());

        assert_eq!(
            deserialized.unwrap(),
            Snapshot {
                values: to_hash([("enum", Value::Enum("b".to_string())),])
            }
        );
    }

    #[test]
    fn removing_parameter_without_migration() {
        let snapshot = Snapshot {
            values: to_hash([("enum", Value::Enum("b".to_string()))]),
        };
        let lookup = |_: &_| {
            Some(super::WriteInfoRef::Enum {
                values: ["a", "b", "c"],
            })
        };
        let serialized = snapshot.into_serialize(&lookup);
        assert!(serialized.is_some());

        let deserialized = serialized
            .unwrap()
            .into_snapshot::<std::iter::Empty<&'_ str>>([]);
        assert!(deserialized.is_ok());

        assert_eq!(deserialized.unwrap(), Snapshot { values: [].into() });
    }

    #[test]
    fn re_ordering_enum_without_migration() {
        let snapshot = Snapshot {
            values: to_hash([("enum", Value::Enum("b".to_string()))]),
        };
        let lookup = |_: &_| {
            Some(super::WriteInfoRef::Enum {
                values: ["a", "b", "c"],
            })
        };
        let serialized = snapshot.into_serialize(&lookup);
        assert!(serialized.is_some());

        let deserialized = serialized.unwrap().into_snapshot([(
            "enum",
            super::ReadInfoRef::Enum {
                default: 0,
                values: ["b", "c", "a"],
            },
        )]);
        assert!(deserialized.is_ok());

        assert_eq!(
            deserialized.unwrap(),
            Snapshot {
                values: to_hash([("enum", Value::Enum("b".to_string()))])
            }
        );
    }

    #[test]
    fn narrowing_range_causes_too_new() {
        let snapshot = Snapshot {
            values: to_hash([("numeric", Value::Numeric(0.7))]),
        };
        let lookup = |_: &_| Some(super::WriteInfoRef::Numeric {});
        let serialized = snapshot.into_serialize_no_enum(&lookup);
        assert!(serialized.is_some());

        let deserialized = serialized.unwrap().into_snapshot_no_enums([(
            "numeric",
            super::ReadInfoRef::Numeric {
                default: 0.0,
                valid_range: 0.0..=0.5,
            },
        )]);
        assert_eq!(
            deserialized,
            Err(super::DeserializationError::VersionTooNew())
        );
    }

    #[test]
    fn removing_enum_value_causes_too_new() {
        let snapshot = Snapshot {
            values: to_hash([("enum", Value::Enum("b".to_string()))]),
        };
        let lookup = |_: &_| {
            Some(super::WriteInfoRef::Enum {
                values: ["a", "b", "c"],
            })
        };
        let serialized = snapshot.into_serialize(&lookup);
        assert!(serialized.is_some());

        let deserialized = serialized.unwrap().into_snapshot([(
            "enum",
            super::ReadInfoRef::Enum {
                default: 0,
                values: ["a", "c"],
            },
        )]);
        assert_eq!(
            deserialized,
            Err(super::DeserializationError::VersionTooNew())
        );
    }
}