chore(java): new error system

This commit is contained in:
zaaarf 2024-09-05 02:45:33 +02:00
parent 921a8ee69a
commit cd8b0c64f4
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
21 changed files with 184 additions and 130 deletions

View file

@ -2,7 +2,7 @@ package mp.code;
import mp.code.data.Cursor; import mp.code.data.Cursor;
import mp.code.data.TextChange; import mp.code.data.TextChange;
import mp.code.exceptions.CodeMPException; import mp.code.exceptions.ControllerException;
import java.util.Optional; import java.util.Optional;
@ -18,23 +18,23 @@ public class BufferController {
return get_name(this.ptr); return get_name(this.ptr);
} }
private static native String get_content(long self) throws CodeMPException; private static native String get_content(long self) throws ControllerException;
public String getContent() throws CodeMPException { public String getContent() throws ControllerException {
return get_content(this.ptr); return get_content(this.ptr);
} }
private static native TextChange try_recv(long self) throws CodeMPException; private static native TextChange try_recv(long self) throws ControllerException;
public Optional<TextChange> tryRecv() throws CodeMPException { public Optional<TextChange> tryRecv() throws ControllerException {
return Optional.ofNullable(try_recv(this.ptr)); return Optional.ofNullable(try_recv(this.ptr));
} }
private static native Cursor recv(long self) throws CodeMPException; private static native Cursor recv(long self) throws ControllerException;
public Cursor recv() throws CodeMPException { public Cursor recv() throws ControllerException {
return recv(this.ptr); return recv(this.ptr);
} }
private static native void send(long self, TextChange change) throws CodeMPException; private static native void send(long self, TextChange change) throws ControllerException;
public void send(TextChange change) throws CodeMPException { public void send(TextChange change) throws ControllerException {
send(this.ptr, change); send(this.ptr, change);
} }

View file

@ -1,7 +1,8 @@
package mp.code; package mp.code;
import cz.adamh.utils.NativeUtils; import cz.adamh.utils.NativeUtils;
import mp.code.exceptions.CodeMPException; import mp.code.exceptions.ConnectionException;
import mp.code.exceptions.ConnectionRemoteException;
import java.io.IOException; import java.io.IOException;
import java.util.Optional; import java.util.Optional;
@ -9,7 +10,7 @@ import java.util.Optional;
public class Client { public class Client {
private final long ptr; private final long ptr;
public static native Client connect(String url, String username, String password) throws CodeMPException; public static native Client connect(String url, String username, String password) throws ConnectionException;
Client(long ptr) { Client(long ptr) {
this.ptr = ptr; this.ptr = ptr;
} }
@ -19,28 +20,28 @@ public class Client {
return get_url(this.ptr); return get_url(this.ptr);
} }
private static native Workspace join_workspace(long self, String id) throws CodeMPException; private static native Workspace join_workspace(long self, String id) throws ConnectionException;
public Workspace joinWorkspace(String id) throws CodeMPException { public Workspace joinWorkspace(String id) throws ConnectionException {
return join_workspace(this.ptr, id); return join_workspace(this.ptr, id);
} }
private static native void create_workspace(long self, String id) throws CodeMPException; private static native void create_workspace(long self, String id) throws ConnectionRemoteException;
public void createWorkspace(String id) throws CodeMPException { public void createWorkspace(String id) throws ConnectionRemoteException {
return create_workspace(this.ptr, id); create_workspace(this.ptr, id);
} }
private static native void delete_workspace(long self, String id) throws CodeMPException; private static native void delete_workspace(long self, String id) throws ConnectionRemoteException;
public void deleteWorkspace(String id) throws CodeMPException { public void deleteWorkspace(String id) throws ConnectionRemoteException {
return delete_workspace(this.ptr, id); delete_workspace(this.ptr, id);
} }
private static native void invite_to_workspace(long self, String ws, String usr) throws CodeMPException; private static native void invite_to_workspace(long self, String ws, String usr) throws ConnectionRemoteException;
public void inviteToWorkspace(String ws, String usr) throws CodeMPException { public void inviteToWorkspace(String ws, String usr) throws ConnectionRemoteException {
return invite_to_workspace(this.ptr, id); invite_to_workspace(this.ptr, ws, usr);
} }
private static native String[] list_workspaces(long self, boolean owned, boolean invited) throws CodeMPException; private static native String[] list_workspaces(long self, boolean owned, boolean invited) throws ConnectionRemoteException;
public String[] listWorkspaces(boolean owned, boolean invited) throws CodeMPException { public String[] listWorkspaces(boolean owned, boolean invited) throws ConnectionRemoteException {
return list_workspaces(this.ptr, owned, invited); return list_workspaces(this.ptr, owned, invited);
} }
@ -54,9 +55,9 @@ public class Client {
return Optional.ofNullable(get_workspace(this.ptr)); return Optional.ofNullable(get_workspace(this.ptr));
} }
private static native void refresh_native(long self); private static native void refresh_native(long self) throws ConnectionRemoteException;
public void refresh() { public void refresh() throws ConnectionRemoteException {
return refresh_native(this.ptr); refresh_native(this.ptr);
} }
private static native void free(long self); private static native void free(long self);

View file

@ -1,7 +1,7 @@
package mp.code; package mp.code;
import mp.code.data.Cursor; import mp.code.data.Cursor;
import mp.code.exceptions.CodeMPException; import mp.code.exceptions.ControllerException;
import java.util.Optional; import java.util.Optional;
@ -12,18 +12,18 @@ public class CursorController {
this.ptr = ptr; this.ptr = ptr;
} }
private static native Cursor try_recv(long self) throws CodeMPException; private static native Cursor try_recv(long self) throws ControllerException;
public Optional<Cursor> tryRecv() throws CodeMPException { public Optional<Cursor> tryRecv() throws ControllerException {
return Optional.ofNullable(try_recv(this.ptr)); return Optional.ofNullable(try_recv(this.ptr));
} }
private static native Cursor recv(long self) throws CodeMPException; private static native Cursor recv(long self) throws ControllerException;
public Cursor recv() throws CodeMPException { public Cursor recv() throws ControllerException {
return recv(this.ptr); return recv(this.ptr);
} }
private static native void send(long self, Cursor cursor) throws CodeMPException; private static native void send(long self, Cursor cursor) throws ControllerException;
public void send(Cursor cursor) throws CodeMPException { public void send(Cursor cursor) throws ControllerException {
send(this.ptr, cursor); send(this.ptr, cursor);
} }

View file

@ -4,7 +4,9 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import mp.code.data.DetachResult; import mp.code.data.DetachResult;
import mp.code.exceptions.CodeMPException; import mp.code.exceptions.ConnectionException;
import mp.code.exceptions.ConnectionRemoteException;
import mp.code.exceptions.ControllerException;
public class Workspace { public class Workspace {
private final long ptr; private final long ptr;
@ -33,13 +35,13 @@ public class Workspace {
return get_file_tree(this.ptr, filter.orElse(null)); return get_file_tree(this.ptr, filter.orElse(null));
} }
private static native void create_buffer(String path) throws CodeMPException; private static native void create_buffer(String path) throws ConnectionRemoteException;
public void createBuffer(String path) throws CodeMPException { public void createBuffer(String path) throws ConnectionRemoteException {
create_buffer(path); create_buffer(path);
} }
private static native BufferController attach_to_buffer(long self, String path) throws CodeMPException; private static native BufferController attach_to_buffer(long self, String path) throws ConnectionException;
public BufferController attachToBuffer(String path) throws CodeMPException { public BufferController attachToBuffer(String path) throws ConnectionException {
return attach_to_buffer(ptr, path); return attach_to_buffer(ptr, path);
} }
@ -48,33 +50,33 @@ public class Workspace {
return detach_from_buffer(this.ptr, path); return detach_from_buffer(this.ptr, path);
} }
private static native void fetch_buffers(long self) throws CodeMPException; private static native void fetch_buffers(long self) throws ConnectionRemoteException;
public void fetchBuffers() throws CodeMPException { public void fetchBuffers() throws ConnectionRemoteException {
fetch_buffers(this.ptr); fetch_buffers(this.ptr);
} }
private static native void fetch_users(long self) throws CodeMPException; private static native void fetch_users(long self) throws ConnectionRemoteException;
public void fetchUsers() throws CodeMPException { public void fetchUsers() throws ConnectionRemoteException {
fetch_buffers(this.ptr); fetch_buffers(this.ptr);
} }
private static native UUID[] list_buffer_users(long self, String path) throws CodeMPException; private static native UUID[] list_buffer_users(long self, String path) throws ConnectionRemoteException;
public UUID[] listBufferUsers(String path) throws CodeMPException { public UUID[] listBufferUsers(String path) throws ConnectionRemoteException {
return list_buffer_users(this.ptr, path); return list_buffer_users(this.ptr, path);
} }
private static native void delete_buffer(long self, String path) throws CodeMPException; private static native void delete_buffer(long self, String path) throws ConnectionRemoteException;
public void deleteBuffer(String path) throws CodeMPException { public void deleteBuffer(String path) throws ConnectionRemoteException {
delete_buffer(this.ptr, path); delete_buffer(this.ptr, path);
} }
private static native Event event(long self) throws CodeMPException; private static native Event event(long self) throws ControllerException;
public Event event() throws CodeMPException { public Event event() throws ControllerException {
return event(this.ptr); return event(this.ptr);
} }
private static native BufferController select_buffer(long self, long timeout) throws CodeMPException; private static native BufferController select_buffer(long self, long timeout) throws ControllerException;
public Optional<BufferController> selectBuffer(long timeout) throws CodeMPException { public Optional<BufferController> selectBuffer(long timeout) throws ControllerException {
return Optional.ofNullable(select_buffer(this.ptr, timeout)); return Optional.ofNullable(select_buffer(this.ptr, timeout));
} }

View file

@ -1,7 +0,0 @@
package mp.code.exceptions;
public class ChannelException extends CodeMPException {
public ChannelException(String input) {
super(input);
}
}

View file

@ -1,10 +0,0 @@
package mp.code.exceptions;
/**
* A generic class for all our exceptions coming through the JNI from the library.
*/
public abstract class CodeMPException extends Exception {
protected CodeMPException(String msg) {
super(msg);
}
}

View file

@ -0,0 +1,10 @@
package mp.code.exceptions;
/**
* An exception that may occur when processing network requests.
*/
public abstract class ConnectionException extends Exception {
protected ConnectionException(String msg) {
super(msg);
}
}

View file

@ -0,0 +1,10 @@
package mp.code.exceptions;
/**
* An exception returned by the server as a response.
*/
public abstract class ConnectionRemoteException extends Exception {
protected ConnectionRemoteException(String msg) {
super(msg);
}
}

View file

@ -0,0 +1,10 @@
package mp.code.exceptions;
/**
* An exception that occurred from the underlying tonic layer.
*/
public abstract class ConnectionTransportException extends Exception {
protected ConnectionTransportException(String msg) {
super(msg);
}
}

View file

@ -0,0 +1,13 @@
package mp.code.exceptions;
/**
* An exception that may occur when a {@link mp.code.BufferController} or
* a {@link mp.code.CursorController} perform an illegal operation.
* It may also occur as a result of {@link mp.code.Workspace#event()} and
* {@link mp.code.Workspace#selectBuffer(long)}.
*/
public abstract class ControllerException extends Exception {
protected ControllerException(String msg) {
super(msg);
}
}

View file

@ -0,0 +1,11 @@
package mp.code.exceptions;
/**
* An exception that occurs when attempting to send an operation when
* the worker has already stopped.
*/
public class ControllerStoppedException extends ControllerException {
protected ControllerStoppedException(String msg) {
super(msg);
}
}

View file

@ -0,0 +1,11 @@
package mp.code.exceptions;
/**
* An exception that occurs when the underlying controller stopped before
* fulfilling the request, without rejecting it first.
*/
public class ControllerUnfulfilledException extends ControllerException {
protected ControllerUnfulfilledException(String msg) {
super(msg);
}
}

View file

@ -1,7 +0,0 @@
package mp.code.exceptions;
public class DeadlockedException extends CodeMPException {
public DeadlockedException(String s) {
super(s);
}
}

View file

@ -1,7 +0,0 @@
package mp.code.exceptions;
public class InvalidStateException extends CodeMPException {
public InvalidStateException(String message) {
super(message);
}
}

View file

@ -1,7 +0,0 @@
package mp.code.exceptions;
public class TransportException extends CodeMPException {
public TransportException(String message) {
super(message);
}
}

View file

@ -12,7 +12,7 @@ pub extern "system" fn Java_mp_code_BufferController_get_1name(
self_ptr: jlong, self_ptr: jlong,
) -> jstring { ) -> jstring {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) }; let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
let content = controller.name(); let content = controller.path();
env.new_string(content) env.new_string(content)
.jexcept(&mut env) .jexcept(&mut env)
.as_raw() .as_raw()

View file

@ -3,7 +3,7 @@ use crate::{client::Client, Workspace};
use super::{JExceptable, RT}; use super::{JExceptable, RT};
/// Connects to a given URL and returns a [Client] to interact with that server. /// Connect to a given URL and return a [Client] to interact with that server.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_connect<'local>( pub extern "system" fn Java_mp_code_Client_connect<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -30,7 +30,7 @@ pub extern "system" fn Java_mp_code_Client_connect<'local>(
}).jexcept(&mut env).as_raw() }).jexcept(&mut env).as_raw()
} }
/// Joins a [Workspace] and returns a pointer to it. /// Join a [Workspace] and return a pointer to it.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_join_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_join_1workspace<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -52,7 +52,7 @@ pub extern "system" fn Java_mp_code_Client_join_1workspace<'local>(
}).jexcept(&mut env).as_raw() }).jexcept(&mut env).as_raw()
} }
/// Creates a workspace on server, if allowed to /// Create a workspace on server, if allowed to.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_create_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_create_1workspace<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -69,7 +69,7 @@ pub extern "system" fn Java_mp_code_Client_create_1workspace<'local>(
.jexcept(&mut env); .jexcept(&mut env);
} }
/// Deletes a workspace on server, if allowed to /// Delete a workspace on server, if allowed to.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_delete_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_delete_1workspace<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -86,7 +86,7 @@ pub extern "system" fn Java_mp_code_Client_delete_1workspace<'local>(
.jexcept(&mut env); .jexcept(&mut env);
} }
/// Invites another user to an owned workspace /// Invite another user to an owned workspace.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_invite_1to_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_invite_1to_1workspace<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -107,7 +107,7 @@ pub extern "system" fn Java_mp_code_Client_invite_1to_1workspace<'local>(
.jexcept(&mut env); .jexcept(&mut env);
} }
/// List available workspaces /// List available workspaces.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_list_1workspaces<'local>( pub extern "system" fn Java_mp_code_Client_list_1workspaces<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -146,7 +146,7 @@ fn spawn_updater(workspace: Workspace) -> Workspace {
workspace workspace
} }
/// Leaves a [Workspace] and returns whether or not the client was in such workspace. /// Leave a [Workspace] and return whether or not the client was in such workspace.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_leave_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_leave_1workspace<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -161,7 +161,7 @@ pub extern "system" fn Java_mp_code_Client_leave_1workspace<'local>(
.jexcept(&mut env) .jexcept(&mut env)
} }
/// Gets a [Workspace] by name and returns a pointer to it. /// Get a [Workspace] by name and returns a pointer to it.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -182,7 +182,7 @@ pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>(
}).unwrap_or_default().as_raw() }).unwrap_or_default().as_raw()
} }
/// Refresh client's session token /// Refresh the client's session token.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_refresh<'local>( pub extern "system" fn Java_mp_code_Client_refresh<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -190,10 +190,11 @@ pub extern "system" fn Java_mp_code_Client_refresh<'local>(
self_ptr: jlong, self_ptr: jlong,
) { ) {
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) }; let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
RT.block_on(client.refresh()).jexcept(&mut env); RT.block_on(client.refresh())
.jexcept(&mut env);
} }
/// Sets up the tracing subscriber. /// Set up the tracing subscriber.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_setup_1tracing<'local>( pub extern "system" fn Java_mp_code_Client_setup_1tracing<'local>(
mut env: JNIEnv, mut env: JNIEnv,

View file

@ -3,7 +3,7 @@ use crate::api::Controller;
use super::{JExceptable, RT}; use super::{JExceptable, RT};
/// Tries to fetch a [crate::api::Cursor], or returns null if there's nothing. /// Try to fetch a [crate::api::Cursor], or returns null if there's nothing.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_try_1recv( pub extern "system" fn Java_mp_code_CursorController_try_1recv(
mut env: JNIEnv, mut env: JNIEnv,
@ -15,7 +15,7 @@ pub extern "system" fn Java_mp_code_CursorController_try_1recv(
jni_recv(&mut env, cursor) jni_recv(&mut env, cursor)
} }
/// Blocks until it receives a [crate::api::Cursor]. /// Block until it receives a [crate::api::Cursor].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_recv( pub extern "system" fn Java_mp_code_CursorController_recv(
mut env: JNIEnv, mut env: JNIEnv,
@ -56,7 +56,7 @@ fn jni_recv(env: &mut JNIEnv, cursor: Option<crate::api::Cursor>) -> jobject {
}.as_raw() }.as_raw()
} }
/// Receives from Java, converts and sends a [crate::api::Cursor]. /// Receive from Java, converts and sends a [crate::api::Cursor].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_send<'local>( pub extern "system" fn Java_mp_code_CursorController_send<'local>(
mut env: JNIEnv, mut env: JNIEnv,

View file

