Skip to content

Improve tvf! macro for negative numerical values - #88

Draft
oschijns wants to merge 3 commits into
mainfrom
improve-tvf-macro
Draft

Improve tvf! macro for negative numerical values#88
oschijns wants to merge 3 commits into
mainfrom
improve-tvf-macro

Conversation

@oschijns

Copy link
Copy Markdown
Contributor

Allow defining the sign of a numerical value without wrapping into into extra parenthesis.
from

  let buffer = tvf!(SimpleStringTvf {
      10 => (-10),
      11 => (-10.25),
      12 => (-2) as Signed,
      13 => (+2.125) as Float,
  });

to

  let buffer = tvf!(SimpleStringTvf {
      10 => -10,
      11 => -10.25,
      12 => -2 as Signed,
      13 => +2.125 as Float,
  });

…extra parenthesis

Signed-off-by: Olivier Schyns <olivier.schyns@worldline.com>
@oschijns oschijns self-assigned this Aug 31, 2026
@oschijns
oschijns requested a review from reneca August 31, 2026 16:43
@reneca

reneca commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Some edge cases are not working with this solution. I think you should reconsider the negative sign parsing. It should be apply on the expression.
For me these should work (not the case right now):

let amount: i64 = 7;

let buffer = tvf!(SimpleStringTvf {
    1 => -amount as Signed,
});

assert_eq!(Ok(-7), buffer.get_signed(1));

Also for String conversion:

let buffer = tvf!(SimpleStringTvf {
    1 => -7 as String,
});

assert_eq!(
    Ok("-7"),
    buffer.get_string(1).map(|value| value.to_string()).as_deref(),
);

And maybe an error could be trigger if we try to negate an unsigned value:

let buffer = tvf!(SimpleStringTvf {
    1 => -7 as Unsigned,
    2 => -7u64,
});

assert_ne!(Ok(7), buffer.get_unsigned(1));
assert_ne!(Ok(7), buffer.get_unsigned(2));

@reneca reneca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You fix common things, but we still have an issue regarding expression.
For example: -(left - right) as Signed, with left = 10 and right = 3, stores 7 instead of -7. Can you also consider it please ?

Also what can we do about these ?

-7 as Unsigned // stores 7
-7u64          // stores 7
-7 as Byte     // stores 7

BTW you didn't sign your last commit.
And can you increase prose_macros version to 0.5.1 please.

// Check if the first element is a sign (+ or -)
let is_negative = if let Some(TokenTree::Punct(punct)) = tokens.peek() {
let negative = punct.as_char() == '-';
tokens.next(); // move to next token

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User can also put !false and it'll be consider as false. It should consume the token only if it's -.

Signed-off-by: Olivier Schyns <olivier.schyns@worldline.com>
@oschijns

oschijns commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Also what can we do about these ?

-7 as Unsigned // stores 7
-7u64          // stores 7
-7 as Byte     // stores 7

They should throw an error. I'll look into it.

Signed-off-by: Olivier Schyns <olivier.schyns@worldline.com>
@oschijns
oschijns marked this pull request as draft September 2, 2026 16:24
@oschijns

oschijns commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

I think I've fixed some of the issues. But I am not satisfied with the implementation.
value.rs and literal.rs are kind of doing the same thing so there should be a way to group them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants