From e4b19cba5d0be3d915af8b2554e5ba996b3f3f08 Mon Sep 17 00:00:00 2001 From: Thelie Date: Sun, 27 Mar 2022 15:20:14 +0200 Subject: [PATCH] Update 'README.md' Added example and more explaination of how the macros work. --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 714a1bb..09c9f71 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,56 @@ **S**td - **I**/o - **N**egotiator - **G**enerator ## What is sing meant to be? This crate is meant to create interfaces between traits in Rust libraries and the command line. -It does this by providing two macros: … +It does this by providing two macros: + +- `sing_add_trait`, which acts as an attribute to a trait implementation and gathers the method signatures that are used by `sing_loop!`. +- `sing_loop!`, which takes a trait object and serialization/deserialization functions as well as some optional parameters. It generates a loop parsing function calls coming from STDIN (or any other `BufRead` stream), evaluating them by calling the appropriate method on the object and returning them on STDOUT (or any other `Write` stream). + +## Example usage + +```rust +use serde::{Serialize, Deserialize}; +use sing::{sing_add_trait, sing_loop}; + +trait FruitTree { + fn shake(&self, shakes: u32) -> Vec; +} + +#[derive(Debug, Serialize, Deserialize)] +struct Fruit { + fruit_type: String, +} + +struct BananaTree; + +#[sing_add_trait()] +impl FruitTree for BananaTree { + fn shake(&self, shakes: u32) -> Vec{ + let mut out = vec!(); + + for _ in 0..shakes { + out.push(Fruit{fruit_type: String::from("Banana")}); + } + + out + } +} + +fn main() { + let tree = BananaTree {}; + + sing_loop!(tree, [different, ron::to_string, ron::from_str],); +} +``` ## Negotiator? Really?