https://github.com/eigr/spawn-rust-sdk
https://github.com/eigr/spawn-rust-sdk
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/eigr/spawn-rust-sdk
- Owner: eigr
- License: apache-2.0
- Created: 2022-07-05T15:46:32.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-05T16:15:06.000Z (about 2 years ago)
- Last Synced: 2025-04-07T18:02:17.225Z (about 1 year ago)
- Language: Rust
- Size: 143 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Spawn Rust SDK
```protobuf
syntax = "proto3";
package domain;
message State {
repeated string languages = 1;
}
message Request {
string language = 1;
}
message Reply {
string response = 1;
}
```
```rust
use spawn_examples::domain::domain::{Reply, Request, State};
use spawn_rs::{value::Value, Context, Message};
use log::info;
pub fn set_language(msg: Message, ctx: Context) -> Value {
info!("Actor msg: {:?}", msg);
return match msg.body::() {
Ok(request) => {
let lang = request.language;
info!("Setlanguage To: {:?}", lang);
let mut reply = Reply::default();
reply.response = lang;
match &ctx.state::() {
Some(state) => Value::new()
.state::(&state.as_ref().unwrap(), "domain.State".to_string())
.response(&reply, "domain.Reply".to_string())
.to_owned(),
_ => Value::new()
.state::(&State::default(), "domain.State".to_string())
.response(&reply, "domain.Reply".to_string())
.to_owned(),
}
}
Err(_e) => Value::new()
.state::(&State::default(), "domain.State".to_string())
.to_owned(),
};
}
pub fn set_language_with_timer(msg: Message, ctx: Context) -> Value {
info!("Actor msg: {:?}", msg);
return match msg.body::() {
Ok(request) => {
let lang = request.language;
info!("Setlanguage To: {:?}", lang);
let mut reply = Reply::default();
reply.response = lang;
match &ctx.state::() {
Some(state) => Value::new()
.state::(&state.as_ref().unwrap(), "domain.State".to_string())
.response(&reply, "domain.Reply".to_string())
.to_owned(),
_ => Value::new()
.state::(&State::default(), "domain.State".to_string())
.response(&reply, "domain.Reply".to_string())
.to_owned(),
}
}
Err(_e) => Value::new()
.state::(&State::default(), "domain.State".to_string())
.to_owned(),
};
}
```
```rust
extern crate env_logger;
extern crate prost_types;
extern crate rocket;
mod joe;
use actors::joe::{set_language, set_language_with_timer};
use spawn_rs::actor::{ActorDefinition, ActorSettings, Kind};
use spawn_rs::spawn::Spawn;
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
let mut spawn: Spawn = Spawn::new()
.create("spawn-system".to_string())
.with_service_name(env!("CARGO_PKG_NAME").to_string()) // optional
.with_service_version(env!("CARGO_PKG_VERSION").to_string()) // optional
.with_proxy_port(9003)
.with_actor(
ActorDefinition::new()
.with_settings(
ActorSettings::new()
.name("joe".to_owned())
.kind(Kind::NAMED)
.stateful(true)
.deactivated_timeout(30000) // optional
.snapshot_timeout(10000) // optional
.to_owned(),
)
.with_action("setLanguage".to_owned(), set_language)
.with_timer_action(
"set_language_with_timer".to_owned(),
set_language_with_timer,
1000,
),
)
.clone();
spawn.start().await?;
Ok(())
}
```