codemp/src/ffi/java/client.rs

75 lines
2.8 KiB
Rust
Raw Normal View History

use jni_toolbox::jni;
use crate::{api::Config, client::Client, errors::{ConnectionError, RemoteError}, Workspace};
2024-09-22 02:22:51 +02:00
/// Connect using the given credentials to the default server, and return a [Client] to interact with it.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client", ptr)]
fn connect(config: Config) -> Result<Client, ConnectionError> {
super::tokio().block_on(Client::connect(config))
2024-09-22 02:22:51 +02:00
}
/// Gets the current [crate::api::User].
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client", ptr)]
fn get_user(client: &mut Client) -> crate::api::User {
client.user().clone()
}
2024-09-05 02:45:33 +02:00
/// Join a [Workspace] and return a pointer to it.
2024-09-23 18:12:13 +02:00
#[jni(package = "mp.code", class = "Client")]
fn join_workspace(client: &mut Client, workspace: String) -> Result<Workspace, ConnectionError> {
super::tokio().block_on(client.join_workspace(workspace))
2024-08-06 23:30:00 +02:00
}
/// Create a workspace on server, if allowed to.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn create_workspace(client: &mut Client, workspace: String) -> Result<(), RemoteError> {
super::tokio().block_on(client.create_workspace(workspace))
}
2024-09-05 02:45:33 +02:00
/// Delete a workspace on server, if allowed to.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn delete_workspace(client: &mut Client, workspace: String) -> Result<(), RemoteError> {
super::tokio().block_on(client.delete_workspace(workspace))
}
2024-09-05 02:45:33 +02:00
/// Invite another user to an owned workspace.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn invite_to_workspace(client: &mut Client, workspace: String, user: String) -> Result<(), RemoteError> {
super::tokio().block_on(client.invite_to_workspace(workspace, user))
}
2024-09-05 02:45:33 +02:00
/// List available workspaces.
2024-09-23 18:12:13 +02:00
#[jni(package = "mp.code", class = "Client")]
fn list_workspaces(client: &mut Client, owned: bool, invited: bool) -> Result<Vec<String>, RemoteError> {
super::tokio().block_on(client.list_workspaces(owned, invited))
}
/// List available workspaces.
2024-09-23 18:12:13 +02:00
#[jni(package = "mp.code", class = "Client")]
fn active_workspaces(client: &mut Client) -> Vec<String> {
2024-09-22 02:22:51 +02:00
client.active_workspaces()
2024-08-06 23:30:00 +02:00
}
2024-09-05 02:45:33 +02:00
/// Leave a [Workspace] and return whether or not the client was in such workspace.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn leave_workspace(client: &mut Client, workspace: String) -> bool {
2024-09-22 02:22:51 +02:00
client.leave_workspace(&workspace)
}
2024-09-05 02:45:33 +02:00
/// Get a [Workspace] by name and returns a pointer to it.
2024-09-23 18:12:13 +02:00
#[jni(package = "mp.code", class = "Client")]
fn get_workspace(client: &mut Client, workspace: String) -> Option<Workspace> {
2024-09-22 02:22:51 +02:00
client.get_workspace(&workspace)
}
2024-09-05 02:45:33 +02:00
/// Refresh the client's session token.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn refresh(client: &mut Client) -> Result<(), RemoteError> {
super::tokio().block_on(client.refresh())
}
2024-08-08 00:29:54 +02:00
/// Called by the Java GC to drop a [Client].
#[jni(package = "mp.code", class = "Client")]
fn free(input: jni::sys::jlong) {
2024-08-08 00:29:54 +02:00
let _ = unsafe { Box::from_raw(input as *mut Client) };
}