generated from QuiltMC/quilt-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create structure of new token-getting Server in Rust
- Loading branch information
Showing
13 changed files
with
177 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/Server.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.pixaurora.kitten_thoughts.impl.http.server; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Server { | ||
public static Server create(String tokenArgName) { | ||
return new ServerImpl(tokenArgName); | ||
} | ||
|
||
public String runServer() throws IOException; | ||
} |
18 changes: 18 additions & 0 deletions
18
...ten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/ServerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package net.pixaurora.kitten_thoughts.impl.http.server; | ||
|
||
public class ServerImpl implements Server { | ||
private final long pointer; | ||
|
||
public ServerImpl(String tokenArgName) { | ||
this.pointer = create(tokenArgName); | ||
} | ||
|
||
private static native long create(String tokenArgName); | ||
|
||
@Override | ||
public String runServer() { | ||
return this.runServer0(); | ||
} | ||
|
||
private native String runServer0(); | ||
} |
5 changes: 0 additions & 5 deletions
5
...n-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/scrobbler/ScrobblerSetup.java
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...cts/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/util/CryptoUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use crate::Result; | ||
use jni::{objects::JObject, sys::jlong, JNIEnv}; | ||
|
||
mod http; | ||
|
||
fn get_rust_pointer<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>) -> Result<jlong> { | ||
Ok(env.get_field(object, "pointer", "J")?.try_into()?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod server; |
62 changes: 62 additions & 0 deletions
62
projects/kitten-thoughts/src/main/rust/bridge/http/server.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::Result; | ||
|
||
use jni::{ | ||
objects::{JClass, JObject, JString}, | ||
sys::jlong, | ||
JNIEnv, | ||
}; | ||
|
||
use crate::{bridge::get_rust_pointer, http::server::Server}; | ||
|
||
fn create<'local>(token_arg_name: JString<'local>, env: &mut JNIEnv<'local>) -> Result<jlong> { | ||
let token_arg_name: String = env.get_string(&token_arg_name)?.into(); | ||
|
||
let server = Server::new(token_arg_name); | ||
|
||
Ok(Box::into_raw(Box::from(server)) as jlong) | ||
} | ||
|
||
fn run_server<'local>( | ||
object: &JObject<'local>, | ||
env: &mut JNIEnv<'local>, | ||
) -> Result<JString<'local>> { | ||
let pointer = get_rust_pointer(env, object)?; | ||
let server = unsafe { &*(pointer as *mut Server) }; | ||
|
||
let token = server.run_server()?; | ||
|
||
Ok(env.new_string(token)?) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_http_server_ServerImpl_create< | ||
'local, | ||
>( | ||
mut env: JNIEnv<'local>, | ||
_class: JClass<'local>, | ||
token_arg_name: JString<'local>, | ||
) -> jlong { | ||
match create(token_arg_name, &mut env) { | ||
Ok(pointer) => pointer, | ||
Err(b) => { | ||
b.throw(&mut env); | ||
jlong::default() | ||
} | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_http_server_ServerImpl_runServer0< | ||
'local, | ||
>( | ||
mut env: JNIEnv<'local>, | ||
object: JObject<'local>, | ||
) -> JString<'local> { | ||
match run_server(&object, &mut env) { | ||
Ok(token) => token, | ||
Err(error) => { | ||
error.throw(&mut env); | ||
JString::default().into() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::fmt::Display; | ||
|
||
use jni::errors::Error as JNIError; | ||
use jni::JNIEnv; | ||
use std::io::Error as IOError; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
JNI(JNIError), | ||
IO(IOError), | ||
} | ||
|
||
pub type Result<T> = core::result::Result<T, Error>; | ||
|
||
impl Error { | ||
pub fn throw(&self, env: &mut JNIEnv) { | ||
let a = env | ||
.throw_new("java/io/IOException", self.to_string()) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
impl Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Error::JNI(error) => error.fmt(f), | ||
Error::IO(error) => error.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl From<JNIError> for Error { | ||
fn from(value: JNIError) -> Self { | ||
Error::JNI(value) | ||
} | ||
} | ||
|
||
impl From<IOError> for Error { | ||
fn from(value: IOError) -> Self { | ||
Error::IO(value) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::Result; | ||
|
||
pub struct Server { | ||
token_arg_name: String, | ||
} | ||
|
||
impl Server { | ||
pub fn new(token_arg_name: String) -> Self { | ||
Server { token_arg_name } | ||
} | ||
|
||
pub fn run_server(&self) -> Result<String> { | ||
Ok(format!( | ||
"Wait... this isn't a token! Token argument name is {}", | ||
self.token_arg_name | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,6 @@ | ||
use jni::{ | ||
objects::{JClass, JString}, | ||
sys::jstring, | ||
JNIEnv, | ||
}; | ||
mod bridge; | ||
pub mod errors; | ||
pub mod http; | ||
|
||
pub mod hello; | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_scrobbler_ScrobblerSetup_hello< | ||
'local, | ||
>( | ||
mut env: JNIEnv<'local>, | ||
_class: JClass<'local>, | ||
input: JString<'local>, | ||
) -> jstring { | ||
let input: String = env | ||
.get_string(&input) | ||
.expect("Couldn't get java string!") | ||
.into(); | ||
|
||
let output = hello::hello(input); | ||
let output = env | ||
.new_string(output) | ||
.expect("Couldn't create java string!"); | ||
|
||
output.into_raw() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::hello::hello; | ||
|
||
#[test] | ||
fn hello_message() { | ||
let output = hello("Coolcat".into()); | ||
|
||
assert_eq!(output, "Hello, Coolcat!"); | ||
} | ||
} | ||
pub use errors::Error; | ||
pub use errors::Result; |