Skip to content

Commit

Permalink
Merge branch 'master' into vo/macros-extend-checks
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed May 16, 2024
2 parents e7f4fd6 + ab6a786 commit 8e2a897
Show file tree
Hide file tree
Showing 41 changed files with 274 additions and 225 deletions.
9 changes: 2 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ members = [

[workspace.dependencies]
anyhow = "1"
const-format = { package = "const_format", version = "0.2" }
convert-case = { package = "convert_case", version = "0.6" }
futures = { version = "0.3", default-features = false }
gear-core-errors = "1.4.1"
Expand Down
59 changes: 0 additions & 59 deletions client-gen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,62 +718,3 @@ impl<'ast> Visitor<'ast> for CallBuilderGenerator {
self.code.push_str(") -> Vec<u8>");
}
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_basic_works() {
let idl = r"
type MyParam = struct {
f1: u32,
f2: vec str,
f3: opt struct { u8, u32 },
};
type MyParam2 = enum {
Variant1,
Variant2: u32,
Variant3: struct { u32 },
Variant4: struct { u8, u32 },
Variant5: struct { f1: str, f2: vec u8 },
};
service {
DoThis: (p1: u32, p2: MyParam) -> u16;
DoThat: (p1: struct { u8, u32 }) -> u8;
};
";

let program = sails_idl_parser::ast::parse_idl(idl).expect("parse IDL");

insta::assert_snapshot!(generate(program, "Basic").unwrap());
}

#[test]
fn test_multiple_services() {
let idl = r"
service {
DoThis: (p1: u32, p2: MyParam) -> u16;
DoThat: (p1: struct { u8, u32 }) -> u8;
};
service Named {
query That: (p1: u32) -> str;
};
";

let program = sails_idl_parser::ast::parse_idl(idl).expect("parse IDL");

insta::assert_snapshot!(generate(program, "Multiple").unwrap());
}

#[test]
fn test_rmrk_works() {
let idl = include_str!("../../examples/rmrk/catalog/wasm/rmrk-catalog.idl");

let program = sails_idl_parser::ast::parse_idl(idl).expect("parse IDL");

insta::assert_snapshot!(generate(program, "RmrkCatalog").unwrap());
}
}
51 changes: 1 addition & 50 deletions client-gen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod generator;
pub mod generator;

use anyhow::{Context, Result};
use convert_case::{Case, Casing};
Expand Down Expand Up @@ -32,52 +32,3 @@ pub fn generate_client_from_idl(

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn full() {
const IDL: &str = r#"
type ThisThatSvcAppTupleStruct = struct {
bool,
};
type ThisThatSvcAppDoThatParam = struct {
p1: u32,
p2: str,
p3: ThisThatSvcAppManyVariants,
};
type ThisThatSvcAppManyVariants = enum {
One,
Two: u32,
Three: opt u32,
Four: struct { a: u32, b: opt u16 },
Five: struct { str, u32 },
Six: struct { u32 },
};
type T = enum { One };
constructor {
New : (a: u32);
};
service {
DoThis : (p1: u32, p2: str, p3: struct { opt str, u8 }, p4: ThisThatSvcAppTupleStruct) -> struct { str, u32 };
DoThat : (param: ThisThatSvcAppDoThatParam) -> result (struct { str, u32 }, struct { str });
query This : (v1: vec u16) -> u32;
query That : (v1: null) -> result (str, str);
};
"#;
let program = sails_idl_parser::ast::parse_idl(IDL).expect("parse IDL");

let generated = generator::generate(program, "Service").unwrap();

dbg!(&generated);

insta::assert_snapshot!(generated);
}
}
93 changes: 93 additions & 0 deletions client-gen/tests/generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#[test]
fn full() {
const IDL: &str = r#"
type ThisThatSvcAppTupleStruct = struct {
bool,
};
type ThisThatSvcAppDoThatParam = struct {
p1: u32,
p2: str,
p3: ThisThatSvcAppManyVariants,
};
type ThisThatSvcAppManyVariants = enum {
One,
Two: u32,
Three: opt u32,
Four: struct { a: u32, b: opt u16 },
Five: struct { str, u32 },
Six: struct { u32 },
};
type T = enum { One };
constructor {
New : (a: u32);
};
service {
DoThis : (p1: u32, p2: str, p3: struct { opt str, u8 }, p4: ThisThatSvcAppTupleStruct) -> struct { str, u32 };
DoThat : (param: ThisThatSvcAppDoThatParam) -> result (struct { str, u32 }, struct { str });
query This : (v1: vec u16) -> u32;
query That : (v1: null) -> result (str, str);
};
"#;

insta::assert_snapshot!(gen(IDL, "Service"));
}

#[test]
fn test_basic_works() {
let idl = r"
type MyParam = struct {
f1: u32,
f2: vec str,
f3: opt struct { u8, u32 },
};
type MyParam2 = enum {
Variant1,
Variant2: u32,
Variant3: struct { u32 },
Variant4: struct { u8, u32 },
Variant5: struct { f1: str, f2: vec u8 },
};
service {
DoThis: (p1: u32, p2: MyParam) -> u16;
DoThat: (p1: struct { u8, u32 }) -> u8;
};
";

insta::assert_snapshot!(gen(idl, "Basic"));
}

