This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: enforce type checks on logging and build info (#57)
- Loading branch information
1 parent
9263984
commit 325a423
Showing
9 changed files
with
119 additions
and
72 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use gloo_utils::format::JsValueSerdeExt; | ||
use serde::{Deserialize, Serialize}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen(typescript_custom_section)] | ||
const LOG_LEVEL: &'static str = r#" | ||
/** | ||
* @typedef {Object} BuildInfo - Information about how the installed package was built | ||
* @property {string} gitHash - The hash of the git commit from which the package was built. | ||
* @property {string} version - The version of the package at the built git commit. | ||
* @property {boolean} dirty - Whether the package contained uncommitted changes when built. | ||
*/ | ||
export type BuildInfo = { | ||
gitHash: string; | ||
version: string; | ||
dirty: string; | ||
} | ||
"#; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(typescript_type = "BuildInfo")] | ||
pub type JsBuildInfo; | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct BuildInfo { | ||
#[serde(rename = "gitHash")] | ||
git_hash: &'static str, | ||
version: &'static str, | ||
dirty: bool, | ||
} | ||
|
||
const BUILD_INFO: BuildInfo = BuildInfo { | ||
git_hash: env!("GIT_COMMIT"), | ||
version: env!("CARGO_PKG_VERSION"), | ||
dirty: const_str::equal!(env!("GIT_DIRTY"), "true"), | ||
}; | ||
|
||
/// Returns the `BuildInfo` object containing information about how the installed package was built. | ||
/// @returns {BuildInfo} - Information on how the installed package was built. | ||
#[wasm_bindgen(js_name = buildInfo, skip_jsdoc)] | ||
pub fn build_info() -> JsBuildInfo { | ||
console_error_panic_hook::set_once(); | ||
<JsValue as JsValueSerdeExt>::from_serde(&BUILD_INFO).unwrap().into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,20 @@ | ||
#![warn(unused_crate_dependencies, unused_extern_crates)] | ||
#![warn(unreachable_pub)] | ||
|
||
use gloo_utils::format::JsValueSerdeExt; | ||
use js_sys::Map; | ||
use log::Level; | ||
use serde::{Deserialize, Serialize}; | ||
use std::str::FromStr; | ||
use wasm_bindgen::prelude::*; | ||
|
||
mod abi; | ||
mod barretenberg; | ||
mod build_info; | ||
mod compression; | ||
mod execute; | ||
mod foreign_calls; | ||
mod js_transforms; | ||
mod js_witness_map; | ||
mod logging; | ||
mod public_witness; | ||
|
||
pub use abi::{abi_decode, abi_encode}; | ||
pub use build_info::build_info; | ||
pub use compression::{compress_witness, decompress_witness}; | ||
pub use execute::execute_circuit; | ||
pub use js_witness_map::JsWitnessMap; | ||
pub use logging::{init_log_level, LogLevel}; | ||
pub use public_witness::{get_public_parameters_witness, get_public_witness, get_return_witness}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct BuildInfo { | ||
git_hash: &'static str, | ||
version: &'static str, | ||
dirty: &'static str, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn init_log_level(level: String) { | ||
// Set the static variable from Rust | ||
use std::sync::Once; | ||
|
||
let log_level = Level::from_str(&level).unwrap_or(Level::Error); | ||
static SET_HOOK: Once = Once::new(); | ||
SET_HOOK.call_once(|| { | ||
wasm_logger::init(wasm_logger::Config::new(log_level)); | ||
}); | ||
} | ||
|
||
const BUILD_INFO: BuildInfo = BuildInfo { | ||
git_hash: env!("GIT_COMMIT"), | ||
version: env!("CARGO_PKG_VERSION"), | ||
dirty: env!("GIT_DIRTY"), | ||
}; | ||
|
||
#[wasm_bindgen(js_name = buildInfo)] | ||
pub fn build_info() -> JsValue { | ||
console_error_panic_hook::set_once(); | ||
<JsValue as JsValueSerdeExt>::from_serde(&BUILD_INFO).unwrap() | ||
} | ||
|
||
#[wasm_bindgen(typescript_custom_section)] | ||
const WITNESS_MAP: &'static str = r#" | ||
// Map from witness index to hex string value of witness. | ||
export type WitnessMap = Map<number, string>; | ||
"#; | ||
|
||
// WitnessMap | ||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(extends = Map, js_name = "WitnessMap", typescript_type = "WitnessMap")] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub type JsWitnessMap; | ||
|
||
#[wasm_bindgen(constructor, js_class = "Map")] | ||
pub fn new() -> JsWitnessMap; | ||
|
||
} | ||
|
||
impl Default for JsWitnessMap { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use js_sys::JsString; | ||
use log::Level; | ||
use std::str::FromStr; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen(typescript_custom_section)] | ||
const LOG_LEVEL: &'static str = r#" | ||
export type LogLevel = "OFF" | "ERROR" | "WARN" | "INFO" | "DEBUG" | "TRACE"; | ||
"#; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(extends = JsString, typescript_type = "LogLevel")] | ||
pub type LogLevel; | ||
} | ||
|
||
#[wasm_bindgen(js_name = initLogLevel)] | ||
pub fn init_log_level(level: LogLevel) { | ||
// Set the static variable from Rust | ||
use std::sync::Once; | ||
|
||
let log_level = level.as_string().unwrap(); | ||
let log_level = Level::from_str(&log_level).unwrap_or(Level::Error); | ||
static SET_HOOK: Once = Once::new(); | ||
SET_HOOK.call_once(|| { | ||
wasm_logger::init(wasm_logger::Config::new(log_level)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters