-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/target | ||
target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use arrayvec::ArrayString; | ||
use to_arraystring_macros::aformat; | ||
|
||
extern crate self as to_arraystring; | ||
|
||
fn test_aformat() { | ||
let name = ArrayString::<5>::from("Daisy").unwrap(); | ||
let age = 18_u8; | ||
|
||
assert_eq!( | ||
&aformat!("Hello {}, you are {} years old", age), | ||
"Hello Daisy, you are 18 years old" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "to-arraystring-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = { version = "2.0.61", features = ["parsing", "proc-macro"], default-features = false } | ||
proc-macro2 = "1.0.82" | ||
quote = "1.0.36" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::error::Error; | ||
|
||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote, ToTokens as _}; | ||
use syn::{punctuated::Punctuated, Ident, Result, Token}; | ||
|
||
struct Arguments { | ||
format_str: String, | ||
format_str_span: proc_macro2::Span, | ||
arguments: Punctuated<Ident, Token![,]>, | ||
} | ||
|
||
impl syn::parse::Parse for Arguments { | ||
fn parse(input: syn::parse::ParseStream) -> Result<Self> { | ||
let format_str = input.parse::<syn::LitStr>()?; | ||
input.parse::<Option<Token![,]>>()?; | ||
|
||
Ok(Self { | ||
format_str: format_str.value(), | ||
format_str_span: format_str.span(), | ||
arguments: input.parse_terminated(Ident::parse, Token![,])?, | ||
}) | ||
} | ||
} | ||
|
||
fn aformat_impl(tokens: proc_macro::TokenStream) -> Result<TokenStream> { | ||
let Arguments { | ||
arguments, | ||
format_str, | ||
format_str_span, | ||
} = syn::parse(tokens)?; | ||
|
||
if arguments.is_empty() { | ||
let str_length = format_str.len(); | ||
return Ok(quote!(::arrayvec::ArrayString::<#str_length>::from(#format_str).unwrap())); | ||
} | ||
|
||
let arg_type_idents = (0..arguments.len()).map(|i| format_ident!("ARG_TYPE_{i}")); | ||
let arg_type_idents_clone = arg_type_idents.clone(); | ||
let arguments_iter = arguments.into_iter(); | ||
let arguments_iter_clone = arguments_iter.clone(); | ||
|
||
let split_fmt_str = format_str.split("{}"); | ||
|
||
let out = quote!( | ||
{ | ||
use to_arraystring::{ArrayString, ToArrayString}; | ||
|
||
#( | ||
type #arg_type_idents = impl ToArrayString; | ||
let _: #arg_type_idents = #arguments_iter; | ||
)* | ||
|
||
const OUT_CAP: usize = #( | ||
<#arg_type_idents_clone as ToArrayString>::MAX_LENGTH | ||
)+*; | ||
|
||
let mut out_buffer = ArrayString::<OUT_CAP>::new(); | ||
#( | ||
out_buffer.push_str(#split_fmt_str); | ||
out_buffer.push_str(&#arguments_iter_clone.to_arraystring()); | ||
);* | ||
|
||
out_buffer | ||
} | ||
); | ||
|
||
Ok(out) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn aformat(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
match aformat_impl(tokens) { | ||
Ok(tokens) => tokens.into(), | ||
Err(err) => err.into_compile_error().into(), | ||
} | ||
} |