Skip to content

Instantly share code, notes, and snippets.

@zhe-t
Created August 30, 2024 19:48
Show Gist options
  • Save zhe-t/0d961e0cfac0360877888cebc84882b2 to your computer and use it in GitHub Desktop.
Save zhe-t/0d961e0cfac0360877888cebc84882b2 to your computer and use it in GitHub Desktop.
/**
* Function that converts a Rust enum to a string.
* Example rust: { switchboard: {} } -> ts: Switchboard
* @param value Rust enum value
*/
public static convertRustEnumValueToString(value: any) {
// get the key of the enum
let key = Object.keys(value)[0];
// capitalize first letter
key = key.charAt(0).toUpperCase() + key.slice(1);
return key;
}
/**
* Function that converts a Rust enum to a TS enum value.
* Example rust: { switchboard: {} } -> ts: OracleType.Switchboard
* @param value Rust enum value
* @param enumType TS enum type
*/
public static convertRustEnumValueToTSEnumValue(value: any, enumType: any) {
let key = convertRustEnumValueToString(value);
return enumType[key];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment