From 8e63a5493e3d782cd547c743c227311763c1bdf1 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Sat, 19 Aug 2023 23:42:27 +0200 Subject: [PATCH] feat: implemented connect --- .gitignore | 6 ++- src/main/java/com/codemp/intellij/CodeMP.java | 2 +- src/main/rust/Cargo.toml | 14 ++++++ src/main/rust/src/lib.rs | 46 +++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/main/rust/Cargo.toml create mode 100644 src/main/rust/src/lib.rs diff --git a/.gitignore b/.gitignore index a156b7a..86096d8 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,8 @@ bin/ ### Mac OS ### .DS_Store -.idea/* \ No newline at end of file +.idea/* + +### Rust ### +src/main/rust/Cargo.lock +src/main/rust/target/* \ No newline at end of file diff --git a/src/main/java/com/codemp/intellij/CodeMP.java b/src/main/java/com/codemp/intellij/CodeMP.java index b07f549..2d56730 100644 --- a/src/main/java/com/codemp/intellij/CodeMP.java +++ b/src/main/java/com/codemp/intellij/CodeMP.java @@ -1,5 +1,5 @@ package com.codemp.intellij; public class CodeMP { - + public static native void connect(String addr); } diff --git a/src/main/rust/Cargo.toml b/src/main/rust/Cargo.toml new file mode 100644 index 0000000..4088d81 --- /dev/null +++ b/src/main/rust/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "codemp-intellij" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +# codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", rev = "a2a841d8921871a0e254ff901458d038b5ee659f" } +codemp = { path = "../../../../codemp/", features = ["global", "sync"] } +jni = "0.21.1" + +[lib] +crate_type = ["cdylib"] diff --git a/src/main/rust/src/lib.rs b/src/main/rust/src/lib.rs new file mode 100644 index 0000000..4d86503 --- /dev/null +++ b/src/main/rust/src/lib.rs @@ -0,0 +1,46 @@ +use jni::JNIEnv; //interface to the JVM +use jni::objects::{JClass, JString}; //stuff with lifetime +use codemp::prelude::*; + +const JAVA_PACKAGE: &str = "com.codemp.intellij"; +const JAVA_FOLDER: &str = "com/codemp/intellij"; + +#[no_mangle] +pub extern "system" fn Java_CodeMP_connect<'local>(mut env: JNIEnv<'local>, class: JClass<'local>, input: JString<'local>) { + let addr: String = env.get_string(&input).expect("Failed to get String from JVM!").into(); + match CODEMP_INSTANCE.connect(&addr) { + Ok(()) => (), + Err(err) => ErrorWrapper(err).throw(env) + } +} + +struct ErrorWrapper(CodempError); + +impl From:: for ErrorWrapper { + fn from(value: CodempError) -> Self { + ErrorWrapper(value) + } +} + +impl ErrorWrapper { + fn throw(&self, mut env: JNIEnv) { + let exception_package: String = format!("{}/exceptions", JAVA_FOLDER); + let res = match &self.0 { + CodempError::Transport { status, message } => env.throw_new(format!("{}/TransportException", exception_package), format!("Error {}: {}", status, message)), + CodempError::InvalidState { msg } => env.throw_new(format!("{}/InvalidStateException", exception_package), msg), + CodempError::Filler { message } => env.throw_new(format!("{}/CodeMPException", exception_package), message), + CodempError::Channel { send } => { + let class_name:String = if *send { + format!("{}/ChannelException/Send", exception_package) + } else { + format!("{}/ChannelException/Read", exception_package) + }; + env.throw_new(class_name, "The requested channel was closed!") + } + }; + + if let Err(e) = res { + panic!("An error occurred while converting a Rust error to a Java Exception: {}", e); + } + } +} \ No newline at end of file