Save the rust version of settings (#5563)
* ensure we save the correct value for the settings versus relying on tomlStringify Signed-off-by: Jess Frazelle <github@jessfraz.com> * more exact functions Signed-off-by: Jess Frazelle <github@jessfraz.com> * fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
@ -12,7 +12,6 @@ import {
|
||||
get_kcl_version,
|
||||
make_default_planes,
|
||||
coredump,
|
||||
toml_stringify,
|
||||
default_app_settings,
|
||||
parse_app_settings,
|
||||
parse_project_settings,
|
||||
@ -21,6 +20,8 @@ import {
|
||||
clear_scene_and_bust_cache,
|
||||
kcl_settings,
|
||||
change_kcl_settings,
|
||||
serialize_project_configuration,
|
||||
serialize_configuration,
|
||||
reloadModule,
|
||||
} from 'lib/wasm_lib_wrapper'
|
||||
|
||||
@ -636,10 +637,6 @@ export async function coreDump(
|
||||
}
|
||||
}
|
||||
|
||||
export function tomlStringify(toml: any): string | Error {
|
||||
return toml_stringify(JSON.stringify(toml))
|
||||
}
|
||||
|
||||
export function defaultAppSettings(): DeepPartial<Configuration> | Error {
|
||||
return default_app_settings()
|
||||
}
|
||||
@ -786,3 +783,27 @@ export function unitAngToUnitAngle(input: UnitAng): UnitAngle {
|
||||
export function getKclVersion(): string {
|
||||
return get_kcl_version()
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a project configuration to a TOML string.
|
||||
*/
|
||||
export function serializeConfiguration(configuration: any): string | Error {
|
||||
try {
|
||||
return serialize_configuration(configuration)
|
||||
} catch (e: any) {
|
||||
return new Error(`Error serializing configuration: ${e}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a project configuration to a TOML string.
|
||||
*/
|
||||
export function serializeProjectConfiguration(
|
||||
configuration: any
|
||||
): string | Error {
|
||||
try {
|
||||
return serialize_project_configuration(configuration)
|
||||
} catch (e: any) {
|
||||
return new Error(`Error serializing project configuration: ${e}`)
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ import {
|
||||
initPromise,
|
||||
parseAppSettings,
|
||||
parseProjectSettings,
|
||||
tomlStringify,
|
||||
serializeConfiguration,
|
||||
serializeProjectConfiguration,
|
||||
} from 'lang/wasm'
|
||||
import { mouseControlsToCameraSystem } from 'lib/cameraControls'
|
||||
import { BROWSER_PROJECT_NAME } from 'lib/constants'
|
||||
@ -131,7 +132,7 @@ export function readLocalStorageAppSettingsFile():
|
||||
} catch (e) {
|
||||
const settings = defaultAppSettings()
|
||||
if (err(settings)) return settings
|
||||
const tomlStr = tomlStringify(settings)
|
||||
const tomlStr = serializeConfiguration(settings)
|
||||
if (err(tomlStr)) return tomlStr
|
||||
|
||||
localStorage.setItem(localStorageAppSettingsPath(), tomlStr)
|
||||
@ -152,7 +153,7 @@ function readLocalStorageProjectSettingsFile():
|
||||
const projectSettings = parseProjectSettings(stored)
|
||||
if (err(projectSettings)) {
|
||||
const settings = defaultProjectSettings()
|
||||
const tomlStr = tomlStringify(settings)
|
||||
const tomlStr = serializeProjectConfiguration(settings)
|
||||
if (err(tomlStr)) return tomlStr
|
||||
|
||||
localStorage.setItem(localStorageProjectSettingsPath(), tomlStr)
|
||||
@ -229,7 +230,7 @@ export async function saveSettings(
|
||||
|
||||
// Get the user settings.
|
||||
const jsAppSettings = getChangedSettingsAtLevel(allSettings, 'user')
|
||||
const appTomlString = tomlStringify({ settings: jsAppSettings })
|
||||
const appTomlString = serializeConfiguration({ settings: jsAppSettings })
|
||||
if (err(appTomlString)) return
|
||||
|
||||
// Write the app settings.
|
||||
@ -246,7 +247,9 @@ export async function saveSettings(
|
||||
|
||||
// Get the project settings.
|
||||
const jsProjectSettings = getChangedSettingsAtLevel(allSettings, 'project')
|
||||
const projectTomlString = tomlStringify({ settings: jsProjectSettings })
|
||||
const projectTomlString = serializeProjectConfiguration({
|
||||
settings: jsProjectSettings,
|
||||
})
|
||||
if (err(projectTomlString)) return
|
||||
|
||||
// Write the project settings.
|
||||
|
@ -19,7 +19,6 @@ import {
|
||||
get_tangential_arc_to_info as GetTangentialArcToInfo,
|
||||
make_default_planes as MakeDefaultPlanes,
|
||||
coredump as CoreDump,
|
||||
toml_stringify as TomlStringify,
|
||||
default_app_settings as DefaultAppSettings,
|
||||
parse_app_settings as ParseAppSettings,
|
||||
parse_project_settings as ParseProjectSettings,
|
||||
@ -29,6 +28,8 @@ import {
|
||||
kcl_settings as KclSettings,
|
||||
change_kcl_settings as ChangeKclSettings,
|
||||
get_kcl_version as GetKclVersion,
|
||||
serialize_configuration as SerializeConfiguration,
|
||||
serialize_project_configuration as SerializeProjectConfiguration,
|
||||
} from '../wasm-lib/pkg/wasm_lib'
|
||||
|
||||
type ModuleType = typeof import('../wasm-lib/pkg/wasm_lib')
|
||||
@ -86,9 +87,6 @@ export const make_default_planes: typeof MakeDefaultPlanes = (...args) => {
|
||||
export const coredump: typeof CoreDump = (...args) => {
|
||||
return getModule().coredump(...args)
|
||||
}
|
||||
export const toml_stringify: typeof TomlStringify = (...args) => {
|
||||
return getModule().toml_stringify(...args)
|
||||
}
|
||||
export const default_app_settings: typeof DefaultAppSettings = (...args) => {
|
||||
return getModule().default_app_settings(...args)
|
||||
}
|
||||
@ -122,3 +120,12 @@ export const change_kcl_settings: typeof ChangeKclSettings = (...args) => {
|
||||
export const get_kcl_version: typeof GetKclVersion = () => {
|
||||
return getModule().get_kcl_version()
|
||||
}
|
||||
export const serialize_configuration: typeof SerializeConfiguration = (
|
||||
...args
|
||||
) => {
|
||||
return getModule().serialize_configuration(...args)
|
||||
}
|
||||
export const serialize_project_configuration: typeof SerializeProjectConfiguration =
|
||||
(...args) => {
|
||||
return getModule().serialize_project_configuration(...args)
|
||||
}
|
||||
|
@ -1,11 +1,7 @@
|
||||
//! Wasm bindings for `kcl`.
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
mod toml;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
mod wasm;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub use toml::*;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub use wasm::*;
|
||||
|
@ -1,13 +0,0 @@
|
||||
//! Functions for interacting with TOML files.
|
||||
//! We do this in rust because the Javascript TOML libraries are actual trash.
|
||||
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn toml_stringify(json: &str) -> Result<String, String> {
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
let value: serde_json::Value = serde_json::from_str(json).map_err(|e| e.to_string())?;
|
||||
|
||||
toml::to_string_pretty(&value).map_err(|e| e.to_string())
|
||||
}
|
@ -483,9 +483,9 @@ pub fn parse_project_settings(toml_str: &str) -> Result<JsValue, String> {
|
||||
JsValue::from_serde(&settings).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Serialize the project settings.
|
||||
/// Serialize the configuration settings.
|
||||
#[wasm_bindgen]
|
||||
pub fn serialize_project_settings(val: JsValue) -> Result<JsValue, String> {
|
||||
pub fn serialize_configuration(val: JsValue) -> Result<JsValue, String> {
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
let config: kcl_lib::Configuration = val.into_serde().map_err(|e| e.to_string())?;
|
||||
@ -497,6 +497,20 @@ pub fn serialize_project_settings(val: JsValue) -> Result<JsValue, String> {
|
||||
Ok(JsValue::from_str(&toml_str))
|
||||
}
|
||||
|
||||
/// Serialize the project configuration settings.
|
||||
#[wasm_bindgen]
|
||||
pub fn serialize_project_configuration(val: JsValue) -> Result<JsValue, String> {
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
let config: kcl_lib::ProjectConfiguration = val.into_serde().map_err(|e| e.to_string())?;
|
||||
|
||||
let toml_str = toml::to_string_pretty(&config).map_err(|e| e.to_string())?;
|
||||
|
||||
// The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the
|
||||
// gloo-serialize crate instead.
|
||||
Ok(JsValue::from_str(&toml_str))
|
||||
}
|
||||
|
||||
static ALLOWED_DECODING_FORMATS: &[data_encoding::Encoding] = &[
|
||||
data_encoding::BASE64,
|
||||
data_encoding::BASE64URL,
|
||||
|
Reference in New Issue
Block a user