@ -16,7 +16,7 @@ lazy_static::lazy_static! {
pub(crate) static ref RT: tokio::runtime::Runtime = tokio::runtime::Runtime::new().expect("could not create tokio runtime"); pub(crate) static ref RT: tokio::runtime::Runtime = tokio::runtime::Runtime::new().expect("could not create tokio runtime");
} }
/// Sets up logging. Useful for debugging. /// Set up logging. Useful for debugging.
pub(crate) fn setup_logger(debug: bool, path: Option<String>) { pub(crate) fn setup_logger(debug: bool, path: Option<String>) {
let format = tracing_subscriber::fmt::format() let format = tracing_subscriber::fmt::format()
.with_level(true) .with_level(true)
@ -43,23 +43,44 @@ pub(crate) fn setup_logger(debug: bool, path: Option<String>) {
} }
} }
/// A trait meant for our [crate::Result] type to make converting it to Java easier. /// A trait meant for our local result type to make converting it to Java easier.
/// jni-rs technically has [jni::errors::ToException], but this approach keeps it stream-like. /// jni-rs technically has [jni::errors::ToException], but this approach keeps it stream-like.
pub(crate) trait JExceptable<T> { pub(crate) trait JExceptable<T> {
/// Unwraps it and throws an appropriate Java exception if it's an error. /// Unwrap it and throws an appropriate Java exception if it's an error.
/// Theoretically it returns the type's default value, but the exception makes the value ignored. /// Theoretically it returns the type's default value, but the exception makes the value ignored.
fn jexcept(self, env: &mut jni::JNIEnv) -> T; fn jexcept(self, env: &mut jni::JNIEnv) -> T;
} }
impl<T> JExceptable<T> for crate::Result<T> where T: Default { impl<T> JExceptable<T> for crate::errors::ConnectionResult<T> where T: Default {
fn jexcept(self, env: &mut jni::JNIEnv) -> T { fn jexcept(self, env: &mut jni::JNIEnv) -> T {
if let Err(err) = &self { if let Err(err) = &self {
let msg = format!("{err}"); let msg = format!("{err}");
match err { match err {
crate::Error::InvalidState { .. } => env.throw_new("mp/code/exceptions/InvalidStateException", msg), crate::errors::ConnectionError::Transport(_) => env.throw_new("mp/code/exceptions/ConnectionTransportException", msg),
crate::Error::Deadlocked => env.throw_new("mp/code/exceptions/DeadlockedException", msg), crate::errors::ConnectionError::Remote(_) => env.throw_new("mp/code/exceptions/ConnectionRemoteException", msg),
crate::Error::Transport { .. } => env.throw_new("mp/code/exceptions/TransportException", msg), }.jexcept(env);
crate::Error::Channel { .. } => env.throw_new("mp/code/exceptions/ChannelException", msg) }
self.unwrap_or_default()
}
}
impl<T> JExceptable<T> for crate::errors::RemoteResult<T> where T: Default {
fn jexcept(self, env: &mut jni::JNIEnv) -> T {
if let Err(err) = &self {
let msg = format!("{err}");
env.throw_new("mp/code/exceptions/connection/RemoteException", msg).jexcept(env);
}
self.unwrap_or_default()
}
}
impl<T> JExceptable<T> for crate::errors::ControllerResult<T> where T: Default {
fn jexcept(self, env: &mut jni::JNIEnv) -> T {
if let Err(err) = &self {
let msg = format!("{err}");
match err {
crate::errors::ControllerError::Stopped => env.throw_new("mp/code/exceptions/ControllerStoppedException", msg),
crate::errors::ControllerError::Unfulfilled => env.throw_new("mp/code/exceptions/ControllerUnfulfilledException", msg),
}.jexcept(env); }.jexcept(env);
} }
self.unwrap_or_default() self.unwrap_or_default()
@ -94,7 +115,7 @@ pub(crate) trait JObjectify<'local> {
/// The error type, likely to be [jni::errors::Error]. /// The error type, likely to be [jni::errors::Error].
type Error; type Error;
/// Attempts to convert the given object to a [jni::objects::JObject]. /// Attempt to convert the given object to a [jni::objects::JObject].
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, Self::Error>; fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, Self::Error>;
} }

View file

@ -2,7 +2,7 @@ use jni::{objects::{JClass, JString}, sys::jlong, JNIEnv};
use super::JExceptable; use super::JExceptable;
/// Calculates the XXH3 hash for a given String. /// Calculate the XXH3 hash for a given String.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Utils_hash<'local>( pub extern "system" fn Java_mp_code_Utils_hash<'local>(
mut env: JNIEnv, mut env: JNIEnv,

View file

@ -3,7 +3,7 @@ use crate::Workspace;
use super::{JExceptable, JObjectify, RT}; use super::{JExceptable, JObjectify, RT};
/// Gets the workspace id. /// Get the workspace id.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_get_1workspace_1id<'local>( pub extern "system" fn Java_mp_code_Workspace_get_1workspace_1id<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -14,7 +14,7 @@ pub extern "system" fn Java_mp_code_Workspace_get_1workspace_1id<'local>(
env.new_string(workspace.id()).jexcept(&mut env).as_raw() env.new_string(workspace.id()).jexcept(&mut env).as_raw()
} }
/// Gets a cursor controller by name and returns a pointer to it. /// Get a cursor controller by name and returns a pointer to it.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_get_1cursor<'local>( pub extern "system" fn Java_mp_code_Workspace_get_1cursor<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -27,7 +27,7 @@ pub extern "system" fn Java_mp_code_Workspace_get_1cursor<'local>(
).jexcept(&mut env).as_raw() ).jexcept(&mut env).as_raw()
} }
/// Gets a buffer controller by name and returns a pointer to it. /// Get a buffer controller by name and returns a pointer to it.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_get_1buffer<'local>( pub extern "system" fn Java_mp_code_Workspace_get_1buffer<'local>(
mut env: JNIEnv<'local>, mut env: JNIEnv<'local>,
@ -47,7 +47,7 @@ pub extern "system" fn Java_mp_code_Workspace_get_1buffer<'local>(
}).unwrap_or_default().as_raw() }).unwrap_or_default().as_raw()
} }
/// Creates a new buffer. /// Create a new buffer.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>( pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -63,7 +63,7 @@ pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>(
.jexcept(&mut env); .jexcept(&mut env);
} }
/// Gets the filetree. /// Get the filetree.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree( pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree(
mut env: JNIEnv, mut env: JNIEnv,
@ -95,7 +95,7 @@ pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree(
}).jexcept(&mut env).as_raw() }).jexcept(&mut env).as_raw()
} }
/// Attaches to a buffer and returns a pointer to its [crate::buffer::Controller]. /// Attach to a buffer and return a pointer to its [crate::buffer::Controller].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_attach_1to_1buffer<'local>( pub extern "system" fn Java_mp_code_Workspace_attach_1to_1buffer<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -116,6 +116,7 @@ pub extern "system" fn Java_mp_code_Workspace_attach_1to_1buffer<'local>(
}).jexcept(&mut env).as_raw() }).jexcept(&mut env).as_raw()
} }
/// Detach from a buffer.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_detach_1from_1buffer<'local>( pub extern "system" fn Java_mp_code_Workspace_detach_1from_1buffer<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -128,9 +129,9 @@ pub extern "system" fn Java_mp_code_Workspace_detach_1from_1buffer<'local>(
.map(|path| path.to_string_lossy().to_string()) .map(|path| path.to_string_lossy().to_string())
.jexcept(&mut env); .jexcept(&mut env);
let name = match workspace.detach(&path) { let name = match workspace.detach(&path) {
crate::workspace::worker::DetachResult::NotAttached => "NOT_ATTACHED", crate::workspace::DetachResult::NotAttached => "NOT_ATTACHED",
crate::workspace::worker::DetachResult::Detaching => "DETACHED", crate::workspace::DetachResult::Detaching => "DETACHED",
crate::workspace::worker::DetachResult::AlreadyDetached => "ALREADY_DETACHED" crate::workspace::DetachResult::AlreadyDetached => "ALREADY_DETACHED"
}; };
env.find_class("mp/code/data/DetachResult") env.find_class("mp/code/data/DetachResult")
@ -140,7 +141,7 @@ pub extern "system" fn Java_mp_code_Workspace_detach_1from_1buffer<'local>(
.as_raw() .as_raw()
} }
/// Updates the local buffer list. /// Update the local buffer list.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers( pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers(
mut env: JNIEnv, mut env: JNIEnv,
@ -151,7 +152,7 @@ pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers(
RT.block_on(workspace.fetch_buffers()).jexcept(&mut env); RT.block_on(workspace.fetch_buffers()).jexcept(&mut env);
} }
/// Updates the local user list. /// Update the local user list.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_fetch_1users( pub extern "system" fn Java_mp_code_Workspace_fetch_1users(
mut env: JNIEnv, mut env: JNIEnv,
@ -162,7 +163,7 @@ pub extern "system" fn Java_mp_code_Workspace_fetch_1users(
RT.block_on(workspace.fetch_users()).jexcept(&mut env); RT.block_on(workspace.fetch_users()).jexcept(&mut env);
} }
/// Lists users attached to a buffer. /// List users attached to a buffer.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>( pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -189,7 +190,7 @@ pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
}).jexcept(&mut env).as_raw() }).jexcept(&mut env).as_raw()
} }
/// Deletes a buffer. /// Delete a buffer.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_delete_1buffer<'local>( pub extern "system" fn Java_mp_code_Workspace_delete_1buffer<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -201,10 +202,11 @@ pub extern "system" fn Java_mp_code_Workspace_delete_1buffer<'local>(
let buffer = unsafe { env.get_string_unchecked(&input) } let buffer = unsafe { env.get_string_unchecked(&input) }
.map(|buffer| buffer.to_string_lossy().to_string()) .map(|buffer| buffer.to_string_lossy().to_string())
.jexcept(&mut env); .jexcept(&mut env);
RT.block_on(workspace.delete(&buffer)).jexcept(&mut env); RT.block_on(workspace.delete(&buffer))
.jexcept(&mut env);
} }
/// Receives a workspace event if present. /// Receive a workspace event if present.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_event( pub extern "system" fn Java_mp_code_Workspace_event(
mut env: JNIEnv, mut env: JNIEnv,
@ -236,7 +238,7 @@ pub extern "system" fn Java_mp_code_Workspace_event(
}).jexcept(&mut env).as_raw() }).jexcept(&mut env).as_raw()
} }
/// Polls a list of buffers, returning the first ready one. /// Poll a list of buffers, returning the first ready one.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_select_1buffer( pub extern "system" fn Java_mp_code_Workspace_select_1buffer(
mut env: JNIEnv, mut env: JNIEnv,