Skip to content

Commit

Permalink
Add support for the dot (.) operator for field access.
Browse files Browse the repository at this point in the history
Idea was suggested by @rich-murphey.

Closes #1.
  • Loading branch information
danielhenrymantilla committed Nov 3, 2019
1 parent 2de1a3d commit 18c8629
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 86 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fstrings"
version = "0.1.4"
version = "0.2.0"
authors = ["Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>"]
edition = "2018"

Expand All @@ -16,15 +16,16 @@ keywords = ["python", "format", "human", "no_std", "interpolation"]
categories = ["rust-patterns", ]

[dependencies]
proc-macro-hack = "0.5.8"
proc-macro-hack = "0.5.11"

[dependencies.proc-macro]
version = "0.1.4"
version = "0.2.0"
package = "fstrings-proc-macro"
path = "src/proc_macro"

[features]
default = []

nightly = []
verbose-expansions = ["proc-macro/verbose-expansions", ]

Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,33 @@ fn main ()
String::from("Hello, World!"),
);

// advanced_cases
// ## Advanced cases:
{
// it remains compatible with classic formatting parameters
// It remains compatible with classic formatting parameters
assert_eq!(
f!("{hi}, {name}!", hi = "Hello"),
"Hello, World!",
);

// you can override / shadow the named arguments
// You can override / shadow the named arguments
assert_eq!(
f!("Hello, {name}!", name = "Earth"),
"Hello, Earth!",
);

// You can use field access (but no method calls!)
let foo = Foo { name }; /* where */ struct Foo<T> { name: T }
assert_eq!(
f!("Hello, {foo.name}!"),
"Hello, World!",
);

// This also works with tuple indexing.
let ft_and_name = (42, name);
assert_eq!(
f!("Hello, {ft_and_name.1}!"),
"Hello, World!",
);
}
}
```
20 changes: 17 additions & 3 deletions examples/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,32 @@ fn main ()
String::from("Hello, World!"),
);

// advanced_cases
// ## Advanced cases:
{
// it remains compatible with classic formatting parameters
// It remains compatible with classic formatting parameters
assert_eq!(
f!("{hi}, {name}!", hi = "Hello"),
"Hello, World!",
);

// you can override / shadow the named arguments
// You can override / shadow the named arguments
assert_eq!(
f!("Hello, {name}!", name = "Earth"),
"Hello, Earth!",
);

// You can use field access (but no method calls!)
let foo = Foo { name }; /* where */ struct Foo<T> { name: T }
assert_eq!(
f!("Hello, {foo.name}!"),
"Hello, World!",
);

// This also works with tuple indexing.
let ft_and_name = (42, name);
assert_eq!(
f!("Hello, {ft_and_name.1}!"),
"Hello, World!",
);
}
}
14 changes: 9 additions & 5 deletions src/proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ path = "mod.rs"

[package]
name = "fstrings-proc-macro"
version = "0.1.4"
version = "0.2.0"
authors = ["Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>"]
edition = "2018"

description = "Python3 fstring interpolation in Rust"
license = "MIT"

documentation = "https://docs.rs/fstrings"
repository = "https://github.com/danielhenrymantilla/fstrings-rs"
homepage = "https://crates.io/crates/fstrings"

[dependencies]
proc-macro2 = "0.4.30"
proc-macro-hack = "0.5.8"
proc-quote = "0.2.2"
syn = { version = "0.15.42", features = ["full", ]}
proc-macro2 = "1.0.*"
proc-macro-hack = "0.5.11"
quote = "1.0.*"
syn = { version = "1.0.*", features = ["full"]}

[features]
verbose-expansions = ["syn/extra-traits", ]
4 changes: 2 additions & 2 deletions src/proc_macro/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ macro_rules! debug_input {($expr:expr) => ($expr)}
#[cfg(feature = "verbose-expansions")]
macro_rules! debug_input {($expr:expr) => (
match $expr { expr => {
eprintln!("{} ! ( {} )", FUNCTION_NAME, expr);
eprintln!("-------------------\n{} ! ( {} )", FUNCTION_NAME, expr);
expr
}}
)}
Expand All @@ -15,7 +15,7 @@ macro_rules! debug_output {($expr:expr) => ($expr)}
#[cfg(feature = "verbose-expansions")]
macro_rules! debug_output {($expr:expr) => (
match $expr { expr => {
eprintln!("=>\n{}", expr);
eprintln!("=>\n{}\n-------------------\n", expr);
expr
}}
)}
Loading

0 comments on commit 18c8629

Please sign in to comment.