#[test]
fn test_multiple_services() {
let idl = r"
service {
DoThis: (p1: u32, p2: MyParam) -> u16;
DoThat: (p1: struct { u8, u32 }) -> u8;
};
service Named {
query That: (p1: u32) -> str;
};
";

insta::assert_snapshot!(gen(idl, "Multiple"));
}

#[test]
fn test_rmrk_works() {
let idl = include_str!("../../examples/rmrk/catalog/wasm/rmrk-catalog.idl");

insta::assert_snapshot!(gen(idl, "RmrkCatalog"));
}

fn gen(program: &str, service_name: &str) -> String {
let program = sails_idl_parser::ast::parse_idl(program).expect("parse IDL");

sails_client_gen::generator::generate(program, service_name).expect("generate client")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: client-gen/src/generator.rs
expression: "generate(program, \"Basic\").unwrap()"
source: client-gen/tests/generator.rs
expression: "gen(idl, \"Basic\")"
---
// Code generated by sails-client-gen. DO NOT EDIT.
use core::marker::PhantomData;
Expand Down Expand Up @@ -96,4 +96,3 @@ pub mod traits {
fn do_that(&mut self, p1: (u8, u32)) -> impl Call<TCallArgs, u8>;
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: client-gen/src/lib.rs
expression: generated
source: client-gen/tests/generator.rs
expression: "gen(IDL, \"Service\")"
---
// Code generated by sails-client-gen. DO NOT EDIT.
use core::marker::PhantomData;
Expand Down Expand Up @@ -175,4 +175,3 @@ pub mod traits {
fn that(&self, v1: ()) -> impl Call<TCallArgs, Result<String, String>>;
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: client-gen/src/generator.rs
expression: "generate(program, \"Multiple\").unwrap()"
source: client-gen/tests/generator.rs
expression: "gen(idl, \"Multiple\")"
---
// Code generated by sails-client-gen. DO NOT EDIT.
use core::marker::PhantomData;
Expand Down Expand Up @@ -134,4 +134,3 @@ pub mod traits {
fn that(&self, p1: u32) -> impl Call<TCallArgs, String>;
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: client-gen/src/generator.rs
expression: "generate(program, \"RmrkCatalog\").unwrap()"
source: client-gen/tests/generator.rs
expression: "gen(idl, \"RmrkCatalog\")"
---
// Code generated by sails-client-gen. DO NOT EDIT.
use core::marker::PhantomData;
Expand Down Expand Up @@ -305,4 +305,3 @@ pub mod traits {
fn part(&self, part_id: u32) -> impl Call<TCallArgs, Option<Part>>;
}
}

1 change: 0 additions & 1 deletion examples/puppeteer/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"
gstd = { workspace = true, features = ["debug"] }
parity-scale-codec = { workspace = true, features = ["derive"] }
sails-rtl.workspace = true
sails-macros.workspace = true
scale-info = { workspace = true, features = ["derive"] }

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/puppeteer/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ pub mod puppet;
use core::marker::PhantomData;
use gstd::prelude::*;
use puppet::traits::ThisThatSvc;
use sails_macros::gservice;
use sails_rtl::{
calls::{Call, Remoting},
gstd::gservice,
ActorId,
};

Expand Down
1 change: 0 additions & 1 deletion examples/rmrk/catalog/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ edition = "2021"

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
sails-macros.workspace = true
sails-rtl.workspace = true
scale-info = { workspace = true, features = ["derive"] }
3 changes: 1 addition & 2 deletions examples/rmrk/catalog/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_std]

use sails_macros::{gprogram, groute};
use sails_rtl::gstd::GStdExecContext;
use sails_rtl::gstd::{gprogram, groute, GStdExecContext};
use services::Catalog;

// Exposed publicly because of tests which use generated data
Expand Down
3 changes: 1 addition & 2 deletions examples/rmrk/catalog/app/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use errors::Error;
use parts::{CollectionId, Part, PartId, SlotPart};
use sails_macros::gservice;
use sails_rtl::{
collections::{BTreeMap, BTreeSet},
gstd::ExecContext,
gstd::{gservice, ExecContext},
prelude::*,
Result as RtlResult,
};
Expand Down
1 change: 0 additions & 1 deletion examples/rmrk/resource/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
gstd.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
sails-macros.workspace = true
sails-rtl.workspace = true
scale-info = { workspace = true, features = ["derive"] }

Expand Down
3 changes: 1 addition & 2 deletions examples/rmrk/resource/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![no_std]

use sails_macros::{gprogram, groute};
use sails_rtl::gstd::{
calls::{GStdArgs, GStdRemoting},
events::GStdEventTrigger,
GStdExecContext,
gprogram, groute, GStdExecContext,
};
use services::{ResourceStorage, ResourceStorageEvent};

Expand Down
Loading

0 comments on commit 8e2a897

Please sign in to comment.