Skip to content

Commit

Permalink
feat(template): add client and tests to the program template (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky authored Aug 14, 2024
1 parent 9215ce0 commit dc5cf13
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
4 changes: 3 additions & 1 deletion templates/program/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sails-rs = "{{ sails-rs-version }}"

[build-dependencies]
{{ project-name }} = { path = ".." }
sails-client-gen = "{{ sails-rs-version }}"
sails-idl-gen = "{{ sails-rs-version }}"

[features]
mocks = ["sails-rs/mockall", "dep:mockall"]
{{ mocks-feature-name }} = ["sails-rs/mockall", "dep:mockall"]
16 changes: 15 additions & 1 deletion templates/program/client/build.rs
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
fn main() {}
use sails_client_gen::ClientGenerator;
use std::{env, path::PathBuf};

use {{ crate_name }}::{{ program-struct-name }} as ProgramType;

fn main() {
let idl_file_path = PathBuf::from("{{ crate_name }}.idl");
// Generate IDL file for the program
sails_idl_gen::generate_idl_to_file::<ProgramType>(&idl_file_path).unwrap();
// Generate client code from IDL file
ClientGenerator::from_idl_path(&idl_file_path)
.with_mocks("{{ mocks-feature-name}}")
.generate_to(PathBuf::from(env::var("OUT_DIR").unwrap()).join("{{ client_crate_name }}.rs"))
.unwrap();
}
4 changes: 4 additions & 0 deletions templates/program/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![no_std]

// Incorporate code generated based on the IDL file
include!(concat!(env!("OUT_DIR"), "/{{ client_crate_name }}.rs"));
17 changes: 10 additions & 7 deletions templates/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@ pub use code::WASM_BINARY_OPT as WASM_BINARY;

use sails_rs::prelude::*;

struct {{ program-name }}Service(());
struct {{ service-struct-name }}(());

#[sails_rs::service]
impl {{ program-name }}Service {
impl {{ service-struct-name }} {
pub fn new() -> Self {
Self(())
}

// Service's method (command)
pub fn do_something(&mut self) -> String {
"Hello from {{ program-name }}!".to_string()
"Hello from {{ service-name }}!".to_string()
}
}

pub struct {{ program-name }}Program(());
pub struct {{ program-struct-name }}(());

#[sails_rs::program]
impl {{ program-name }}Program {
impl {{ program-struct-name }} {
// Program's constructor
pub fn new() -> Self {
Self(())
}

pub fn {{ program-name-snake}}(&self) -> {{ program-name }}Service {
{{ program-name }}Service::new()
// Exposed service
pub fn {{ service-name-snake}}(&self) -> {{ service-struct-name }} {
{{ service-struct-name }}::new()
}
}

Expand Down
30 changes: 29 additions & 1 deletion templates/program/tests/gtest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
use sails_rs::{calls::*, gtest::calls::*};

use {{ client_crate_name }}::traits::*;

const ACTOR_ID: u64 = 42;

#[tokio::test]
async fn do_something_works() {
println!("WASM_BINARY length: {}", {{ program-name-snake }}::WASM_BINARY.len());
let remoting = GTestRemoting::new(ACTOR_ID.into());
remoting.system().init_logger();

// Submit program code into the system
let program_code_id = remoting.system().submit_code({{ crate_name }}::WASM_BINARY);

let program_factory = {{ client_crate_name }}::{{ service-name }}Factory::new(remoting.clone());

let program_id = program_factory
.new() // Call program's constructor (see src/lib.rs:27)
.send_recv(program_code_id, b"salt")
.await
.unwrap();

let mut service_client = {{ client_crate_name }}::{{ service-name }}::new(remoting.clone());

let result = service_client
.do_something() // Call service's method (see src/lib.rs:17)
.send_recv(program_id)
.await
.unwrap();

assert_eq!(result, "Hello from {{ service-name }}!".to_string());
}
12 changes: 10 additions & 2 deletions templates/set-vars.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ let client_project_name = if project_name.to_snake_case() == project_name {

variable::set("client-project-name", client_project_name);

variable::set("program-name", project_name.to_pascal_case());
variable::set("client_crate_name", client_project_name.to_snake_case());

variable::set("program-name-snake", project_name.to_snake_case());
variable::set("program-struct-name", project_name.to_pascal_case() + "Program");

variable::set("service-struct-name", project_name.to_pascal_case() + "Service");

variable::set("service-name", project_name.to_pascal_case());

variable::set("service-name-snake", project_name.to_snake_case());

variable::set("mocks-feature-name", "mocks");

// Set versions of used crates
variable::set("mockall-version", "0.12");
Expand Down

0 comments on commit dc5cf13

Please sign in to comment.