Skip to content

Commit

Permalink
updates and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jewlexx committed Sep 8, 2023
1 parent 798285c commit 8b34439
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
8 changes: 7 additions & 1 deletion quork-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ pub fn time(
args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
time_fn::time_inner(args.into(), input.into()).into()
let args_str = args.to_string();
let fmt = match args_str.as_str() {
"ms" | "milliseconds" => time_fn::TimeFormat::Milliseconds,
"ns" | "nanoseconds" => time_fn::TimeFormat::Nanoseconds,
_ => time_fn::TimeFormat::Seconds,
};
time_fn::time_inner(&fmt, input.into()).into()
}

#[proc_macro]
Expand Down
15 changes: 4 additions & 11 deletions quork-proc/src/time_fn.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;

enum TimeFormat {
#[derive(Debug, Copy, Clone)]
pub enum TimeFormat {
Seconds,
Milliseconds,
Nanoseconds,
}

pub fn time_inner(attrs: TokenStream, item: TokenStream) -> TokenStream {
let time_format: u8 = match attrs.to_string().as_str() {
"s" | "seconds" | "" => TimeFormat::Seconds,
"ms" | "milliseconds" => TimeFormat::Milliseconds,
"ns" | "nanoseconds" => TimeFormat::Nanoseconds,
_ => {
abort!(attrs.span(), "attributes can only be s/ms/ns for seconds, milliseconds and nanoseconds respectively");
}
} as u8;
pub fn time_inner(format: &TimeFormat, item: TokenStream) -> TokenStream {
let time_format = *format as u8;

let input: syn::ItemFn = match syn::parse2(item.clone()) {
Ok(input) => input,
Expand Down
1 change: 0 additions & 1 deletion quork-proc/tests/strip.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use quork_proc::{lstrip_lines, rstrip_lines, strip_lines};


#[test]
fn test_multiline_both() {
let expected = "function foo() {
Expand Down
6 changes: 6 additions & 0 deletions quork-proc/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ fn test_time_maths() {
assert_eq!(2 + 2, 4);
}

#[test]
#[time(nanoseconds)]
fn test_time_nanoseconds() {
assert_eq!(2 + 2, 4);
}

#[test]
fn test_time_unsafe() {
#[time]
Expand Down

0 comments on commit 8b34439

Please sign in to comment.