mirror of
https://github.com/hexedtech/codemp-intellij.git
synced 2024-11-24 16:04:48 +01:00
feat: implemented connect
This commit is contained in:
parent
c263211cff
commit
8e63a5493e
4 changed files with 66 additions and 2 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -41,4 +41,8 @@ bin/
|
|||
### Mac OS ###
|
||||
.DS_Store
|
||||
|
||||
.idea/*
|
||||
.idea/*
|
||||
|
||||
### Rust ###
|
||||
src/main/rust/Cargo.lock
|
||||
src/main/rust/target/*
|
|
@ -1,5 +1,5 @@
|
|||
package com.codemp.intellij;
|
||||
|
||||
public class CodeMP {
|
||||
|
||||
public static native void connect(String addr);
|
||||
}
|
||||
|
|
14
src/main/rust/Cargo.toml
Normal file
14
src/main/rust/Cargo.toml
Normal 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
46
src/main/rust/src/lib.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue