From e23ab07dfcd3749ed4d5b70c79892eff811710fa Mon Sep 17 00:00:00 2001 From: Adriano Santos Date: Tue, 19 Sep 2023 09:43:17 -0300 Subject: [PATCH] Refactor and better context --- spawn-examples/src/{ => actors}/joe.rs | 10 +++--- spawn-examples/src/actors/mod.rs | 1 + spawn-examples/src/main.rs | 11 ++++--- spawn-rs/src/actor.rs | 2 +- spawn-rs/src/context.rs | 30 ------------------ spawn-rs/src/handler/actor_router.rs | 2 +- spawn-rs/src/lib.rs | 43 +++++++++++++++++++------- spawn-rs/src/spawn.rs | 2 +- spawn-rs/src/value.rs | 16 +++++++--- 9 files changed, 60 insertions(+), 57 deletions(-) rename spawn-examples/src/{ => actors}/joe.rs (59%) create mode 100644 spawn-examples/src/actors/mod.rs delete mode 100644 spawn-rs/src/context.rs diff --git a/spawn-examples/src/joe.rs b/spawn-examples/src/actors/joe.rs similarity index 59% rename from spawn-examples/src/joe.rs rename to spawn-examples/src/actors/joe.rs index 82e29ec..f205740 100644 --- a/spawn-examples/src/joe.rs +++ b/spawn-examples/src/actors/joe.rs @@ -1,5 +1,5 @@ -use spawn_examples::domain::domain::{Reply, Request}; -use spawn_rs::{context::Context, value::Value, Message}; +use spawn_examples::domain::domain::{Reply, Request, State}; +use spawn_rs::{value::Value, Context, Message}; use log::info; @@ -13,11 +13,13 @@ pub fn set_language(msg: Message, ctx: Context) -> Value { reply.response = lang; Value::new() - .state(ctx.state().clone()) + .state::(&ctx.state::().unwrap(), "domain.State".to_string()) .response(&reply, "domain.Reply".to_string()) .to_owned() } - Err(_e) => Value::new().state(ctx.state().clone()).to_owned(), + Err(_e) => Value::new() + .state::(&ctx.state::().unwrap(), "domain.State".to_string()) + .to_owned(), }; return value; diff --git a/spawn-examples/src/actors/mod.rs b/spawn-examples/src/actors/mod.rs new file mode 100644 index 0000000..ff68b88 --- /dev/null +++ b/spawn-examples/src/actors/mod.rs @@ -0,0 +1 @@ +pub mod joe; diff --git a/spawn-examples/src/main.rs b/spawn-examples/src/main.rs index 7e26874..0c9fb20 100644 --- a/spawn-examples/src/main.rs +++ b/spawn-examples/src/main.rs @@ -2,15 +2,15 @@ extern crate env_logger; extern crate prost_types; extern crate rocket; -mod joe; +mod actors; -use joe::set_language; +use actors::joe::set_language; use spawn_rs::actor::{ActorDefinition, ActorSettings, Kind}; use spawn_rs::spawn::Spawn; #[rocket::main] async fn main() -> Result<(), rocket::Error> { - Spawn::new() + let mut spawn: Spawn = Spawn::new() .create("spawn-system".to_string()) .with_actor( ActorDefinition::new() @@ -25,8 +25,9 @@ async fn main() -> Result<(), rocket::Error> { ) .with_action("setLanguage".to_owned(), set_language), ) - .start() - .await?; + .clone(); + + spawn.start().await?; Ok(()) } diff --git a/spawn-rs/src/actor.rs b/spawn-rs/src/actor.rs index 851d77b..cae6c2a 100644 --- a/spawn-rs/src/actor.rs +++ b/spawn-rs/src/actor.rs @@ -1,5 +1,5 @@ use crate::Message; -use crate::{context::Context, value::Value}; +use crate::{value::Value, Context}; use std::collections::HashMap; diff --git a/spawn-rs/src/context.rs b/spawn-rs/src/context.rs deleted file mode 100644 index ebee38c..0000000 --- a/spawn-rs/src/context.rs +++ /dev/null @@ -1,30 +0,0 @@ -use prost_types::Any; - -#[derive(Debug, Clone)] -pub struct Context { - state: Any, -} - -impl Default for Context { - fn default() -> Context { - Context { - state: Any::default(), - } - } -} - -impl Context { - pub fn new() -> Self { - Default::default() - } - - /// Returns a reference to the state of this [`Context`]. - pub fn state(&self) -> &Any { - &self.state - } - - /// Sets the state of this [`Context`]. - pub fn set_state(&mut self, state: Any) { - self.state = state; - } -} diff --git a/spawn-rs/src/handler/actor_router.rs b/spawn-rs/src/handler/actor_router.rs index 01b041b..9083170 100644 --- a/spawn-rs/src/handler/actor_router.rs +++ b/spawn-rs/src/handler/actor_router.rs @@ -2,13 +2,13 @@ use std::collections::HashMap; use crate::actor::ActorDefinition; -use crate::context::Context as ActorContext; use crate::eigr::spawn::actor_invocation::Payload; use crate::eigr::spawn::actor_invocation_response::Payload as ResponsePayload; use crate::eigr::spawn::{ ActorId, ActorInvocation, ActorInvocationResponse, Context, Noop, Workflow, }; use crate::value::Value; +use crate::Context as ActorContext; use crate::Message as ActorMessage; use log::{debug, info}; diff --git a/spawn-rs/src/lib.rs b/spawn-rs/src/lib.rs index 5d9f614..b58f1d9 100644 --- a/spawn-rs/src/lib.rs +++ b/spawn-rs/src/lib.rs @@ -6,7 +6,6 @@ extern crate prost_types; mod eigr; pub mod actor; -pub mod context; pub mod handler; pub mod serializer; pub mod spawn; @@ -15,16 +14,6 @@ pub mod value; use prost::DecodeError; use prost_types::Any; -// fn to_any(message: &T) -> Any -// where -// T: prost::Message, -// { -// Any { -// type_url: T::type_url().to_string(), -// value: message.encode_to_vec(), -// } -// } - fn from_any(message: &Any) -> Result where T: prost::Message + Default, @@ -61,3 +50,35 @@ impl Message { self.body = message } } + +#[derive(Debug, Clone)] +pub struct Context { + state: Any, +} + +impl Default for Context { + fn default() -> Context { + Context { + state: Any::default(), + } + } +} + +impl Context { + pub fn new() -> Self { + Default::default() + } + + /// Returns a reference to the state of this [`Context`]. + pub fn state(&self) -> Result + where + T: prost::Message + Default, + { + from_any(&self.state) + } + + /// Sets the state of this [`Context`]. + pub fn set_state(&mut self, state: Any) { + self.state = state; + } +} diff --git a/spawn-rs/src/spawn.rs b/spawn-rs/src/spawn.rs index f15812b..5ad62ab 100644 --- a/spawn-rs/src/spawn.rs +++ b/spawn-rs/src/spawn.rs @@ -31,7 +31,7 @@ async fn handle(data: Data<'_>, handler: &State>>) -> io::Res return Ok(buf); } -#[derive()] +#[derive(Clone)] pub struct Spawn { system: String, actors: Vec, diff --git a/spawn-rs/src/value.rs b/spawn-rs/src/value.rs index b7a001c..2c12cd5 100644 --- a/spawn-rs/src/value.rs +++ b/spawn-rs/src/value.rs @@ -21,8 +21,16 @@ impl Value { Default::default() } - pub fn state(&mut self, state: Any) -> &mut Value { - self.state = state; + pub fn state(&mut self, state: &T, import: String) -> &mut Value + where + T: prost::Message, + { + let mut value = Vec::new(); + Message::encode(state, &mut value).unwrap(); + + let type_url = format!("type.googleapis.com/{}", import); + + self.state = Any { type_url, value }; self } @@ -30,14 +38,14 @@ impl Value { &self.state } - pub fn response(&mut self, message: &T, proto_fqdn: String) -> &mut Value + pub fn response(&mut self, message: &T, import: String) -> &mut Value where T: prost::Message, { let mut value = Vec::new(); Message::encode(message, &mut value).unwrap(); - let type_url = format!("type.googleapis.com/{}", proto_fqdn); + let type_url = format!("type.googleapis.com/{}", import); let any = Any { type_url, value }; self.response = any;