use std::any::Any;
use rs_ervice_macro_lib::{r_service, r_service_struct};
#[r_service_struct]
#[derive(Debug, Clone)]
struct MyService {
pub state: String,
}
trait Chant{
fn chanting(st:String) -> String;
}
#[r_service]
impl MyService {
pub fn new() -> Self {
MyService{
state : "HELLO - ".to_string(),
}
}
#[cfg(feature = "tokio")]
pub async fn doing_something(&self, something: String) -> String {
self.state.clone() + &something
}
#[cfg(not(feature = "tokio"))]
pub fn doing_something(&self, something: String) -> String {
self.state.clone() + &something
}
}
impl Chant for MyService {
fn chanting(st: String) -> String {
st + "!!!!!"
}
}
impl RSContextService for MyService {
fn on_register_crate_instance() -> Self {
MyService::new()
}
fn on_service_created(&mut self, service_builder: &RSContextBuilder) -> Result<(), RsServiceError> {
println!("Service {} registered successfully!", std::any::type_name::<Self>());
Ok(())
}
fn on_all_services_built(&self, context: &rs_ervice::RSContext) -> Result<(), RsServiceError> {
println!("All services built successfully in context: {:?}", context.type_id());
Ok(())
}
}
#[r_service_struct]
#[derive(Debug, Clone)]
struct AnotherService {
}
#[r_service]
impl AnotherService {
pub fn new() -> Self {
AnotherService {}
}
}
impl RSContextService for AnotherService {
fn on_register_crate_instance() -> Self {
AnotherService::new()
}
fn on_service_created(&mut self, service_builder: &RSContextBuilder) -> Result<(), RsServiceError> {
print!("AnotherService registered!\n");
Ok(())
}
fn on_all_services_built(&self, context: &rs_ervice::RSContext) -> Result<(), RsServiceError> {
println!("All services built successfully in context: {:?}", context.type_id());
Ok(())
}
}
use rs_ervice::{RSContextBuilder, RSContextService, RsServiceError};
#[cfg(not(feature = "tokio"))]
fn main(){
let service_context = RSContextBuilder::new()
.register::<MyService>()
.register::<AnotherService>()
.build()
.expect("Failed to build RSContext");
let do_service = service_context.call::<MyService>();
if do_service.is_none() {
panic!("MyService is not registered!");
}
let do_service = do_service.unwrap().lock().unwrap().doing_something("Hi!".to_string());
println!("{}", do_service);
let chanting_output = <MyService as Chant>::chanting("Hi!".to_string());
println!("{}", chanting_output)
}
#[cfg(feature = "tokio")]
#[tokio::main]
async fn main() {
let service_context = RSContextBuilder::new()
.register::<MyService>()
.register::<AnotherService>()
.build()
.await
.expect("Failed to build RSContext");
let do_service = service_context.call::<MyService>();
if do_service.is_none() {
panic!("MyService is not registered!");
}
let do_service = do_service.unwrap().lock().await.doing_something("Hi!".to_string()).await;
println!("{}", do_service);
let chanting_output = <MyService as Chant>::chanting("Hi!".to_string());
println!("{}", chanting_output);
}