Skip to content

Commit

Permalink
Add CSV recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodb committed Dec 8, 2024
1 parent d0d6c03 commit bee35ab
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contrib/quirky_binder_csv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license-file = "../LICENSE"
description = "Quirky Binder - CSV"
documentation = "https://docs.rs/quirky_binder_csv"
repository = "https://github.com/arnodb/quirky_binder"
readme = "../README.md"
readme = "README.md"

[dependencies]
getset = "0.1"
Expand Down
42 changes: 42 additions & 0 deletions contrib/quirky_binder_csv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Quirky Binder - CSV

`quirky_binder_csv` helps reading and writing CSV files.

## Setup

* add `quirky_binder_csv` to `[build-dependencies]`
* add `csv = "1"` to `[dependencies]`

## Read

```
use quirky_binder_csv::read_csv;
{
(
read_csv(
input_file: "input/hello_universe.csv",
fields: [("hello", "String"), ("universe", "usize")],
has_headers: true,
)
- ...
)
}
```

## Write

```
use quirky_binder_csv::write_csv;
{
(
...
- write_csv(
output_file: "output/hello_universe.csv",
has_headers: true,
)
)
}
```

1 change: 1 addition & 0 deletions recipes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
resolver = "2"
members = [
"csv",
"hello_world",
]
1 change: 1 addition & 0 deletions recipes/csv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/output
30 changes: 30 additions & 0 deletions recipes/csv/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "csv"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
csv = "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_csv = { path = "../../contrib/quirky_binder_csv" }
quirky_binder_lang = { path = "../../quirky_binder_lang" }
serde = { version = "1", features = ["derive"] }
truc = { git = "https://github.com/arnodb/truc.git" }
13 changes: 13 additions & 0 deletions recipes/csv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Quirky Binder Recipe - csv

## Description

Quirky Binder allows reading and writing CSV files with the help of `quirky_binder_csv` crate.

## Setup

In addition to mandatory quirky binder dependencies:

* add `quirky_binder_csv` to `[build-dependencies]`
* add `csv` to `[dependencies]` (see `quirky_binder_csv` documentation)

25 changes: 25 additions & 0 deletions recipes/csv/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();
}
2 changes: 2 additions & 0 deletions recipes/csv/input/hello_universe.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello,universe
world,42
21 changes: 21 additions & 0 deletions recipes/csv/main.qb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use quirky_binder_csv::{read_csv, write_csv};

{
(
read_csv(
input_file: "input/hello_universe.csv",
fields: [("hello", "String"), ("universe", "usize")],
has_headers: true,
)
- write_csv(
output_file: "output/hello_universe.csv",
has_headers: true,
)
)
}

#(
name: "quirky_binder_monitor",
feature: "quirky_binder_monitor",
)
{ ( quirky_binder::filter::monitor::monitor() ) }
18 changes: 18 additions & 0 deletions recipes/csv/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 bee35ab

Please sign in to comment.