Crate mini
Console program226 lines of .st255 lines of generated Rust
pear=3 apple=1 fig=2 seen pear seen apple name pear name apple name fig 3 rows, 1 kept, fig 2 (true) kiwi found: false, painted Green, max 100 packed cba; error: empty path
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
standard = "2021"
}
}
lib_provider {
Rust_lang = {
source = "StructOp/rust"
version = "@latest"
}
Mini = {
source = "Crate/minicrate"
version = "@latest"
}
}
}main.st
# The small test crate through the provider structop bind writes: a handle in a value,
# Result and Option returns, a closure, a then-chain over a borrowing type and an iterator.
ability "value" "db" {
type = Db
}
ability "value" "rows" {
type = Size
}
data "value" "rows" {
of = ability.value.rows
}
ability "value" "score" {
type = Int
}
data "value" "score" {
of = ability.value.score
}
ability "value" "found" {
type = Bool
}
data "value" "found" {
of = ability.value.found
}
ability "value" "name" {
type = String
}
data "value" "name" {
of = ability.value.name
}
ability "value" "n" {
type = Int
}
data "value" "n" {
of = ability.value.n
}
ability "value" "kept" {
type = Size
}
data "value" "kept" {
of = ability.value.kept
}
ability "value" "err" {
type = String
}
data "value" "err" {
of = ability.value.err
}
ability "value" "color" {
type = Color
}
data "value" "color" {
of = ability.value.color
}
ability "value" "input" {
type = Bytes
size = 16
}
ability "value" "packed" {
type = Bytes
size = 16
}
data "value" "packed" {
of = ability.value.packed
}
ability "db_open" "Open" {
path = "notes.db"
target = ability.value.db
}
ability "db_insert" "Pear" {
db = ability.value.db
name = "pear"
n = 3
}
ability "db_insert" "Apple" {
db = ability.value.db
name = "apple"
n = 1
}
ability "db_insert" "Fig" {
db = ability.value.db
name = "fig"
n = 2
target = ability.value.rows
}
ability "db_prepare_query_map" "List" {
db = ability.value.db
sql = "SELECT name, n FROM fruit"
f = [ability.row_name.Name, ability.row_n.N, ability.print.Row]
}
data "db_prepare_query_map_f" "R" {
of = ability.db_prepare_query_map.List
}
ability "row_name" "Name" {
row = data.db_prepare_query_map_f.R.row
target = ability.value.name
}
ability "row_n" "N" {
row = data.db_prepare_query_map_f.R.row
target = ability.value.n
}
ability "print" "Row" {
message = "${data.value.name.current}=${data.value.n.current}"
}
ability "db_each" "Count" {
db = ability.value.db
f = [ability.print.Seen]
f_result = data.db_each_f.E.arg2 > 1
target = ability.value.kept
}
data "db_each_f" "E" {
of = ability.db_each.Count
}
ability "print" "Seen" {
message = "seen ${data.db_each_f.E.arg1}"
}
ability "db_names" "Names" {
db = ability.value.db
each = [ability.print.Name]
}
data "db_names_each" "I" {
of = ability.db_names.Names
}
ability "print" "Name" {
message = "name ${data.db_names_each.I.item}"
}
ability "db_find" "Fig" {
db = ability.value.db
name = "fig"
target = ability.value.score
found = ability.value.found
}
ability "print" "Fig" {
message = "${data.value.rows.current} rows, ${data.value.kept.current} kept, fig ${data.value.score.current} (${data.value.found.current})"
}
ability "db_find" "Kiwi" {
db = ability.value.db
name = "kiwi"
found = ability.value.found
}
ability "db_paint" "Paint" {
db = ability.value.db
c = Color.Red
target = ability.value.color
}
ability "print" "Kiwi" {
message = "kiwi found: ${data.value.found.current}, painted ${data.value.color.current}, max ${const.MAX_ROWS}"
}
ability "set" "Fill" {
target = ability.value.input
value = bytes("abc")
}
ability "db_pack" "Pack" {
db = ability.value.db
data = ability.value.input
target = ability.value.packed
}
ability "db_open" "Bad" {
path = ""
error = ability.value.err
}
ability "print" "End" {
message = "packed ${text(data.value.packed.current)}; error: ${data.value.err.current}"
}Input, plan and generated Rust#
tests/bind/minicrate/minicrate.bind.st · hints for structop bind
tests/bind/minicrate/minicrate.bind.st · hints for structop bind
bind "minicrate" {
crate = "minicrate"
version = "0.1.0"
path = "."
}
struct "Encoder" {
W = Bytes
}
function "Db::prepare" {
then = ["Stmt::query_map"]
variant "db_prepare_query_map" {
T = Int
}
}
function "Db::get" {
variant "db_get_int" {
T = Int
}
}
function "Encoder::write_all" {}structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.value.db
+ ability.value.rows
+ ability.value.score
+ ability.value.found
+ ability.value.name
+ ability.value.n
+ ability.value.kept
+ ability.value.err
+ ability.value.color
+ ability.value.input
+ ability.value.packed
+ ability.db_open.Open
+ ability.db_insert.Pear
+ ability.db_insert.Apple
+ ability.db_insert.Fig
+ ability.db_prepare_query_map.List
+ ability.row_name.Name
+ ability.row_n.N
+ ability.print.Row
+ ability.db_each.Count
+ ability.print.Seen
+ ability.db_names.Names
+ ability.print.Name
+ ability.db_find.Fig
+ ability.print.Fig
+ ability.db_find.Kiwi
+ ability.db_paint.Paint
+ ability.print.Kiwi
+ ability.set.Fill
+ ability.db_pack.Pack
+ ability.db_open.Bad
+ ability.print.End
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.rs
To inspect this generated file, select Rust in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
structop bind tests/bind/minicrate/minicrate.bind.st # from the repository root
cd crate-mini # a folder holding the .st files above
structop init
structop plan
structop apply