Skip to content

Commit

Permalink
ensure we save the correct value for the settings versus relying on t…
Browse files Browse the repository at this point in the history
…omlStringify

Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz committed Feb 27, 2025
1 parent 850c5c6 commit b112af6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/lang/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
clear_scene_and_bust_cache,
kcl_settings,
change_kcl_settings,
serialize_project_configuration,
serialize_configuration,
reloadModule,
} from 'lib/wasm_lib_wrapper'

Expand Down Expand Up @@ -786,3 +788,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}`)
}
}
13 changes: 8 additions & 5 deletions src/lib/settings/settingsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/lib/wasm_lib_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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')
Expand Down Expand Up @@ -122,3 +124,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)
}
18 changes: 16 additions & 2 deletions src/wasm-lib/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand All @@ -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,
Expand Down

0 comments on commit b112af6

Please sign in to comment.