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,
|
get_kcl_version,
|
||||||
make_default_planes,
|
make_default_planes,
|
||||||
coredump,
|
coredump,
|
||||||
toml_stringify,
|
|
||||||
default_app_settings,
|
default_app_settings,
|
||||||
parse_app_settings,
|
parse_app_settings,
|
||||||
parse_project_settings,
|
parse_project_settings,
|
||||||
@ -21,6 +20,8 @@ import {
|
|||||||
clear_scene_and_bust_cache,
|
clear_scene_and_bust_cache,
|
||||||
kcl_settings,
|
kcl_settings,
|
||||||
change_kcl_settings,
|
change_kcl_settings,
|
||||||
|
serialize_project_configuration,
|
||||||
|
serialize_configuration,
|
||||||
reloadModule,
|
reloadModule,
|
||||||
} from 'lib/wasm_lib_wrapper'
|
} 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 {
|
export function defaultAppSettings(): DeepPartial<Configuration> | Error {
|
||||||
return default_app_settings()
|
return default_app_settings()
|
||||||
}
|
}
|
||||||
@ -786,3 +783,27 @@ export function unitAngToUnitAngle(input: UnitAng): UnitAngle {
|
|||||||
export function getKclVersion(): string {
|
export function getKclVersion(): string {
|
||||||
return get_kcl_version()
|
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,
|
initPromise,
|
||||||
parseAppSettings,
|
parseAppSettings,
|
||||||
parseProjectSettings,
|
parseProjectSettings,
|
||||||
tomlStringify,
|
serializeConfiguration,
|
||||||
|
serializeProjectConfiguration,
|
||||||
} from 'lang/wasm'
|
} from 'lang/wasm'
|
||||||
import { mouseControlsToCameraSystem } from 'lib/cameraControls'
|
import { mouseControlsToCameraSystem } from 'lib/cameraControls'
|
||||||
import { BROWSER_PROJECT_NAME } from 'lib/constants'
|
import { BROWSER_PROJECT_NAME } from 'lib/constants'
|
||||||
@ -131,7 +132,7 @@ export function readLocalStorageAppSettingsFile():
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
const settings = defaultAppSettings()
|
const settings = defaultAppSettings()
|
||||||
if (err(settings)) return settings
|
if (err(settings)) return settings
|
||||||
const tomlStr = tomlStringify(settings)
|
const tomlStr = serializeConfiguration(settings)
|
||||||
if (err(tomlStr)) return tomlStr
|
if (err(tomlStr)) return tomlStr
|
||||||
|
|
||||||
localStorage.setItem(localStorageAppSettingsPath(), tomlStr)
|
localStorage.setItem(localStorageAppSettingsPath(), tomlStr)
|
||||||
@ -152,7 +153,7 @@ function readLocalStorageProjectSettingsFile():
|
|||||||
const projectSettings = parseProjectSettings(stored)
|
const projectSettings = parseProjectSettings(stored)
|
||||||
if (err(projectSettings)) {
|
if (err(projectSettings)) {
|
||||||
const settings = defaultProjectSettings()
|
const settings = defaultProjectSettings()
|
||||||
const tomlStr = tomlStringify(settings)
|
const tomlStr = serializeProjectConfiguration(settings)
|
||||||
if (err(tomlStr)) return tomlStr
|
if (err(tomlStr)) return tomlStr
|
||||||
|
|
||||||
localStorage.setItem(localStorageProjectSettingsPath(), tomlStr)
|
localStorage.setItem(localStorageProjectSettingsPath(), tomlStr)
|
||||||
@ -229,7 +230,7 @@ export async function saveSettings(
|
|||||||
|
|
||||||
// Get the user settings.
|
// Get the user settings.
|
||||||
const jsAppSettings = getChangedSettingsAtLevel(allSettings, 'user')
|
const jsAppSettings = getChangedSettingsAtLevel(allSettings, 'user')
|
||||||
const appTomlString = tomlStringify({ settings: jsAppSettings })
|
const appTomlString = serializeConfiguration({ settings: jsAppSettings })
|
||||||
if (err(appTomlString)) return
|
if (err(appTomlString)) return
|
||||||
|
|
||||||
// Write the app settings.
|
// Write the app settings.
|
||||||
@ -246,7 +247,9 @@ export async function saveSettings(
|
|||||||
|
|
||||||
// Get the project settings.
|
// Get the project settings.
|
||||||
const jsProjectSettings = getChangedSettingsAtLevel(allSettings, 'project')
|
const jsProjectSettings = getChangedSettingsAtLevel(allSettings, 'project')
|
||||||
const projectTomlString = tomlStringify({ settings: jsProjectSettings })
|
const projectTomlString = serializeProjectConfiguration({
|
||||||
|
settings: jsProjectSettings,
|
||||||
|
})
|
||||||
if (err(projectTomlString)) return
|
if (err(projectTomlString)) return
|
||||||
|
|
||||||
// Write the project settings.
|
// Write the project settings.
|
||||||
|
@ -19,7 +19,6 @@ import {
|
|||||||
get_tangential_arc_to_info as GetTangentialArcToInfo,
|
get_tangential_arc_to_info as GetTangentialArcToInfo,
|
||||||
make_default_planes as MakeDefaultPlanes,
|
make_default_planes as MakeDefaultPlanes,
|
||||||
coredump as CoreDump,
|
coredump as CoreDump,
|
||||||
toml_stringify as TomlStringify,
|
|
||||||
default_app_settings as DefaultAppSettings,
|
default_app_settings as DefaultAppSettings,
|
||||||
parse_app_settings as ParseAppSettings,
|
parse_app_settings as ParseAppSettings,
|
||||||
parse_project_settings as ParseProjectSettings,
|
parse_project_settings as ParseProjectSettings,
|
||||||
@ -29,6 +28,8 @@ import {
|
|||||||
kcl_settings as KclSettings,
|
kcl_settings as KclSettings,
|
||||||
change_kcl_settings as ChangeKclSettings,
|
change_kcl_settings as ChangeKclSettings,
|
||||||
get_kcl_version as GetKclVersion,
|
get_kcl_version as GetKclVersion,
|
||||||
|
serialize_configuration as SerializeConfiguration,
|
||||||
|
serialize_project_configuration as SerializeProjectConfiguration,
|
||||||
} from '../wasm-lib/pkg/wasm_lib'
|
} from '../wasm-lib/pkg/wasm_lib'
|
||||||
|
|
||||||
type ModuleType = typeof import('../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) => {
|
export const coredump: typeof CoreDump = (...args) => {
|
||||||
return getModule().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) => {
|
export const default_app_settings: typeof DefaultAppSettings = (...args) => {
|
||||||
return getModule().default_app_settings(...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 = () => {
|
export const get_kcl_version: typeof GetKclVersion = () => {
|
||||||
return getModule().get_kcl_version()
|
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`.
|
//! Wasm bindings for `kcl`.
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
mod toml;
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
mod wasm;
|
mod wasm;
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
pub use toml::*;
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
pub use wasm::*;
|
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())
|
JsValue::from_serde(&settings).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialize the project settings.
|
/// Serialize the configuration settings.
|
||||||
#[wasm_bindgen]
|
#[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();
|
console_error_panic_hook::set_once();
|
||||||
|
|
||||||
let config: kcl_lib::Configuration = val.into_serde().map_err(|e| e.to_string())?;
|
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))
|
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] = &[
|
static ALLOWED_DECODING_FORMATS: &[data_encoding::Encoding] = &[
|
||||||
data_encoding::BASE64,
|
data_encoding::BASE64,
|
||||||
data_encoding::BASE64URL,
|
data_encoding::BASE64URL,
|
||||||
|
Reference in New Issue
Block a user