Skip to content

Commit e83ccc4

Browse files
authored
Merge pull request #30 from strict-types/ident
Improvements for Ident type
2 parents 9975e4a + 9c20b32 commit e83ccc4

File tree

4 files changed

+89
-19
lines changed

4 files changed

+89
-19
lines changed

rust/src/ident.rs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ impl Debug for Ident {
120120
}
121121
}
122122

123+
impl Ident {
124+
pub fn from_uint(val: impl Into<u64>) -> Ident {
125+
Self::try_from(format!("_{}", val.into())).expect("always a valid identifier")
126+
}
127+
}
128+
123129
impl StrictDumb for Ident {
124130
fn strict_dumb() -> Self { Self::from("Dumb") }
125131
}
@@ -169,7 +175,7 @@ impl From<&'static str> for TypeName {
169175
impl TryFrom<String> for TypeName {
170176
type Error = InvalidIdent;
171177

172-
fn try_from(s: String) -> Result<Self, Self::Error> { Ident::try_from(s).map(Self) }
178+
fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
173179
}
174180

175181
impl Debug for TypeName {
@@ -178,6 +184,13 @@ impl Debug for TypeName {
178184
}
179185
}
180186

187+
impl TypeName {
188+
pub fn as_str(&self) -> &str { self.0.as_str() }
189+
pub fn as_ident(&self) -> &Ident { &self.0 }
190+
pub fn to_ident(&self) -> Ident { self.clone().into() }
191+
pub fn into_ident(self) -> Ident { self.into() }
192+
}
193+
181194
impl_strict_newtype!(TypeName, STRICT_TYPES_LIB);
182195

183196
#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
@@ -197,7 +210,7 @@ impl From<&'static str> for FieldName {
197210
impl TryFrom<String> for FieldName {
198211
type Error = InvalidIdent;
199212

200-
fn try_from(s: String) -> Result<Self, Self::Error> { Ident::try_from(s).map(Self) }
213+
fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
201214
}
202215

203216
impl Debug for FieldName {
@@ -208,11 +221,47 @@ impl Debug for FieldName {
208221

209222
impl FieldName {
210223
pub fn as_str(&self) -> &str { self.0.as_str() }
224+
pub fn as_ident(&self) -> &Ident { &self.0 }
225+
pub fn to_ident(&self) -> Ident { self.clone().into() }
226+
pub fn into_ident(self) -> Ident { self.into() }
211227
}
212228

213229
impl_strict_newtype!(FieldName, STRICT_TYPES_LIB);
214230

215-
pub type VariantName = FieldName;
231+
#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
232+
#[wrapper(Deref, Display, FromStr)]
233+
#[wrapper_mut(DerefMut)]
234+
#[cfg_attr(
235+
feature = "serde",
236+
derive(Serialize, Deserialize),
237+
serde(crate = "serde_crate", transparent)
238+
)]
239+
pub struct VariantName(Ident);
240+
241+
impl From<&'static str> for VariantName {
242+
fn from(ident: &'static str) -> Self { VariantName(Ident::from(ident)) }
243+
}
244+
245+
impl TryFrom<String> for VariantName {
246+
type Error = InvalidIdent;
247+
248+
fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
249+
}
250+
251+
impl Debug for VariantName {
252+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
253+
f.debug_tuple("VariantName").field(&self.as_str()).finish()
254+
}
255+
}
256+
257+
impl VariantName {
258+
pub fn as_str(&self) -> &str { self.0.as_str() }
259+
pub fn as_ident(&self) -> &Ident { &self.0 }
260+
pub fn to_ident(&self) -> Ident { self.clone().into() }
261+
pub fn into_ident(self) -> Ident { self.into() }
262+
}
263+
264+
impl_strict_newtype!(VariantName, STRICT_TYPES_LIB);
216265

217266
#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
218267
#[wrapper(Deref, Display, FromStr)]
@@ -231,7 +280,7 @@ impl From<&'static str> for LibName {
231280
impl TryFrom<String> for LibName {
232281
type Error = InvalidIdent;
233282

234-
fn try_from(s: String) -> Result<Self, Self::Error> { Ident::try_from(s).map(Self) }
283+
fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
235284
}
236285

237286
impl Debug for LibName {
@@ -240,4 +289,11 @@ impl Debug for LibName {
240289
}
241290
}
242291

292+
impl LibName {
293+
pub fn as_str(&self) -> &str { self.0.as_str() }
294+
pub fn as_ident(&self) -> &Ident { &self.0 }
295+
pub fn to_ident(&self) -> Ident { self.clone().into() }
296+
pub fn into_ident(self) -> Ident { self.into() }
297+
}
298+
243299
impl_strict_newtype!(LibName, STRICT_TYPES_LIB);

rust/src/macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ macro_rules! impl_strict_struct {
9898
};
9999
}
100100

101+
#[macro_export]
102+
macro_rules! ident {
103+
($name:literal) => {
104+
$crate::Ident::from($name).into()
105+
};
106+
($name:expr) => {
107+
$crate::Ident::from($name).into()
108+
};
109+
}
110+
101111
#[macro_export]
102112
macro_rules! tn {
103113
($name:literal) => {

rust/src/traits.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use amplify::confinement::{Collection, Confined};
2727
use amplify::num::u24;
2828
use amplify::Wrapper;
2929

30-
use super::DecodeError;
30+
use super::{DecodeError, VariantName};
3131
use crate::{
3232
DeserializeError, FieldName, Primitive, SerializeError, Sizing, StrictDumb, StrictEnum,
3333
StrictReader, StrictStruct, StrictSum, StrictTuple, StrictType, StrictUnion, StrictWriter,
@@ -147,7 +147,7 @@ pub trait TypedRead: Sized {
147147

148148
fn read_union<T: StrictUnion>(
149149
&mut self,
150-
inner: impl FnOnce(FieldName, &mut Self::UnionReader) -> Result<T, DecodeError>,
150+
inner: impl FnOnce(VariantName, &mut Self::UnionReader) -> Result<T, DecodeError>,
151151
) -> Result<T, DecodeError>;
152152

153153
fn read_enum<T: StrictEnum>(&mut self) -> Result<T, DecodeError>
@@ -234,13 +234,13 @@ pub trait ReadStruct {
234234
pub trait DefineEnum: Sized {
235235
type Parent: TypedWrite;
236236
type EnumWriter: WriteEnum<Parent = Self::Parent>;
237-
fn define_variant(self, name: FieldName) -> Self;
237+
fn define_variant(self, name: VariantName) -> Self;
238238
fn complete(self) -> Self::EnumWriter;
239239
}
240240

241241
pub trait WriteEnum: Sized {
242242
type Parent: TypedWrite;
243-
fn write_variant(self, name: FieldName) -> io::Result<Self>;
243+
fn write_variant(self, name: VariantName) -> io::Result<Self>;
244244
fn complete(self) -> Self::Parent;
245245
}
246246

@@ -250,14 +250,18 @@ pub trait DefineUnion: Sized {
250250
type StructDefiner: DefineStruct<Parent = Self>;
251251
type UnionWriter: WriteUnion<Parent = Self::Parent>;
252252

253-
fn define_unit(self, name: FieldName) -> Self;
254-
fn define_newtype<T: StrictEncode + StrictDumb>(self, name: FieldName) -> Self {
253+
fn define_unit(self, name: VariantName) -> Self;
254+
fn define_newtype<T: StrictEncode + StrictDumb>(self, name: VariantName) -> Self {
255255
self.define_tuple(name, |definer| definer.define_field::<T>().complete())
256256
}
257-
fn define_tuple(self, name: FieldName, inner: impl FnOnce(Self::TupleDefiner) -> Self) -> Self;
257+
fn define_tuple(
258+
self,
259+
name: VariantName,
260+
inner: impl FnOnce(Self::TupleDefiner) -> Self,
261+
) -> Self;
258262
fn define_struct(
259263
self,
260-
name: FieldName,
264+
name: VariantName,
261265
inner: impl FnOnce(Self::StructDefiner) -> Self,
262266
) -> Self;
263267

@@ -269,18 +273,18 @@ pub trait WriteUnion: Sized {
269273
type TupleWriter: WriteTuple<Parent = Self>;
270274
type StructWriter: WriteStruct<Parent = Self>;
271275

272-
fn write_unit(self, name: FieldName) -> io::Result<Self>;
273-
fn write_newtype(self, name: FieldName, value: &impl StrictEncode) -> io::Result<Self> {
276+
fn write_unit(self, name: VariantName) -> io::Result<Self>;
277+
fn write_newtype(self, name: VariantName, value: &impl StrictEncode) -> io::Result<Self> {
274278
self.write_tuple(name, |writer| Ok(writer.write_field(value)?.complete()))
275279
}
276280
fn write_tuple(
277281
self,
278-
name: FieldName,
282+
name: VariantName,
279283
inner: impl FnOnce(Self::TupleWriter) -> io::Result<Self>,
280284
) -> io::Result<Self>;
281285
fn write_struct(
282286
self,
283-
name: FieldName,
287+
name: VariantName,
284288
inner: impl FnOnce(Self::StructWriter) -> io::Result<Self>,
285289
) -> io::Result<Self>;
286290

rust/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::collections::BTreeSet;
2424
use std::fmt::{Debug, Display};
2525
use std::marker::PhantomData;
2626

27-
use crate::{FieldName, LibName, TypeName};
27+
use crate::{LibName, TypeName, VariantName};
2828

2929
#[derive(Clone, Eq, PartialEq, Debug, Display, Error)]
3030
#[display("unexpected variant {1} for enum or union {0:?}")]
@@ -155,7 +155,7 @@ pub trait StrictSum: StrictType {
155155
);
156156
}
157157

158-
fn variant_name_by_tag(tag: u8) -> Option<FieldName> {
158+
fn variant_name_by_tag(tag: u8) -> Option<VariantName> {
159159
Self::ALL_VARIANTS
160160
.iter()
161161
.find(|(n, _)| *n == tag)
@@ -195,7 +195,7 @@ where
195195
Self: StrictSum + Copy + TryFrom<u8, Error = VariantError<u8>>,
196196
u8: From<Self>,
197197
{
198-
fn from_variant_name(name: &FieldName) -> Result<Self, VariantError<&FieldName>> {
198+
fn from_variant_name(name: &VariantName) -> Result<Self, VariantError<&VariantName>> {
199199
for (tag, n) in Self::ALL_VARIANTS {
200200
if *n == name.as_str() {
201201
return Self::try_from(*tag).map_err(|_| VariantError(Self::strict_name(), name));

0 commit comments

Comments
 (0)