2022-03-26 23:21:19 +01:00
# Sing
**S**td
2022-03-26 23:22:30 +01:00
2022-03-26 23:21:19 +01:00
**I**/o
2022-03-26 23:22:30 +01:00
2022-03-26 23:21:19 +01:00
**N**egotiator
2022-03-26 23:22:30 +01:00
2022-03-26 23:21:19 +01:00
**G**enerator
2022-03-27 17:19:47 +02:00
[![Crates.io ](https://img.shields.io/crates/v/sing_rs )](https://crates.io/crates/sing_rs)
[![License ](https://img.shields.io/crates/l/sing_rs )](https://www.gnu.org/licenses/agpl-3.0.html)
2022-03-27 18:28:30 +02:00
## What is sing?
2022-03-26 23:21:19 +01:00
This crate is meant to create interfaces between traits in Rust libraries and the command line.
2022-03-27 15:20:14 +02:00
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).
2022-03-27 15:20:54 +02:00
## Usage example
2022-03-27 15:20:14 +02:00
```rust
use serde::{Serialize, Deserialize};
2022-03-27 17:58:24 +02:00
use sing_rs::{sing_add_trait, sing_loop};
2022-03-27 15:20:14 +02:00
trait FruitTree {
fn shake(& self, shakes: u32) -> Vec< Fruit > ;
}
#[derive(Debug, Serialize, Deserialize)]
struct Fruit {
fruit_type: String,
}
struct BananaTree;
#[sing_add_trait()]
impl FruitTree for BananaTree {
fn shake(& self, shakes: u32) -> Vec< Fruit > {
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],);
}
```
2022-03-26 23:21:19 +01:00
## Negotiator? Really?
2022-04-05 21:22:38 +02:00
Yes, it's somewhat far-fetched, but I really wanted to be able to sing and [clap ](https://github.com/clap-rs/clap ) in my rusty command-line projects.
2022-03-26 23:21:19 +01:00
## Why the name?
2022-03-27 17:58:24 +02:00
See above.