Skip to content

Commit

Permalink
Add recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodb committed Dec 7, 2024
1 parent 8ed7d47 commit 0c68eb3
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 5 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ on:
pre_build_script:
required: false
type: string
post_build_script:
required: false
type: string

env:
CARGO_TERM_COLOR: always
Expand Down Expand Up @@ -83,3 +80,9 @@ jobs:
name: Compile and run tests
run: cargo test ${{ inputs.rust_features }} --verbose

- id: recipes
name: Compile and run recipes
run: |
cd recipes
cargo build ${{ inputs.rust_features }} --verbose
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ members = [
]
exclude = [
"codegen/quirky_binder_codegen_wasm",
"recipes",
]
11 changes: 11 additions & 0 deletions codegen/quirky_binder_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ impl ModuleCode {
}
}

pub fn parse_and_generate_file_module(
file: &str,
quirky_binder_crate: &Ident,
error_emitter: &mut dyn ErrorEmitter,
) -> Result<TokenStream, CodegenError> {
let path =
Path::new(&std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR")).join(file);
let src = std::fs::read_to_string(path).map_err(|err| CodegenError::Error(err.to_string()))?;
parse_and_generate_module(&src, Some(file), quirky_binder_crate, error_emitter)
}

pub fn parse_and_generate_glob_modules(
src: &str,
pattern: &str,
Expand Down
29 changes: 27 additions & 2 deletions codegen/quirky_binder_codegen_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use annotate_snippets::display_list::DisplayList;
use proc_macro2::Ident;
use proc_macro_error::{abort_if_dirty, emit_error, proc_macro_error};
use quirky_binder_codegen::{
parse_and_generate_glob_modules, parse_and_generate_module, CodegenError, ErrorEmitter,
parse_and_generate_file_module, parse_and_generate_glob_modules, parse_and_generate_module,
CodegenError, ErrorEmitter,
};
use quirky_binder_lang::snippet::snippet_for_input_and_part;
use quote::format_ident;
Expand Down Expand Up @@ -73,6 +74,9 @@ enum Definition {
Inline {
qb: String,
},
Include {
file: String,
},
IncludeGlob {
src: String,
pattern: String,
Expand All @@ -81,6 +85,7 @@ enum Definition {
}

const INLINE_DEFINITION: &str = "inline";
const INCLUDE_DEFINITION: &str = "include";
const INCLUDE_GLOB_DEFINITION: &str = "include_glob";
const INCLUDE_GLOB_TEST_DEFINITION: &str = "include_glob_test";

Expand All @@ -98,6 +103,11 @@ fn parse_def(input: ParseStream) -> Result<(Definition, Ident), Error> {
parenthesized!(content in input);
Definition::parse_include_glob(&content, false)?
}
INCLUDE_DEFINITION => {
let content;
parenthesized!(content in input);
Definition::parse_include(&content)?
}
INCLUDE_GLOB_TEST_DEFINITION => {
let content;
parenthesized!(content in input);
Expand All @@ -108,7 +118,12 @@ fn parse_def(input: ParseStream) -> Result<(Definition, Ident), Error> {
def_type.span(),
format!(
"expected [{}]",
[INLINE_DEFINITION, INCLUDE_GLOB_DEFINITION].join(", ")
[
INLINE_DEFINITION,
INCLUDE_DEFINITION,
INCLUDE_GLOB_DEFINITION
]
.join(", ")
),
));
}
Expand All @@ -117,6 +132,11 @@ fn parse_def(input: ParseStream) -> Result<(Definition, Ident), Error> {
}

impl Definition {
fn parse_include(content: ParseStream) -> Result<Definition, Error> {
let file: LitStr = content.parse()?;
Ok(Definition::Include { file: file.value() })
}

fn parse_include_glob(content: ParseStream, test: bool) -> Result<Definition, Error> {
let src: LitStr = content.parse()?;
content.parse::<token::Comma>()?;
Expand Down Expand Up @@ -152,6 +172,11 @@ fn quirky_binder_1(
parse_and_generate_module(&qb, None, quirky_binder_crate, &mut error_emitter);
error_emitter.handle_codegen_result(result).into()
}
Definition::Include { file } => {
let result =
parse_and_generate_file_module(&file, quirky_binder_crate, &mut error_emitter);
error_emitter.handle_codegen_result(result).into()
}
Definition::IncludeGlob { src, pattern, test } => {
let result = parse_and_generate_glob_modules(
&src,
Expand Down
3 changes: 3 additions & 0 deletions recipes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/Cargo.lock
qb_monitor.csv
5 changes: 5 additions & 0 deletions recipes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
resolver = "2"
members = [
"hello_world",
]
28 changes: 28 additions & 0 deletions recipes/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
fallible-iterator = "0.3"
quirky_binder_support = { path = "../../quirky_binder_support" }
serde = "1"
static_assertions = "1"
truc_runtime = { git = "https://github.com/arnodb/truc.git" }

# quirky_binder_monitor
chrono = { version = "0.4", optional = true }
self-meter = { version = "0.6", optional = true }
tracking-allocator = { version = "0.4", optional = true }

[features]
default = []
quirky_binder_monitor = ["chrono", "self-meter", "tracking-allocator"]

[build-dependencies]
handlebars = "4"
quirky_binder = { path = "../../quirky_binder" }
quirky_binder_lang = { path = "../../quirky_binder_lang" }
serde = { version = "1", features = ["derive"] }
truc = { git = "https://github.com/arnodb/truc.git" }
25 changes: 25 additions & 0 deletions recipes/hello_world/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::path::Path;

use quirky_binder::{prelude::*, quirky_binder};
use truc::record::type_resolver::{StaticTypeResolver, TypeResolver};

fn main() {
quirky_binder!(include("main.qb"));

let type_resolver = {
let mut resolver = StaticTypeResolver::new();
resolver.add_std_types();
resolver
};

let graph = quirky_binder_main(GraphBuilder::new(
&type_resolver,
ChainCustomizer::default(),
))
.unwrap_or_else(|err| {
panic!("{}", err);
});

let out_dir = std::env::var("OUT_DIR").unwrap();
graph.generate(Path::new(&out_dir)).unwrap();
}
36 changes: 36 additions & 0 deletions recipes/hello_world/main.qb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use quirky_binder::{
filter::{
function::{
produce::function_produce,
terminate::function_terminate,
},
},
};

{
(
function_produce(
fields: [("hello", "String")],
body: r#"{
let record = new_record("world".to_string());
output.send(Some(record))?;
output.send(None)?;
Ok(())
}"#,
)
- function_terminate(
body: r#"
let record = input.next()?.unwrap();
println!("Hello {}!", record.hello());
assert!(input.next()?.is_none());
Ok(())
"#,
)
)
}

#(
name: "quirky_binder_monitor",
feature: "quirky_binder_monitor",
)
{ ( quirky_binder::filter::monitor::monitor() ) }
18 changes: 18 additions & 0 deletions recipes/hello_world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use quirky_binder_support::chain::configuration::ChainConfiguration;

#[macro_use]
extern crate static_assertions;

#[allow(dead_code)]
#[allow(clippy::borrowed_box)]
#[allow(clippy::module_inception)]
mod chain {
include!(concat!(env!("OUT_DIR"), "/chain.rs"));
}

quirky_binder_support::tracking_allocator_static!();

#[quirky_binder_support::tracking_allocator_main]
fn main() {
chain::main(ChainConfiguration::default()).unwrap();
}
10 changes: 10 additions & 0 deletions recipes/new_recipe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -eu

SHORT_NAME="$1"

mkdir "$SHORT_NAME"
cd "$SHORT_NAME"
cargo generate --init --name "${SHORT_NAME}" --path ../template

28 changes: 28 additions & 0 deletions recipes/template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "{{project-name}}"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
fallible-iterator = "0.3"
quirky_binder_support = { path = "../../quirky_binder_support" }
serde = "1"
static_assertions = "1"
truc_runtime = { git = "https://github.com/arnodb/truc.git" }

# quirky_binder_monitor
chrono = { version = "0.4", optional = true }
self-meter = { version = "0.6", optional = true }
tracking-allocator = { version = "0.4", optional = true }

[features]
default = []
quirky_binder_monitor = ["chrono", "self-meter", "tracking-allocator"]

[build-dependencies]
handlebars = "4"
quirky_binder = { path = "../../quirky_binder" }
quirky_binder_lang = { path = "../../quirky_binder_lang" }
serde = { version = "1", features = ["derive"] }
truc = { git = "https://github.com/arnodb/truc.git" }
25 changes: 25 additions & 0 deletions recipes/template/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::path::Path;

use quirky_binder::{prelude::*, quirky_binder};
use truc::record::type_resolver::{StaticTypeResolver, TypeResolver};

fn main() {
quirky_binder!(include("main.qb"));

let type_resolver = {
let mut resolver = StaticTypeResolver::new();
resolver.add_std_types();
resolver
};

let graph = quirky_binder_main(GraphBuilder::new(
&type_resolver,
ChainCustomizer::default(),
))
.unwrap_or_else(|err| {
panic!("{}", err);
});

let out_dir = std::env::var("OUT_DIR").unwrap();
graph.generate(Path::new(&out_dir)).unwrap();
}
36 changes: 36 additions & 0 deletions recipes/template/main.qb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use quirky_binder::{
filter::{
function::{
produce::function_produce,
terminate::function_terminate,
},
},
};

{
(
function_produce(
fields: [("hello", "String")],
body: r#"{
let record = new_record("world".to_string());
output.send(Some(record))?;
output.send(None)?;
Ok(())
}"#,
)
- function_terminate(
body: r#"
let record = input.next()?.unwrap();
println!("Hello {}!", record.hello());
assert!(input.next()?.is_none());
Ok(())
"#,
)
)
}

#(
name: "quirky_binder_monitor",
feature: "quirky_binder_monitor",
)
{ ( quirky_binder::filter::monitor::monitor() ) }
18 changes: 18 additions & 0 deletions recipes/template/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use quirky_binder_support::chain::configuration::ChainConfiguration;

#[macro_use]
extern crate static_assertions;

#[allow(dead_code)]
#[allow(clippy::borrowed_box)]
#[allow(clippy::module_inception)]
mod chain {
include!(concat!(env!("OUT_DIR"), "/chain.rs"));
}

quirky_binder_support::tracking_allocator_static!();

#[quirky_binder_support::tracking_allocator_main]
fn main() {
chain::main(ChainConfiguration::default()).unwrap();
}

0 comments on commit 0c68eb3

Please sign in to comment.