Crates.io | ranting |
lib.rs | ranting |
version | 0.2.1 |
source | src |
created_at | 2022-12-20 16:24:40.055708 |
updated_at | 2023-02-22 23:19:31.658355 |
description | Linguistic formatting placeholder extensions for rust |
homepage | https://github.com/RoelKluin/ranting.rs |
repository | https://github.com/RoelKluin/ranting.rs |
max_upload_size | |
id | 742451 |
size | 74,705 |
This library provides Ranting
, a trait for inflection within say!()
litteral string placeholders.
[dependencies]
ranting = "0.2"
say!()
macro produces a String similar to format!()
, but with placeholder markers a pronouns can be
received. A verb alongside, always specified in plural, inflects accordingly.use ranting::*;
use ranting_derive::*;
fn say_this(who: Noun, title: &Noun) -> String {
say!("{=who do} say {`who title are} {who}.")
}
fn main() {
let title = Noun::new("name", "it");
assert_eq!(
say_this(Noun::new("Jane", "I"), &title),
"I do say my name is Jane.".to_string()
);
assert_eq!(
say_this(Noun::new("Tarzan", "he"), &title),
"He does say his name is Tarzan.".to_string()
);
}
Noun
is a struct with the Ranting
trait. You can use #[derive(Ranting)]
on a struct or enum for similar
behavior. To specify the subject use I .. they, thou or ye.
A placeholder to display a Ranting variable has a structure like:
,
and ^
lower- and uppercase are enforced, but a placeholder at sentence start is uppercase by default.
Also an article or verb with an uppercase causess an uppercase for the first character.fn state<T: Ranting>(who: T, liberty: &str) -> String {
say!("{haven't =who} a {liberty} to say {a who's} land is {~who}?")
}
#[derive_ranting]
#[ranting(subject = "he")]
struct Earl {}
#[derive_ranting]
#[ranting(subject = "they")]
struct Farmers {}
fn main() {
assert_eq!(
state(Earl {}, "right"),
"Hasn't he a right to say an earl's land is his?".to_string()
);
assert_eq!(
state(Farmers {}, "right"),
"Haven't they a right to say some farmers' land is theirs?".to_string()
);
}
An article, possesive 's
or verbs before the noun are also adapted. Normal placeholders just follow their Display or
Debug traits within say!()
.
A given Ranting Enum or Struct can also be inflected to plural or singular. To force plurality use +
, for a singular
use -
. If prependeded by $var
or #var
, plurality of the noun is adapted to the numeric variable var. Which is
displayed, unless prepended with a '?'. The number is converted to a word for #var
. Other words within the
placeholder are adapted accordingly as well.
A Noun or pronoun is displayed dependent on its leading character or string marker.
?
- subject in inflection, but neither variable nor its space is displayed.=
- subject@
- object`
- possesive~
- adjective*
- display the name (as is the default) but also mark this word as the Ranting element in the placeholder.
"A {*can can} contain water."
(removed the mutname variant)If a Noun or numeric plurality has a leading question mark, it is hidden but its inferred inflection does apply.
An 'article' can be one of a
, an
, some
, the
, those
or these
. These and those are converted to
this and that if the pronoun is singular. A question mark indicates its display dependends (see no_article).
ack!()
and nay!()
provide an Ok() / Err() return with a say!()
formatted string included. Intended for allow or
deny ranting responses. Not for error handling, because true errors should be easy to search in code.
A struct can receive via attributes:
Positional argument and numeric references are supported, but named arguments or empty arguments are not, currecntly.
fn main() {
let thing = Noun::new("thing", "it");
assert_eq!(say!("this is {=thing}."), "this is it.".to_string());
assert_eq!(say!("this is {=0}.", thing), "this is it.".to_string());
// won't work:
//assert_eq!("{}", say!("this is {=x}.", x = thing), "this is it.".to_string());
//assert_eq!("{}", say!("this is {=}.", thing), "this is it.".to_string());
}