enum_ordinalize_derive/
int_wrapper.rs1use proc_macro2::{Literal, TokenStream};
2use quote::{quote, ToTokens, TokenStreamExt};
3use syn::Expr;
4
5use crate::int128::Int128;
6
7pub(crate) enum IntWrapper {
8 Integer(Int128),
9 Constant(Expr, usize),
10}
11
12impl From<Int128> for IntWrapper {
13 #[inline]
14 fn from(v: Int128) -> IntWrapper {
15 Self::Integer(v)
16 }
17}
18
19impl From<i128> for IntWrapper {
20 #[inline]
21 fn from(v: i128) -> IntWrapper {
22 Self::Integer(Int128::from(v))
23 }
24}
25
26impl From<(&Expr, usize)> for IntWrapper {
27 #[inline]
28 fn from((expr, counter): (&Expr, usize)) -> IntWrapper {
29 Self::Constant(expr.clone(), counter)
30 }
31}
32
33impl ToTokens for IntWrapper {
34 #[inline]
35 fn to_tokens(&self, tokens: &mut TokenStream) {
36 match self {
37 Self::Integer(v) => {
38 let lit = match v {
39 Int128::Signed(i) => Literal::i128_unsuffixed(*i),
40 Int128::Unsigned(u) => Literal::u128_unsuffixed(*u),
41 };
42
43 tokens.append(lit);
44 },
45 Self::Constant(expr, counter) => {
46 let counter = *counter;
47
48 if counter > 0 {
49 tokens.extend(quote!(#expr +));
50 tokens.append(Literal::usize_unsuffixed(counter));
51 } else {
52 tokens.extend(quote!(#expr));
53 }
54 },
55 }
56 }
57}