Compare commits
2 Commits
c168df8365
...
0.0.3
Author | SHA1 | Date | |
---|---|---|---|
bfb8c076be | |||
f4b32f650c |
@ -146,8 +146,9 @@ impl RustCompiler {
|
||||
|
||||
f.a0("#[async_trait]");
|
||||
f.a0(format!("pub trait {} {{", definition.name));
|
||||
f.a1("type Context: Clone + Sync + Send + 'static;");
|
||||
for method in definition.methods.iter() {
|
||||
let params = method
|
||||
let mut params = method
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|arg| {
|
||||
@ -157,8 +158,9 @@ impl RustCompiler {
|
||||
Self::type_to_rust_ext(&arg.typ)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ");
|
||||
.collect::<Vec<String>>();
|
||||
params.push("ctx: Self::Context".to_string());
|
||||
let params = params.join(", ");
|
||||
|
||||
let ret = method
|
||||
.output
|
||||
@ -179,17 +181,20 @@ impl RustCompiler {
|
||||
f.a0("}");
|
||||
|
||||
f.a0("");
|
||||
f.a0(format!("pub struct {}Handler {{", definition.name));
|
||||
f.a0(format!("pub struct {}Handler<Context> {{", definition.name));
|
||||
f.a1(format!(
|
||||
"implementation: Box<dyn {} + Sync + Send + 'static>,",
|
||||
"implementation: Box<dyn {}<Context = Context> + Sync + Send + 'static>,",
|
||||
definition.name
|
||||
));
|
||||
f.a0("}");
|
||||
f.a0("");
|
||||
|
||||
f.a0(format!("impl {}Handler {{", definition.name));
|
||||
f.a0(format!(
|
||||
"impl<Context: Clone + Sync + Send + 'static> {}Handler<Context> {{",
|
||||
definition.name
|
||||
));
|
||||
f.a1(format!(
|
||||
"pub fn new(implementation: Box<dyn {} + Sync + Send + 'static>) -> Arc<Self> {{",
|
||||
"pub fn new(implementation: Box<dyn {}<Context = Context> + Sync + Send + 'static>) -> Arc<Self> {{",
|
||||
definition.name,
|
||||
));
|
||||
f.a2("Arc::from(Self { implementation })");
|
||||
@ -200,9 +205,10 @@ impl RustCompiler {
|
||||
|
||||
f.a0("#[async_trait]");
|
||||
f.a0(format!(
|
||||
"impl JRPCServerService for {}Handler {{",
|
||||
"impl<Context: Clone + Sync + Send + 'static> JRPCServerService for {}Handler<Context> {{",
|
||||
definition.name
|
||||
));
|
||||
f.a1("type Context = Context;");
|
||||
f.a1(format!(
|
||||
"fn get_id(&self) -> String {{ \"{}\".to_owned() }} ",
|
||||
definition.name
|
||||
@ -212,7 +218,7 @@ impl RustCompiler {
|
||||
|
||||
f.a1("#[allow(non_snake_case)]");
|
||||
f.a1(
|
||||
"async fn handle(&self, msg: &JRPCRequest, function: &str) -> Result<(bool, Value)> {",
|
||||
"async fn handle(&self, msg: &JRPCRequest, function: &str, ctx: Self::Context) -> Result<(bool, Value)> {",
|
||||
);
|
||||
f.a2("match function {");
|
||||
|
||||
@ -225,7 +231,7 @@ impl RustCompiler {
|
||||
));
|
||||
if method.inputs.len() < 1 {
|
||||
f.a5(format!(
|
||||
"let res = self.implementation.{}().await?;",
|
||||
"let res = self.implementation.{}(ctx).await?;",
|
||||
method.name
|
||||
));
|
||||
f.a5("Ok((true, serde_json::to_value(res)?))");
|
||||
@ -249,7 +255,7 @@ impl RustCompiler {
|
||||
),
|
||||
);
|
||||
}
|
||||
f.a5(").await?;");
|
||||
f.a5(",ctx).await?;");
|
||||
|
||||
if let Some(_output) = &method.output {
|
||||
f.a5("Ok((true, serde_json::to_value(res)?))");
|
||||
@ -277,7 +283,7 @@ impl RustCompiler {
|
||||
),
|
||||
);
|
||||
}
|
||||
f.a5(").await?;");
|
||||
f.a5(", ctx).await?;");
|
||||
if let Some(_output) = &method.output {
|
||||
f.a5("Ok((true, serde_json::to_value(res)?))");
|
||||
} else {
|
||||
|
@ -476,9 +476,12 @@ impl<F: Flavour> Compile for TypeScriptCompiler<F> {
|
||||
for field in definition.fields.iter() {
|
||||
let typ = Self::type_to_typescript_ext(&field.typ);
|
||||
|
||||
let opt = if field.typ.is_optional() { "?" } else { "" };
|
||||
|
||||
f.a1(format!(
|
||||
"public {}: {};",
|
||||
"public {}{}: {};",
|
||||
Self::fix_keyword_name(&field.name),
|
||||
opt,
|
||||
typ
|
||||
));
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
int-enum = { version = "0.5.0", features = ["serde", "convert"] }
|
||||
serde = { version = "1.0.147", features = ["derive"] }
|
||||
serde_json = "1.0.88"
|
||||
nanoid = "0.4.0"
|
||||
tokio = { version = "1.22.0", features = ["full"] }
|
||||
log = "0.4.17"
|
||||
async-trait = "0.1.59"
|
||||
int-enum = { version = "0.5", features = ["serde", "convert"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
nanoid = "0.4"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
log = "0.4"
|
||||
async-trait = "0.1"
|
||||
|
@ -105,21 +105,27 @@ impl JRPCClient {
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait JRPCServerService: Send + Sync + 'static {
|
||||
pub trait JRPCServerService: Send + Sync {
|
||||
type Context;
|
||||
fn get_id(&self) -> String;
|
||||
async fn handle(&self, request: &JRPCRequest, function: &str) -> Result<(bool, Value)>;
|
||||
async fn handle(
|
||||
&self,
|
||||
request: &JRPCRequest,
|
||||
function: &str,
|
||||
ctx: Self::Context,
|
||||
) -> Result<(bool, Value)>;
|
||||
}
|
||||
|
||||
pub type JRPCServiceHandle = Arc<dyn JRPCServerService>;
|
||||
pub type JRPCServiceHandle<Context> = Arc<dyn JRPCServerService<Context = Context>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JRPCSession {
|
||||
server: JRPCServer,
|
||||
pub struct JRPCSession<Context> {
|
||||
server: JRPCServer<Context>,
|
||||
message_sender: Sender<JRPCResult>,
|
||||
}
|
||||
|
||||
impl JRPCSession {
|
||||
pub fn new(server: JRPCServer, sender: Sender<JRPCResult>) -> JRPCSession {
|
||||
impl<Context: Clone + Send + Sync + 'static> JRPCSession<Context> {
|
||||
pub fn new(server: JRPCServer<Context>, sender: Sender<JRPCResult>) -> Self {
|
||||
JRPCSession {
|
||||
server,
|
||||
message_sender: sender,
|
||||
@ -148,7 +154,7 @@ impl JRPCSession {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_request(&self, request: JRPCRequest) -> () {
|
||||
pub fn handle_request(&self, request: JRPCRequest, ctx: Context) -> () {
|
||||
let session = self.clone();
|
||||
tokio::task::spawn(async move {
|
||||
info!("Received request: {}", request.method);
|
||||
@ -163,7 +169,7 @@ impl JRPCSession {
|
||||
|
||||
let service = session.server.services.get(service);
|
||||
if let Some(service) = service {
|
||||
let result = service.handle(&request, function).await;
|
||||
let result = service.handle(&request, function, ctx).await;
|
||||
match result {
|
||||
Ok((is_send, result)) => {
|
||||
if is_send && request.id.is_some() {
|
||||
@ -204,23 +210,23 @@ impl JRPCSession {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JRPCServer {
|
||||
services: HashMap<String, JRPCServiceHandle>,
|
||||
pub struct JRPCServer<Context> {
|
||||
services: HashMap<String, JRPCServiceHandle<Context>>,
|
||||
}
|
||||
|
||||
impl JRPCServer {
|
||||
pub fn new() -> JRPCServer {
|
||||
impl<Context: Clone + Send + Sync + 'static> JRPCServer<Context> {
|
||||
pub fn new() -> Self {
|
||||
JRPCServer {
|
||||
services: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_service(&mut self, service: JRPCServiceHandle) -> () {
|
||||
pub fn add_service(&mut self, service: JRPCServiceHandle<Context>) -> () {
|
||||
let id = service.get_id();
|
||||
self.services.insert(id, service);
|
||||
}
|
||||
|
||||
pub fn get_session(&self, sender: Sender<JRPCResult>) -> JRPCSession {
|
||||
pub fn get_session(&self, sender: Sender<JRPCResult>) -> JRPCSession<Context> {
|
||||
JRPCSession::new(self.clone(), sender)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user