Skip to main content

perspective_viewer/config/
viewer_config.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::sync::LazyLock;
14
15use perspective_client::config::viewer::{ViewerConfig, ViewerConfigInitial, ViewerConfigUpdate};
16use perspective_js::utils::*;
17use serde::{Deserialize, Serialize};
18use ts_rs::TS;
19use wasm_bindgen::prelude::*;
20
21/// The `@perspective-dev/viewer` package version, stamped on every saved
22/// config.
23pub static API_VERSION: LazyLock<&'static str> = LazyLock::new(|| {
24    #[derive(Deserialize)]
25    struct Package {
26        version: &'static str,
27    }
28    let pkg: &'static str = include_str!("../../../package.json");
29    let pkg: Package = serde_json::from_str(pkg).unwrap();
30    pkg.version
31});
32
33/// The `version` a raw config, or else one of its workspace panels, was saved
34/// by.
35pub fn saved_version(config: &JsValue) -> Option<String> {
36    let version = |value: &JsValue| {
37        js_sys::Reflect::get(value, &JsValue::from_str("version"))
38            .ok()?
39            .as_string()
40    };
41
42    version(config).or_else(|| {
43        ["panels", "viewers"].into_iter().find_map(|key| {
44            let panels = js_sys::Reflect::get(config, &JsValue::from_str(key)).ok()?;
45            let panels = panels.dyn_into::<js_sys::Object>().ok()?;
46            js_sys::Object::values(&panels)
47                .iter()
48                .find_map(|panel| version(&panel))
49        })
50    })
51}
52
53/// Await `result`, naming the release a config was `saved` by in its error
54/// when that is not this release.
55pub async fn noting_saved_version<T>(
56    saved: Option<String>,
57    result: impl std::future::Future<Output = ApiResult<T>>,
58) -> ApiResult<T> {
59    result.await.map_err(|error| match saved {
60        Some(version) if version != *API_VERSION => ApiError::new(format!(
61            "{error} (this config was saved by {version}, and this is {}; configs from older \
62             releases can be upgraded with `perspective-migrate`)",
63            *API_VERSION
64        )),
65        _ => error,
66    })
67}
68
69/// Encode a config to a `JsValue`.
70pub trait EncodeConfig {
71    /// Encode this config to a `JsValue` in a supported type.
72    fn encode(&self) -> ApiResult<JsValue>;
73}
74
75impl<V: TS + Serialize, T: TS + Serialize> EncodeConfig for ViewerConfig<V, T> {
76    fn encode(&self) -> ApiResult<JsValue> {
77        Ok(JsValue::from_serde_ext(self)?)
78    }
79}
80
81/// Decode a config from a `JsValue`, rejecting unknown keys.
82pub trait DecodeConfig: Sized {
83    /// Decode `value` into this config type.
84    fn decode(value: &JsValue) -> ApiResult<Self>;
85}
86
87impl DecodeConfig for ViewerConfigUpdate {
88    fn decode(value: &JsValue) -> ApiResult<Self> {
89        Ok(value.into_serde_ext()?)
90    }
91}
92
93impl DecodeConfig for ViewerConfigInitial {
94    fn decode(value: &JsValue) -> ApiResult<Self> {
95        Ok(value.into_serde_ext()?)
96    }
97}