Skip to content

Commit

Permalink
Refactor and better context
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Santos committed Sep 19, 2023
1 parent 470258d commit e23ab07
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 57 deletions.
10 changes: 6 additions & 4 deletions spawn-examples/src/joe.rs → spawn-examples/src/actors/joe.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,11 +13,13 @@ pub fn set_language(msg: Message, ctx: Context) -> Value {
reply.response = lang;

Value::new()
.state(ctx.state().clone())
.state::<State>(&ctx.state::<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::<State>(&ctx.state::<State>().unwrap(), "domain.State".to_string())
.to_owned(),
};

return value;
Expand Down
1 change: 1 addition & 0 deletions spawn-examples/src/actors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod joe;
11 changes: 6 additions & 5 deletions spawn-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -25,8 +25,9 @@ async fn main() -> Result<(), rocket::Error> {
)
.with_action("setLanguage".to_owned(), set_language),
)
.start()
.await?;
.clone();

spawn.start().await?;

Ok(())
}
2 changes: 1 addition & 1 deletion spawn-rs/src/actor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Message;
use crate::{context::Context, value::Value};
use crate::{value::Value, Context};

use std::collections::HashMap;

Expand Down
30 changes: 0 additions & 30 deletions spawn-rs/src/context.rs

This file was deleted.

2 changes: 1 addition & 1 deletion spawn-rs/src/handler/actor_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
43 changes: 32 additions & 11 deletions spawn-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,16 +14,6 @@ pub mod value;
use prost::DecodeError;
use prost_types::Any;

// fn to_any<T>(message: &T) -> Any
// where
// T: prost::Message,
// {
// Any {
// type_url: T::type_url().to_string(),
// value: message.encode_to_vec(),
// }
// }

fn from_any<T>(message: &Any) -> Result<T, DecodeError>
where
T: prost::Message + Default,
Expand Down Expand Up @@ -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<T>(&self) -> Result<T, DecodeError>
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;
}
}
2 changes: 1 addition & 1 deletion spawn-rs/src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn handle(data: Data<'_>, handler: &State<Arc<Mutex<Handler>>>) -> io::Res
return Ok(buf);
}

#[derive()]
#[derive(Clone)]
pub struct Spawn {
system: String,
actors: Vec<ActorDefinition>,
Expand Down
16 changes: 12 additions & 4 deletions spawn-rs/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ impl Value {
Default::default()
}

pub fn state(&mut self, state: Any) -> &mut Value {
self.state = state;
pub fn state<T>(&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
}

pub fn get_state(&self) -> &Any {
&self.state
}

pub fn response<T>(&mut self, message: &T, proto_fqdn: String) -> &mut Value
pub fn response<T>(&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;
Expand Down

0 comments on commit e23ab07

Please sign in to comment.