Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all small unsigned int types, fix display for them and other enum Variant #29

Merged
merged 5 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion rust/derive/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,15 @@
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;
}
}

Check warning on line 241 in rust/derive/src/params.rs

View check run for this annotation

Codecov / codecov/patch

rust/derive/src/params.rs#L241

Added line #L241 was not covered by tests
LitStr::new(&camel_case, name.span())
}
Some(ref name) => name.clone(),
}
}
Expand Down
196 changes: 55 additions & 141 deletions rust/src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -134,149 +134,63 @@
}
}

#[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)]

Check warning on line 140 in rust/src/stl.rs

View check run for this annotation

Codecov / codecov/patch

rust/src/stl.rs#L139-L140

Added lines #L139 - L140 were not covered by tests
#[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<TypeName> { Some(tn!("U4")) }
}
impl StrictEncode for u4 {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
writer.write_enum::<U4>(U4::try_from(self.to_u8()).expect("broken u4 types guarantees"))
}
}
impl StrictDecode for u4 {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
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<TypeName> { Some(tn!(stringify!($ty))) }

Check warning on line 150 in rust/src/stl.rs

View check run for this annotation

Codecov / codecov/patch

rust/src/stl.rs#L150

Added line #L150 was not covered by tests
}
impl StrictEncode for $inner {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
writer.write_enum::<$ty>($ty::try_from(self.to_u8())
.expect(concat!("broken", stringify!($inner), "type guarantees")))
}

Check warning on line 156 in rust/src/stl.rs

View check run for this annotation

Codecov / codecov/patch

rust/src/stl.rs#L153-L156

Added lines #L153 - L156 were not covered by tests
}
impl StrictDecode for $inner {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
let v: $ty = reader.read_enum()?;
Ok(<$inner>::with(v as u8))
}

Check warning on line 162 in rust/src/stl.rs

View check run for this annotation

Codecov / codecov/patch

rust/src/stl.rs#L159-L162

Added lines #L159 - L162 were not covered by tests
}
}
}

#[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<TypeName> { Some(tn!("U5")) }
}
impl StrictEncode for u5 {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
writer.write_enum::<U5>(U5::try_from(self.to_u8()).expect("broken u5 types guarantees"))
}
}
impl StrictDecode for u5 {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
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);

Check warning on line 193 in rust/src/stl.rs

View check run for this annotation

Codecov / codecov/patch

rust/src/stl.rs#L167-L193

Added lines #L167 - L193 were not covered by tests

#[derive(Wrapper, WrapperMut, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait StrictType: Sized {
.unwrap_or(path)
}

let name = any::type_name::<Self>();
let name = any::type_name::<Self>().replace('&', "");
let mut ident = vec![];
for mut arg in name.split([',', '<', '>', '(', ')']) {
arg = arg.trim();
Expand Down
4 changes: 0 additions & 4 deletions rust/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Loading