Skip to content

Commit

Permalink
Re-generate electron&web bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Jun 18, 2024
1 parent 4a7b3db commit 744e5f1
Show file tree
Hide file tree
Showing 4 changed files with 458 additions and 0 deletions.
54 changes: 54 additions & 0 deletions bindings/electron/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ export interface ClientEventTooMuchDriftWithServerClock {
export interface ClientEventWorkspaceLocallyCreated {
tag: "WorkspaceLocallyCreated"
}
export interface ClientEventWorkspaceWatchedEntryChanged {
tag: "WorkspaceWatchedEntryChanged"
realm_id: string
entry_id: string
}
export interface ClientEventWorkspacesSelfAccessChanged {
tag: "WorkspacesSelfAccessChanged"
}
Expand All @@ -531,6 +536,7 @@ export type ClientEvent =
| ClientEventServerConfigChanged
| ClientEventTooMuchDriftWithServerClock
| ClientEventWorkspaceLocallyCreated
| ClientEventWorkspaceWatchedEntryChanged
| ClientEventWorkspacesSelfAccessChanged


Expand Down Expand Up @@ -1890,6 +1896,50 @@ export type WorkspaceStorageCacheSize =
| WorkspaceStorageCacheSizeDefault


// WorkspaceWatchError
export interface WorkspaceWatchErrorEntryNotFound {
tag: "EntryNotFound"
error: string
}
export interface WorkspaceWatchErrorInternal {
tag: "Internal"
error: string
}
export interface WorkspaceWatchErrorInvalidCertificate {
tag: "InvalidCertificate"
error: string
}
export interface WorkspaceWatchErrorInvalidKeysBundle {
tag: "InvalidKeysBundle"
error: string
}
export interface WorkspaceWatchErrorInvalidManifest {
tag: "InvalidManifest"
error: string
}
export interface WorkspaceWatchErrorNoRealmAccess {
tag: "NoRealmAccess"
error: string
}
export interface WorkspaceWatchErrorOffline {
tag: "Offline"
error: string
}
export interface WorkspaceWatchErrorStopped {
tag: "Stopped"
error: string
}
export type WorkspaceWatchError =
| WorkspaceWatchErrorEntryNotFound
| WorkspaceWatchErrorInternal
| WorkspaceWatchErrorInvalidCertificate
| WorkspaceWatchErrorInvalidKeysBundle
| WorkspaceWatchErrorInvalidManifest
| WorkspaceWatchErrorNoRealmAccess
| WorkspaceWatchErrorOffline
| WorkspaceWatchErrorStopped


export function bootstrapOrganization(
config: ClientConfig,
on_event_callback: (event: ClientEvent) => void,
Expand Down Expand Up @@ -2267,3 +2317,7 @@ export function workspaceStatFolderChildrenById(
export function workspaceStop(
workspace: number
): Promise<Result<null, WorkspaceStopError>>
export function workspaceWatchEntryOneshot(
workspace: number,
path: string
): Promise<Result<string, WorkspaceWatchError>>
176 changes: 176 additions & 0 deletions bindings/electron/src/meths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,33 @@ fn variant_client_event_js_to_rs<'a>(
"ClientEventWorkspaceLocallyCreated" => {
Ok(libparsec::ClientEvent::WorkspaceLocallyCreated {})
}
"ClientEventWorkspaceWatchedEntryChanged" => {
let realm_id = {
let js_val: Handle<JsString> = obj.get(cx, "realmId")?;
{
let custom_from_rs_string = |s: String| -> Result<libparsec::VlobID, _> {
libparsec::VlobID::from_hex(s.as_str()).map_err(|e| e.to_string())
};
match custom_from_rs_string(js_val.value(cx)) {
Ok(val) => val,
Err(err) => return cx.throw_type_error(err),
}
}
};
let entry_id = {
let js_val: Handle<JsString> = obj.get(cx, "entryId")?;
{
let custom_from_rs_string = |s: String| -> Result<libparsec::VlobID, _> {
libparsec::VlobID::from_hex(s.as_str()).map_err(|e| e.to_string())
};
match custom_from_rs_string(js_val.value(cx)) {
Ok(val) => val,
Err(err) => return cx.throw_type_error(err),
}
}
};
Ok(libparsec::ClientEvent::WorkspaceWatchedEntryChanged { realm_id, entry_id })
}
"ClientEventWorkspacesSelfAccessChanged" => {
Ok(libparsec::ClientEvent::WorkspacesSelfAccessChanged {})
}
Expand Down Expand Up @@ -2845,6 +2872,33 @@ fn variant_client_event_rs_to_js<'a>(
JsString::try_new(cx, "ClientEventWorkspaceLocallyCreated").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::ClientEvent::WorkspaceWatchedEntryChanged {
realm_id, entry_id, ..
} => {
let js_tag =
JsString::try_new(cx, "ClientEventWorkspaceWatchedEntryChanged").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
let js_realm_id = JsString::try_new(cx, {
let custom_to_rs_string =
|x: libparsec::VlobID| -> Result<String, &'static str> { Ok(x.hex()) };
match custom_to_rs_string(realm_id) {
Ok(ok) => ok,
Err(err) => return cx.throw_type_error(err),
}
})
.or_throw(cx)?;
js_obj.set(cx, "realmId", js_realm_id)?;
let js_entry_id = JsString::try_new(cx, {
let custom_to_rs_string =
|x: libparsec::VlobID| -> Result<String, &'static str> { Ok(x.hex()) };
match custom_to_rs_string(entry_id) {
Ok(ok) => ok,
Err(err) => return cx.throw_type_error(err),
}
})
.or_throw(cx)?;
js_obj.set(cx, "entryId", js_entry_id)?;
}
libparsec::ClientEvent::WorkspacesSelfAccessChanged { .. } => {
let js_tag =
JsString::try_new(cx, "ClientEventWorkspacesSelfAccessChanged").or_throw(cx)?;
Expand Down Expand Up @@ -6073,6 +6127,56 @@ fn variant_workspace_storage_cache_size_rs_to_js<'a>(
Ok(js_obj)
}

// WorkspaceWatchError

#[allow(dead_code)]
fn variant_workspace_watch_error_rs_to_js<'a>(
cx: &mut impl Context<'a>,
rs_obj: libparsec::WorkspaceWatchError,
) -> NeonResult<Handle<'a, JsObject>> {
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::WorkspaceWatchError::EntryNotFound { .. } => {
let js_tag = JsString::try_new(cx, "WorkspaceWatchErrorEntryNotFound").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::WorkspaceWatchError::Internal { .. } => {
let js_tag = JsString::try_new(cx, "WorkspaceWatchErrorInternal").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::WorkspaceWatchError::InvalidCertificate { .. } => {
let js_tag =
JsString::try_new(cx, "WorkspaceWatchErrorInvalidCertificate").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::WorkspaceWatchError::InvalidKeysBundle { .. } => {
let js_tag =
JsString::try_new(cx, "WorkspaceWatchErrorInvalidKeysBundle").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::WorkspaceWatchError::InvalidManifest { .. } => {
let js_tag =
JsString::try_new(cx, "WorkspaceWatchErrorInvalidManifest").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::WorkspaceWatchError::NoRealmAccess { .. } => {
let js_tag = JsString::try_new(cx, "WorkspaceWatchErrorNoRealmAccess").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::WorkspaceWatchError::Offline { .. } => {
let js_tag = JsString::try_new(cx, "WorkspaceWatchErrorOffline").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
libparsec::WorkspaceWatchError::Stopped { .. } => {
let js_tag = JsString::try_new(cx, "WorkspaceWatchErrorStopped").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
}
}
Ok(js_obj)
}

// bootstrap_organization
fn bootstrap_organization(mut cx: FunctionContext) -> JsResult<JsPromise> {
let config = {
Expand Down Expand Up @@ -11524,6 +11628,77 @@ fn workspace_stop(mut cx: FunctionContext) -> JsResult<JsPromise> {
Ok(promise)
}

// workspace_watch_entry_oneshot
fn workspace_watch_entry_oneshot(mut cx: FunctionContext) -> JsResult<JsPromise> {
let workspace = {
let js_val = cx.argument::<JsNumber>(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 path = {
let js_val = cx.argument::<JsString>(1)?;
{
let custom_from_rs_string = |s: String| -> Result<_, String> {
s.parse::<libparsec::FsPath>().map_err(|e| e.to_string())
};
match custom_from_rs_string(js_val.value(&mut cx)) {
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::workspace_watch_entry_oneshot(workspace, path).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 = JsString::try_new(&mut cx, {
let custom_to_rs_string =
|x: libparsec::VlobID| -> Result<String, &'static str> {
Ok(x.hex())
};
match custom_to_rs_string(ok) {
Ok(ok) => ok,
Err(err) => return cx.throw_type_error(err),
}
})
.or_throw(&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_workspace_watch_error_rs_to_js(&mut cx, err)?;
js_obj.set(&mut cx, "error", js_err)?;
js_obj
}
};
Ok(js_ret)
});
});

Ok(promise)
}

pub fn register_meths(cx: &mut ModuleContext) -> NeonResult<()> {
cx.export_function("bootstrapOrganization", bootstrap_organization)?;
cx.export_function(
Expand Down Expand Up @@ -11708,5 +11883,6 @@ pub fn register_meths(cx: &mut ModuleContext) -> NeonResult<()> {
workspace_stat_folder_children_by_id,
)?;
cx.export_function("workspaceStop", workspace_stop)?;
cx.export_function("workspaceWatchEntryOneshot", workspace_watch_entry_oneshot)?;
Ok(())
}
Loading

0 comments on commit 744e5f1

Please sign in to comment.