Skip to main content

nice_plug_core/plugin/
state.rs

1//! Utilities for saving a [`crate::plugin::Plugin`]'s state. The actual state object is also exposed
2//! to plugins through the [`GuiContext`][crate::prelude::GuiContext].
3
4use serde::{Deserialize, Serialize};
5use std::collections::BTreeMap;
6
7// These state objects are also exposed directly to the plugin so it can do its own internal preset
8// management
9
10/// A plain, unnormalized value for a parameter.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(rename_all = "snake_case")]
13pub enum ParamValue {
14    F32(f32),
15    I32(i32),
16    Bool(bool),
17    /// Only used for enum parameters that have the `#[id = "..."]` attribute set.
18    String(String),
19}
20
21/// A plugin's state so it can be restored at a later point. This object can be serialized and
22/// deserialized using serde.
23///
24/// The fields are stored as `BTreeMap`s so the order in the serialized file is consistent.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct PluginState {
27    /// The plugin version this state was saved with. Right now this is not used, but later versions
28    /// of nice-plug may allow you to modify the plugin state object directly before it is loaded to
29    /// allow migrating plugin states between breaking parameter changes.
30    ///
31    /// # Notes
32    ///
33    /// If the saved state is very old, then this field may be empty.
34    #[serde(default)]
35    pub version: String,
36
37    /// The plugin's parameter values. These are stored unnormalized. This means the old values will
38    /// be recalled when when the parameter's range gets increased. Doing so may still mess with
39    /// parameter automation though, depending on how the host implements that.
40    pub params: BTreeMap<String, ParamValue>,
41    /// Arbitrary fields that should be persisted together with the plugin's parameters. Any field
42    /// on the [`Params`][crate::params::Params] struct that's annotated with `#[persist =
43    /// "stable_name"]` will be persisted this way.
44    ///
45    /// The individual fields are also serialized as JSON so they can safely be restored
46    /// independently of the other fields.
47    pub fields: BTreeMap<String, String>,
48}