From b57bae06fab93c55561ae56cde162890e38c2ceb Mon Sep 17 00:00:00 2001 From: Emmanuel Leblond Date: Sat, 21 Sep 2024 13:03:36 +0200 Subject: [PATCH] Re-generate electron&web bindings --- bindings/electron/src/index.d.ts | 56 +++++ bindings/electron/src/meths.rs | 227 ++++++++++++++++++++ bindings/web/src/meths.rs | 172 +++++++++++++++ client/src/plugins/libparsec/definitions.ts | 66 ++++++ 4 files changed, 521 insertions(+) diff --git a/bindings/electron/src/index.d.ts b/bindings/electron/src/index.d.ts index a217ea503a3..9c4f005526b 100644 --- a/bindings/electron/src/index.d.ts +++ b/bindings/electron/src/index.d.ts @@ -162,6 +162,12 @@ export interface DeviceInfo { } +export interface Eula { + url: string + updatedOn: number +} + + export interface HumanHandle { email: string label: string @@ -445,6 +451,30 @@ export type ClaimerRetrieveInfoError = | ClaimerRetrieveInfoErrorOffline +// ClientAcceptEulaError +export interface ClientAcceptEulaErrorEulaMismatch { + tag: "EulaMismatch" + error: string +} +export interface ClientAcceptEulaErrorInternal { + tag: "Internal" + error: string +} +export interface ClientAcceptEulaErrorNoEula { + tag: "NoEula" + error: string +} +export interface ClientAcceptEulaErrorOffline { + tag: "Offline" + error: string +} +export type ClientAcceptEulaError = + | ClientAcceptEulaErrorEulaMismatch + | ClientAcceptEulaErrorInternal + | ClientAcceptEulaErrorNoEula + | ClientAcceptEulaErrorOffline + + // ClientCancelInvitationError export interface ClientCancelInvitationErrorAlreadyDeleted { tag: "AlreadyDeleted" @@ -602,6 +632,25 @@ export type ClientEvent = | ClientEventWorkspacesSelfListChanged +// ClientGetEulaError +export interface ClientGetEulaErrorInternal { + tag: "Internal" + error: string +} +export interface ClientGetEulaErrorNoEula { + tag: "NoEula" + error: string +} +export interface ClientGetEulaErrorOffline { + tag: "Offline" + error: string +} +export type ClientGetEulaError = + | ClientGetEulaErrorInternal + | ClientGetEulaErrorNoEula + | ClientGetEulaErrorOffline + + // ClientGetUserDeviceError export interface ClientGetUserDeviceErrorInternal { tag: "Internal" @@ -2104,6 +2153,10 @@ export function claimerUserInitialDoWaitPeer( canceller: number, handle: number ): Promise> +export function clientAcceptEula( + client: number, + eula_updated_on: number +): Promise> export function clientCancelInvitation( client: number, token: string @@ -2117,6 +2170,9 @@ export function clientCreateWorkspace( client: number, name: string ): Promise> +export function clientGetEula( + client: number +): Promise> export function clientGetUserDevice( client: number, device: string diff --git a/bindings/electron/src/meths.rs b/bindings/electron/src/meths.rs index 563d180322e..003297aec42 100644 --- a/bindings/electron/src/meths.rs +++ b/bindings/electron/src/meths.rs @@ -1273,6 +1273,55 @@ fn struct_device_info_rs_to_js<'a>( Ok(js_obj) } +// Eula + +#[allow(dead_code)] +fn struct_eula_js_to_rs<'a>( + cx: &mut impl Context<'a>, + obj: Handle<'a, JsObject>, +) -> NeonResult { + let url = { + let js_val: Handle = obj.get(cx, "url")?; + js_val.value(cx) + }; + let updated_on = { + let js_val: Handle = obj.get(cx, "updatedOn")?; + { + let v = js_val.value(cx); + let custom_from_rs_f64 = |n: f64| -> Result<_, &'static str> { + libparsec::DateTime::from_timestamp_micros((n * 1_000_000f64) as i64) + .map_err(|_| "Out-of-bound datetime") + }; + match custom_from_rs_f64(v) { + Ok(val) => val, + Err(err) => return cx.throw_type_error(err), + } + } + }; + Ok(libparsec::Eula { url, updated_on }) +} + +#[allow(dead_code)] +fn struct_eula_rs_to_js<'a>( + cx: &mut impl Context<'a>, + rs_obj: libparsec::Eula, +) -> NeonResult> { + let js_obj = cx.empty_object(); + let js_url = JsString::try_new(cx, rs_obj.url).or_throw(cx)?; + js_obj.set(cx, "url", js_url)?; + let js_updated_on = JsNumber::new(cx, { + let custom_to_rs_f64 = |dt: libparsec::DateTime| -> Result { + Ok((dt.as_timestamp_micros() as f64) / 1_000_000f64) + }; + match custom_to_rs_f64(rs_obj.updated_on) { + Ok(ok) => ok, + Err(err) => return cx.throw_type_error(err), + } + }); + js_obj.set(cx, "updatedOn", js_updated_on)?; + Ok(js_obj) +} + // HumanHandle #[allow(dead_code)] @@ -2735,6 +2784,37 @@ fn variant_claimer_retrieve_info_error_rs_to_js<'a>( Ok(js_obj) } +// ClientAcceptEulaError + +#[allow(dead_code)] +fn variant_client_accept_eula_error_rs_to_js<'a>( + cx: &mut impl Context<'a>, + rs_obj: libparsec::ClientAcceptEulaError, +) -> NeonResult> { + let js_obj = cx.empty_object(); + let js_display = JsString::try_new(cx, &rs_obj.to_string()).or_throw(cx)?; + js_obj.set(cx, "error", js_display)?; + match rs_obj { + libparsec::ClientAcceptEulaError::EulaMismatch { .. } => { + let js_tag = JsString::try_new(cx, "ClientAcceptEulaErrorEulaMismatch").or_throw(cx)?; + js_obj.set(cx, "tag", js_tag)?; + } + libparsec::ClientAcceptEulaError::Internal { .. } => { + let js_tag = JsString::try_new(cx, "ClientAcceptEulaErrorInternal").or_throw(cx)?; + js_obj.set(cx, "tag", js_tag)?; + } + libparsec::ClientAcceptEulaError::NoEula { .. } => { + let js_tag = JsString::try_new(cx, "ClientAcceptEulaErrorNoEula").or_throw(cx)?; + js_obj.set(cx, "tag", js_tag)?; + } + libparsec::ClientAcceptEulaError::Offline { .. } => { + let js_tag = JsString::try_new(cx, "ClientAcceptEulaErrorOffline").or_throw(cx)?; + js_obj.set(cx, "tag", js_tag)?; + } + } + Ok(js_obj) +} + // ClientCancelInvitationError #[allow(dead_code)] @@ -3422,6 +3502,33 @@ fn variant_client_event_rs_to_js<'a>( Ok(js_obj) } +// ClientGetEulaError + +#[allow(dead_code)] +fn variant_client_get_eula_error_rs_to_js<'a>( + cx: &mut impl Context<'a>, + rs_obj: libparsec::ClientGetEulaError, +) -> NeonResult> { + let js_obj = cx.empty_object(); + let js_display = JsString::try_new(cx, &rs_obj.to_string()).or_throw(cx)?; + js_obj.set(cx, "error", js_display)?; + match rs_obj { + libparsec::ClientGetEulaError::Internal { .. } => { + let js_tag = JsString::try_new(cx, "ClientGetEulaErrorInternal").or_throw(cx)?; + js_obj.set(cx, "tag", js_tag)?; + } + libparsec::ClientGetEulaError::NoEula { .. } => { + let js_tag = JsString::try_new(cx, "ClientGetEulaErrorNoEula").or_throw(cx)?; + js_obj.set(cx, "tag", js_tag)?; + } + libparsec::ClientGetEulaError::Offline { .. } => { + let js_tag = JsString::try_new(cx, "ClientGetEulaErrorOffline").or_throw(cx)?; + js_obj.set(cx, "tag", js_tag)?; + } + } + Ok(js_obj) +} + // ClientGetUserDeviceError #[allow(dead_code)] @@ -7936,6 +8043,74 @@ fn claimer_user_initial_do_wait_peer(mut cx: FunctionContext) -> JsResult JsResult { + crate::init_sentry(); + let client = { + let js_val = cx.argument::(0)?; + { + let v = js_val.value(&mut cx); + if v < (u32::MIN as f64) || (u32::MAX as f64) < v { + cx.throw_type_error("Not an u32 number")? + } + let v = v as u32; + v + } + }; + let eula_updated_on = { + let js_val = cx.argument::(1)?; + { + let v = js_val.value(&mut cx); + let custom_from_rs_f64 = |n: f64| -> Result<_, &'static str> { + libparsec::DateTime::from_timestamp_micros((n * 1_000_000f64) as i64) + .map_err(|_| "Out-of-bound datetime") + }; + match custom_from_rs_f64(v) { + Ok(val) => val, + Err(err) => return cx.throw_type_error(err), + } + } + }; + let channel = cx.channel(); + let (deferred, promise) = cx.promise(); + + // TODO: Promises are not cancellable in Javascript by default, should we add a custom cancel method ? + let _handle = crate::TOKIO_RUNTIME + .lock() + .expect("Mutex is poisoned") + .spawn(async move { + let ret = libparsec::client_accept_eula(client, eula_updated_on).await; + + deferred.settle_with(&channel, move |mut cx| { + let js_ret = match ret { + Ok(ok) => { + let js_obj = JsObject::new(&mut cx); + let js_tag = JsBoolean::new(&mut cx, true); + js_obj.set(&mut cx, "ok", js_tag)?; + let js_value = { + #[allow(clippy::let_unit_value)] + let _ = ok; + JsNull::new(&mut cx) + }; + js_obj.set(&mut cx, "value", js_value)?; + js_obj + } + Err(err) => { + let js_obj = cx.empty_object(); + let js_tag = JsBoolean::new(&mut cx, false); + js_obj.set(&mut cx, "ok", js_tag)?; + let js_err = variant_client_accept_eula_error_rs_to_js(&mut cx, err)?; + js_obj.set(&mut cx, "error", js_err)?; + js_obj + } + }; + Ok(js_ret) + }); + }); + + Ok(promise) +} + // client_cancel_invitation fn client_cancel_invitation(mut cx: FunctionContext) -> JsResult { crate::init_sentry(); @@ -8132,6 +8307,56 @@ fn client_create_workspace(mut cx: FunctionContext) -> JsResult { Ok(promise) } +// client_get_eula +fn client_get_eula(mut cx: FunctionContext) -> JsResult { + crate::init_sentry(); + let client = { + let js_val = cx.argument::(0)?; + { + let v = js_val.value(&mut cx); + if v < (u32::MIN as f64) || (u32::MAX as f64) < v { + cx.throw_type_error("Not an u32 number")? + } + let v = v as u32; + v + } + }; + let channel = cx.channel(); + let (deferred, promise) = cx.promise(); + + // TODO: Promises are not cancellable in Javascript by default, should we add a custom cancel method ? + let _handle = crate::TOKIO_RUNTIME + .lock() + .expect("Mutex is poisoned") + .spawn(async move { + let ret = libparsec::client_get_eula(client).await; + + deferred.settle_with(&channel, move |mut cx| { + let js_ret = match ret { + Ok(ok) => { + let js_obj = JsObject::new(&mut cx); + let js_tag = JsBoolean::new(&mut cx, true); + js_obj.set(&mut cx, "ok", js_tag)?; + let js_value = struct_eula_rs_to_js(&mut cx, ok)?; + js_obj.set(&mut cx, "value", js_value)?; + js_obj + } + Err(err) => { + let js_obj = cx.empty_object(); + let js_tag = JsBoolean::new(&mut cx, false); + js_obj.set(&mut cx, "ok", js_tag)?; + let js_err = variant_client_get_eula_error_rs_to_js(&mut cx, err)?; + js_obj.set(&mut cx, "error", js_err)?; + js_obj + } + }; + Ok(js_ret) + }); + }); + + Ok(promise) +} + // client_get_user_device fn client_get_user_device(mut cx: FunctionContext) -> JsResult { crate::init_sentry(); @@ -12814,9 +13039,11 @@ pub fn register_meths(cx: &mut ModuleContext) -> NeonResult<()> { "claimerUserInitialDoWaitPeer", claimer_user_initial_do_wait_peer, )?; + cx.export_function("clientAcceptEula", client_accept_eula)?; cx.export_function("clientCancelInvitation", client_cancel_invitation)?; cx.export_function("clientChangeAuthentication", client_change_authentication)?; cx.export_function("clientCreateWorkspace", client_create_workspace)?; + cx.export_function("clientGetEula", client_get_eula)?; cx.export_function("clientGetUserDevice", client_get_user_device)?; cx.export_function("clientInfo", client_info)?; cx.export_function("clientListInvitations", client_list_invitations)?; diff --git a/bindings/web/src/meths.rs b/bindings/web/src/meths.rs index bb527527466..029ffa7c260 100644 --- a/bindings/web/src/meths.rs +++ b/bindings/web/src/meths.rs @@ -1364,6 +1364,52 @@ fn struct_device_info_rs_to_js(rs_obj: libparsec::DeviceInfo) -> Result Result { + let url = { + let js_val = Reflect::get(&obj, &"url".into())?; + js_val + .dyn_into::() + .ok() + .and_then(|s| s.as_string()) + .ok_or_else(|| TypeError::new("Not a string"))? + }; + let updated_on = { + let js_val = Reflect::get(&obj, &"updatedOn".into())?; + { + let v = js_val.dyn_into::()?.value_of(); + let custom_from_rs_f64 = |n: f64| -> Result<_, &'static str> { + libparsec::DateTime::from_timestamp_micros((n * 1_000_000f64) as i64) + .map_err(|_| "Out-of-bound datetime") + }; + let v = custom_from_rs_f64(v).map_err(|e| TypeError::new(e.as_ref()))?; + v + } + }; + Ok(libparsec::Eula { url, updated_on }) +} + +#[allow(dead_code)] +fn struct_eula_rs_to_js(rs_obj: libparsec::Eula) -> Result { + let js_obj = Object::new().into(); + let js_url = rs_obj.url.into(); + Reflect::set(&js_obj, &"url".into(), &js_url)?; + let js_updated_on = { + let custom_to_rs_f64 = |dt: libparsec::DateTime| -> Result { + Ok((dt.as_timestamp_micros() as f64) / 1_000_000f64) + }; + let v = match custom_to_rs_f64(rs_obj.updated_on) { + Ok(ok) => ok, + Err(err) => return Err(JsValue::from(TypeError::new(err.as_ref()))), + }; + JsValue::from(v) + }; + Reflect::set(&js_obj, &"updatedOn".into(), &js_updated_on)?; + Ok(js_obj) +} + // HumanHandle #[allow(dead_code)] @@ -2960,6 +3006,48 @@ fn variant_claimer_retrieve_info_error_rs_to_js( Ok(js_obj) } +// ClientAcceptEulaError + +#[allow(dead_code)] +fn variant_client_accept_eula_error_rs_to_js( + rs_obj: libparsec::ClientAcceptEulaError, +) -> Result { + let js_obj = Object::new().into(); + let js_display = &rs_obj.to_string(); + Reflect::set(&js_obj, &"error".into(), &js_display.into())?; + match rs_obj { + libparsec::ClientAcceptEulaError::EulaMismatch { .. } => { + Reflect::set( + &js_obj, + &"tag".into(), + &"ClientAcceptEulaErrorEulaMismatch".into(), + )?; + } + libparsec::ClientAcceptEulaError::Internal { .. } => { + Reflect::set( + &js_obj, + &"tag".into(), + &"ClientAcceptEulaErrorInternal".into(), + )?; + } + libparsec::ClientAcceptEulaError::NoEula { .. } => { + Reflect::set( + &js_obj, + &"tag".into(), + &"ClientAcceptEulaErrorNoEula".into(), + )?; + } + libparsec::ClientAcceptEulaError::Offline { .. } => { + Reflect::set( + &js_obj, + &"tag".into(), + &"ClientAcceptEulaErrorOffline".into(), + )?; + } + } + Ok(js_obj) +} + // ClientCancelInvitationError #[allow(dead_code)] @@ -3742,6 +3830,29 @@ fn variant_client_event_rs_to_js(rs_obj: libparsec::ClientEvent) -> Result Result { + let js_obj = Object::new().into(); + let js_display = &rs_obj.to_string(); + Reflect::set(&js_obj, &"error".into(), &js_display.into())?; + match rs_obj { + libparsec::ClientGetEulaError::Internal { .. } => { + Reflect::set(&js_obj, &"tag".into(), &"ClientGetEulaErrorInternal".into())?; + } + libparsec::ClientGetEulaError::NoEula { .. } => { + Reflect::set(&js_obj, &"tag".into(), &"ClientGetEulaErrorNoEula".into())?; + } + libparsec::ClientGetEulaError::Offline { .. } => { + Reflect::set(&js_obj, &"tag".into(), &"ClientGetEulaErrorOffline".into())?; + } + } + Ok(js_obj) +} + // ClientGetUserDeviceError #[allow(dead_code)] @@ -8301,6 +8412,42 @@ pub fn claimerUserInitialDoWaitPeer(canceller: u32, handle: u32) -> Promise { }) } +// client_accept_eula +#[allow(non_snake_case)] +#[wasm_bindgen] +pub fn clientAcceptEula(client: u32, eula_updated_on: f64) -> Promise { + future_to_promise(async move { + let eula_updated_on = { + let custom_from_rs_f64 = |n: f64| -> Result<_, &'static str> { + libparsec::DateTime::from_timestamp_micros((n * 1_000_000f64) as i64) + .map_err(|_| "Out-of-bound datetime") + }; + custom_from_rs_f64(eula_updated_on).map_err(|e| TypeError::new(e.as_ref())) + }?; + + let ret = libparsec::client_accept_eula(client, eula_updated_on).await; + Ok(match ret { + Ok(value) => { + let js_obj = Object::new().into(); + Reflect::set(&js_obj, &"ok".into(), &true.into())?; + let js_value = { + let _ = value; + JsValue::null() + }; + Reflect::set(&js_obj, &"value".into(), &js_value)?; + js_obj + } + Err(err) => { + let js_obj = Object::new().into(); + Reflect::set(&js_obj, &"ok".into(), &false.into())?; + let js_err = variant_client_accept_eula_error_rs_to_js(err)?; + Reflect::set(&js_obj, &"error".into(), &js_err)?; + js_obj + } + }) + }) +} + // client_cancel_invitation #[allow(non_snake_case)] #[wasm_bindgen] @@ -8416,6 +8563,31 @@ pub fn clientCreateWorkspace(client: u32, name: String) -> Promise { }) } +// client_get_eula +#[allow(non_snake_case)] +#[wasm_bindgen] +pub fn clientGetEula(client: u32) -> Promise { + future_to_promise(async move { + let ret = libparsec::client_get_eula(client).await; + Ok(match ret { + Ok(value) => { + let js_obj = Object::new().into(); + Reflect::set(&js_obj, &"ok".into(), &true.into())?; + let js_value = struct_eula_rs_to_js(value)?; + Reflect::set(&js_obj, &"value".into(), &js_value)?; + js_obj + } + Err(err) => { + let js_obj = Object::new().into(); + Reflect::set(&js_obj, &"ok".into(), &false.into())?; + let js_err = variant_client_get_eula_error_rs_to_js(err)?; + Reflect::set(&js_obj, &"error".into(), &js_err)?; + js_obj + } + }) + }) +} + // client_get_user_device #[allow(non_snake_case)] #[wasm_bindgen] diff --git a/client/src/plugins/libparsec/definitions.ts b/client/src/plugins/libparsec/definitions.ts index 5a8c97d1a12..f1a8863230b 100644 --- a/client/src/plugins/libparsec/definitions.ts +++ b/client/src/plugins/libparsec/definitions.ts @@ -176,6 +176,11 @@ export interface DeviceInfo { createdBy: DeviceID | null } +export interface Eula { + url: string + updatedOn: DateTime +} + export interface HumanHandle { email: string label: string @@ -481,6 +486,36 @@ export type ClaimerRetrieveInfoError = | ClaimerRetrieveInfoErrorNotFound | ClaimerRetrieveInfoErrorOffline +// ClientAcceptEulaError +export enum ClientAcceptEulaErrorTag { + EulaMismatch = 'ClientAcceptEulaErrorEulaMismatch', + Internal = 'ClientAcceptEulaErrorInternal', + NoEula = 'ClientAcceptEulaErrorNoEula', + Offline = 'ClientAcceptEulaErrorOffline', +} + +export interface ClientAcceptEulaErrorEulaMismatch { + tag: ClientAcceptEulaErrorTag.EulaMismatch + error: string +} +export interface ClientAcceptEulaErrorInternal { + tag: ClientAcceptEulaErrorTag.Internal + error: string +} +export interface ClientAcceptEulaErrorNoEula { + tag: ClientAcceptEulaErrorTag.NoEula + error: string +} +export interface ClientAcceptEulaErrorOffline { + tag: ClientAcceptEulaErrorTag.Offline + error: string +} +export type ClientAcceptEulaError = + | ClientAcceptEulaErrorEulaMismatch + | ClientAcceptEulaErrorInternal + | ClientAcceptEulaErrorNoEula + | ClientAcceptEulaErrorOffline + // ClientCancelInvitationError export enum ClientCancelInvitationErrorTag { AlreadyDeleted = 'ClientCancelInvitationErrorAlreadyDeleted', @@ -673,6 +708,30 @@ export type ClientEvent = | ClientEventWorkspaceWatchedEntryChanged | ClientEventWorkspacesSelfListChanged +// ClientGetEulaError +export enum ClientGetEulaErrorTag { + Internal = 'ClientGetEulaErrorInternal', + NoEula = 'ClientGetEulaErrorNoEula', + Offline = 'ClientGetEulaErrorOffline', +} + +export interface ClientGetEulaErrorInternal { + tag: ClientGetEulaErrorTag.Internal + error: string +} +export interface ClientGetEulaErrorNoEula { + tag: ClientGetEulaErrorTag.NoEula + error: string +} +export interface ClientGetEulaErrorOffline { + tag: ClientGetEulaErrorTag.Offline + error: string +} +export type ClientGetEulaError = + | ClientGetEulaErrorInternal + | ClientGetEulaErrorNoEula + | ClientGetEulaErrorOffline + // ClientGetUserDeviceError export enum ClientGetUserDeviceErrorTag { Internal = 'ClientGetUserDeviceErrorInternal', @@ -2508,6 +2567,10 @@ export interface LibParsecPlugin { canceller: Handle, handle: Handle ): Promise> + clientAcceptEula( + client: Handle, + eula_updated_on: DateTime + ): Promise> clientCancelInvitation( client: Handle, token: InvitationToken @@ -2521,6 +2584,9 @@ export interface LibParsecPlugin { client: Handle, name: EntryName ): Promise> + clientGetEula( + client: Handle + ): Promise> clientGetUserDevice( client: Handle, device: DeviceID