diff --git a/rust/derive/src/params.rs b/rust/derive/src/params.rs index 575d80b..79487d3 100644 --- a/rust/derive/src/params.rs +++ b/rust/derive/src/params.rs @@ -232,7 +232,15 @@ impl TryFrom for VariantAttr { impl VariantAttr { pub fn variant_name(&self, name: &Ident) -> LitStr { match self.rename { - None => LitStr::new(&name.to_string().to_lower_camel_case(), name.span()), + None => { + let mut camel_case = name.to_string().to_lower_camel_case(); + if let Some(first) = camel_case.chars().next() { + if !first.is_alphabetic() { + camel_case = "_".to_owned() + &camel_case; + } + } + LitStr::new(&camel_case, name.span()) + } Some(ref name) => name.clone(), } } diff --git a/rust/src/stl.rs b/rust/src/stl.rs index e6e695d..a6eee14 100644 --- a/rust/src/stl.rs +++ b/rust/src/stl.rs @@ -19,14 +19,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![allow(non_camel_case_types)] +#![allow(non_camel_case_types, unused_imports)] use std::io; use std::marker::PhantomData; use amplify::ascii::AsciiChar; use amplify::confinement::Confined; -use amplify::num::{u4, u5}; +use amplify::num::{u2, u3, u4, u5, u6, u7}; use crate::{ DecodeError, StrictDecode, StrictDumb, StrictEncode, StrictEnum, StrictSum, StrictType, @@ -134,149 +134,63 @@ impl StrictDecode for bool { } } -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_STD, tags = repr, into_u8, try_from_u8, crate = crate)] -#[repr(u8)] -pub enum U4 { - #[default] - #[strict_type(rename = "u4_0")] - _0 = 0, - #[strict_type(rename = "u4_1")] - _1, - #[strict_type(rename = "u4_2")] - _2, - #[strict_type(rename = "u4_3")] - _3, - #[strict_type(rename = "u4_4")] - _4, - #[strict_type(rename = "u4_5")] - _5, - #[strict_type(rename = "u4_6")] - _6, - #[strict_type(rename = "u4_7")] - _7, - #[strict_type(rename = "u4_8")] - _8, - #[strict_type(rename = "u4_9")] - _9, - #[strict_type(rename = "u4_10")] - _10, - #[strict_type(rename = "u4_11")] - _11, - #[strict_type(rename = "u4_12")] - _12, - #[strict_type(rename = "u4_13")] - _13, - #[strict_type(rename = "u4_14")] - _14, - #[strict_type(rename = "u4_15")] - _15, -} +macro_rules! impl_u { + ($ty:ident, $inner:ty, $( $no:ident )+) => { + #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)] + #[derive(StrictType, StrictEncode, StrictDecode)] + #[strict_type(lib = LIB_NAME_STD, tags = repr, into_u8, try_from_u8, crate = crate)] + #[repr(u8)] + pub enum $ty { + #[default] + $( $no ),+ + } -impl StrictType for u4 { - const STRICT_LIB_NAME: &'static str = LIB_NAME_STD; - fn strict_name() -> Option { Some(tn!("U4")) } -} -impl StrictEncode for u4 { - fn strict_encode(&self, writer: W) -> io::Result { - writer.write_enum::(U4::try_from(self.to_u8()).expect("broken u4 types guarantees")) - } -} -impl StrictDecode for u4 { - fn strict_decode(reader: &mut impl TypedRead) -> Result { - let v: U4 = reader.read_enum()?; - Ok(u4::with(v as u8)) + impl StrictType for $inner { + const STRICT_LIB_NAME: &'static str = LIB_NAME_STD; + fn strict_name() -> Option { Some(tn!(stringify!($ty))) } + } + impl StrictEncode for $inner { + fn strict_encode(&self, writer: W) -> io::Result { + writer.write_enum::<$ty>($ty::try_from(self.to_u8()) + .expect(concat!("broken", stringify!($inner), "type guarantees"))) + } + } + impl StrictDecode for $inner { + fn strict_decode(reader: &mut impl TypedRead) -> Result { + let v: $ty = reader.read_enum()?; + Ok(<$inner>::with(v as u8)) + } + } } } -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_STD, tags = repr, into_u8, try_from_u8, crate = crate)] -#[repr(u8)] -pub enum U5 { - #[default] - #[strict_type(rename = "u5_0")] - _0 = 0, - #[strict_type(rename = "u5_1")] - _1, - #[strict_type(rename = "u5_2")] - _2, - #[strict_type(rename = "u5_3")] - _3, - #[strict_type(rename = "u5_4")] - _4, - #[strict_type(rename = "u5_5")] - _5, - #[strict_type(rename = "u5_6")] - _6, - #[strict_type(rename = "u5_7")] - _7, - #[strict_type(rename = "u5_8")] - _8, - #[strict_type(rename = "u5_9")] - _9, - #[strict_type(rename = "u5_10")] - _10, - #[strict_type(rename = "u5_11")] - _11, - #[strict_type(rename = "u5_12")] - _12, - #[strict_type(rename = "u5_13")] - _13, - #[strict_type(rename = "u5_14")] - _14, - #[strict_type(rename = "u5_15")] - _15, - #[strict_type(rename = "u5_16")] - _16, - #[strict_type(rename = "u5_17")] - _17, - #[strict_type(rename = "u5_18")] - _18, - #[strict_type(rename = "u5_19")] - _19, - #[strict_type(rename = "u5_20")] - _20, - #[strict_type(rename = "u5_21")] - _21, - #[strict_type(rename = "u5_22")] - _22, - #[strict_type(rename = "u5_23")] - _23, - #[strict_type(rename = "u5_24")] - _24, - #[strict_type(rename = "u5_25")] - _25, - #[strict_type(rename = "u5_26")] - _26, - #[strict_type(rename = "u5_27")] - _27, - #[strict_type(rename = "u5_28")] - _28, - #[strict_type(rename = "u5_29")] - _29, - #[strict_type(rename = "u5_30")] - _30, - #[strict_type(rename = "u5_31")] - _31, -} - -impl StrictType for u5 { - const STRICT_LIB_NAME: &'static str = LIB_NAME_STD; - fn strict_name() -> Option { Some(tn!("U5")) } -} -impl StrictEncode for u5 { - fn strict_encode(&self, writer: W) -> io::Result { - writer.write_enum::(U5::try_from(self.to_u8()).expect("broken u5 types guarantees")) - } -} -impl StrictDecode for u5 { - fn strict_decode(reader: &mut impl TypedRead) -> Result { - let v: U5 = reader.read_enum()?; - Ok(u5::with(v as u8)) - } -} +impl_u!(U2, u2, _0 _1 _2 _3); +impl_u!(U3, u3, _0 _1 _2 _3 _4 _5 _6 _7); +impl_u!(U4, u4, _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _10 _11 _12 _13 _14 _15); +impl_u!(U5, u5, _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 + _10 _11 _12 _13 _14 _15 _16 _17 _18 _19 + _20 _21 _22 _23 _24 _25 _26 _27 _28 _29 + _30 _31); +impl_u!(U6, u6, _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 + _10 _11 _12 _13 _14 _15 _16 _17 _18 _19 + _20 _21 _22 _23 _24 _25 _26 _27 _28 _29 + _30 _31 _32 _33 _34 _35 _36 _37 _38 _39 + _40 _41 _42 _43 _44 _45 _46 _47 _48 _49 + _50 _51 _52 _53 _54 _55 _56 _57 _58 _59 + _60 _61 _62 _63); +impl_u!(U7, u7, _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 + _10 _11 _12 _13 _14 _15 _16 _17 _18 _19 + _20 _21 _22 _23 _24 _25 _26 _27 _28 _29 + _30 _31 _32 _33 _34 _35 _36 _37 _38 _39 + _40 _41 _42 _43 _44 _45 _46 _47 _48 _49 + _50 _51 _52 _53 _54 _55 _56 _57 _58 _59 + _60 _61 _62 _63 _64 _65 _66 _67 _68 _69 + _70 _71 _72 _73 _74 _75 _76 _77 _78 _79 + _80 _81 _82 _83 _84 _85 _86 _87 _88 _89 + _90 _91 _92 _93 _94 _95 _96 _97 _98 _99 + _100 _101 _102 _103 _104 _105 _106 _107 _108 _109 + _110 _111 _112 _113 _114 _115 _116 _117 _118 _119 + _120 _121 _122 _123 _124 _125 _126 _127); #[derive(Wrapper, WrapperMut, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)] #[wrapper(Deref, Display, Debug)] diff --git a/rust/src/types.rs b/rust/src/types.rs index b75be18..d0f6cd8 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -54,7 +54,7 @@ pub trait StrictType: Sized { .unwrap_or(path) } - let name = any::type_name::(); + let name = any::type_name::().replace('&', ""); let mut ident = vec![]; for mut arg in name.split([',', '<', '>', '(', ')']) { arg = arg.trim(); diff --git a/rust/src/util.rs b/rust/src/util.rs index 8373937..af2b62d 100644 --- a/rust/src/util.rs +++ b/rust/src/util.rs @@ -123,10 +123,6 @@ impl Ord for Variant { impl Display for Variant { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "{}", self.name)?; - if f.alternate() { - f.write_str(":")?; - Display::fmt(&self.tag, f)?; - } Ok(()) } }