Improve tvf! macro for negative numerical values - #88
Conversation
…extra parenthesis Signed-off-by: Olivier Schyns <olivier.schyns@worldline.com>
|
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. 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)); |
There was a problem hiding this comment.
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 7BTW 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 |
There was a problem hiding this comment.
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>
8271a3f to
9384d63
Compare
They should throw an error. I'll look into it. |
Signed-off-by: Olivier Schyns <olivier.schyns@worldline.com>
|
I think I've fixed some of the issues. But I am not satisfied with the implementation. |
Allow defining the sign of a numerical value without wrapping into into extra parenthesis.
from
to