use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::DeviceClass;
pub const APP_PERSONAL_SECTION: &str = "app_personal";
pub const APP_DEVICE_LOCAL_SECTION: &str = "app_device_local";
pub const MAX_PERSONAL_VALUE_BYTES: usize = 64 * 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Scope {
Personal,
DeviceLocal,
Shared(&'static str),
}
impl Scope {
#[must_use]
pub const fn section(self) -> &'static str {
match self {
Self::Personal => APP_PERSONAL_SECTION,
Self::DeviceLocal => APP_DEVICE_LOCAL_SECTION,
Self::Shared(name) => name,
}
}
}
pub trait SettingsSchema: Serialize + DeserializeOwned + Default + 'static {
const SCOPE: Scope;
const APP: Option<&'static str> = None;
#[must_use]
fn defaults_on(device: DeviceClass) -> Self {
let _ = device;
Self::default()
}
#[must_use]
fn available_on(device: DeviceClass) -> bool {
let _ = device;
true
}
fn from_settings_value(value: Value) -> serde_json::Result<Self> {
serde_json::from_value(value)
}
fn to_settings_value(&self) -> serde_json::Result<Value> {
serde_json::to_value(self)
}
}
#[macro_export]
macro_rules! settings_codec {
() => {
#[inline(never)]
fn from_settings_value(
value: $crate::__serde_json::Value,
) -> $crate::__serde_json::Result<Self> {
$crate::__serde_json::from_value(value)
}
#[inline(never)]
fn to_settings_value(&self) -> $crate::__serde_json::Result<$crate::__serde_json::Value> {
$crate::__serde_json::to_value(self)
}
};
}