feat: implemented connect

This commit is contained in:
zaaarf 2023-08-19 23:42:27 +02:00
parent c263211cff
commit 8e63a5493e
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
4 changed files with 66 additions and 2 deletions

6
.gitignore vendored
View file

@ -41,4 +41,8 @@ bin/
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
.idea/* .idea/*
### Rust ###
src/main/rust/Cargo.lock
src/main/rust/target/*

View file

@ -1,5 +1,5 @@
package com.codemp.intellij; package com.codemp.intellij;
public class CodeMP { public class CodeMP {
public static native void connect(String addr);
} }

14
src/main/rust/Cargo.toml Normal file
View file

@ -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"]

46
src/main/rust/src/lib.rs Normal file
View file

@ -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::<CodempError> 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);
}
}
}