Skip to main content

settings_model/
schema.rs

1//! A typed setting: where it lives and how far it travels.
2
3use serde::Serialize;
4use serde::de::DeserializeOwned;
5use serde_json::Value;
6
7use crate::DeviceClass;
8
9/// The section holding every app's settings that follow the person, one
10/// object per app id: an app's typed settings declare where each belongs,
11/// and only those that follow the person are written here.
12pub const APP_PERSONAL_SECTION: &str = "app_personal";
13
14pub const APP_DEVICE_LOCAL_SECTION: &str = "app_device_local";
15
16/// The largest value of the person's, as JSON text, that their devices carry. An app's entry in
17/// [`APP_PERSONAL_SECTION`] is one value, so its whole object must fit.
18pub const MAX_PERSONAL_VALUE_BYTES: usize = 64 * 1024;
19
20/// Where an app's typed settings live and how far they travel. Settings follow a person
21/// or a device, never a workspace's members; they are never visible to members.
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub enum Scope {
24    Personal,
25    /// Kept on this device and never sent anywhere: a hardware choice, a path, a window
26    /// placement.
27    DeviceLocal,
28    Shared(&'static str),
29}
30
31impl Scope {
32    /// The document section holding the settings of this scope.
33    #[must_use]
34    pub const fn section(self) -> &'static str {
35        match self {
36            Self::Personal => APP_PERSONAL_SECTION,
37            Self::DeviceLocal => APP_DEVICE_LOCAL_SECTION,
38            Self::Shared(name) => name,
39        }
40    }
41}
42
43/// A typed settings value with a declared [`Scope`].
44///
45/// Reads never fail: a missing document reads as [`Default`], and a key
46/// that is missing or does not decode reads as its default while every
47/// other key keeps its stored value. An app's types of one scope share
48/// its section, so their field names must not collide.
49///
50/// ```ignore
51/// #[derive(Clone, Default, PartialEq, Serialize, Deserialize)]
52/// struct Preferences { confirm_kill: bool }
53/// impl SettingsSchema for Preferences {
54///     const SCOPE: Scope = Scope::Personal;
55/// }
56/// let prefs = context.settings::<Preferences>().get();
57/// ```
58pub trait SettingsSchema: Serialize + DeserializeOwned + Default + 'static {
59    const SCOPE: Scope;
60
61    const APP: Option<&'static str> = None;
62
63    /// What a person who changed nothing has on a `device`: [`Default`] everywhere,
64    /// unless the setting costs what one class of device should not spend unasked (a copy
65    /// kept on the device, work in the background).
66    #[must_use]
67    fn defaults_on(device: DeviceClass) -> Self {
68        let _ = device;
69        Self::default()
70    }
71
72    /// Whether the setting means anything on a `device`: one whose effect a class of
73    /// device cannot have (a copy kept on a device that keeps none) is not offered there,
74    /// neither shown nor published.
75    #[must_use]
76    fn available_on(device: DeviceClass) -> bool {
77        let _ = device;
78        true
79    }
80
81    /// The setting `value` holds, as JSON stores it. When `value` is not one.
82    fn from_settings_value(value: Value) -> serde_json::Result<Self> {
83        serde_json::from_value(value)
84    }
85
86    /// The setting as JSON stores it; see [`Self::from_settings_value`]. When it does not
87    /// serialize.
88    fn to_settings_value(&self) -> serde_json::Result<Value> {
89        serde_json::to_value(self)
90    }
91}
92
93/// [`SettingsSchema::from_settings_value`] and [`SettingsSchema::to_settings_value`]
94/// compiled where the impl is, never in the crates that read the setting: for a large
95/// settings type whose crate is in the shared runtime or an engine, every app or pane
96/// reading it otherwise carries its own copy of serde's decoder.
97#[macro_export]
98macro_rules! settings_codec {
99    () => {
100        #[inline(never)]
101        fn from_settings_value(
102            value: $crate::__serde_json::Value,
103        ) -> $crate::__serde_json::Result<Self> {
104            $crate::__serde_json::from_value(value)
105        }
106
107        #[inline(never)]
108        fn to_settings_value(&self) -> $crate::__serde_json::Result<$crate::__serde_json::Value> {
109            $crate::__serde_json::to_value(self)
110        }
111    };
112}