Skip to content

Commit 5c27d0b

Browse files
committed
Tweak
1 parent 240eb3f commit 5c27d0b

3 files changed

Lines changed: 84 additions & 84 deletions

File tree

crates/filepack-cbor/src/input.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ pub(crate) struct Input {
1414
}
1515

1616
impl Input {
17-
pub(crate) fn derive_decode(&self) -> Result<proc_macro2::TokenStream> {
17+
pub(crate) fn decode(&self) -> Result<proc_macro2::TokenStream> {
1818
match self.data {
19-
Data::Enum(_) => self.derive_decode_enum(),
19+
Data::Enum(_) => self.decode_enum(),
2020
Data::Struct(_) => {
2121
if self.is_transparent()? {
22-
self.derive_decode_transparent()
22+
self.decode_transparent()
2323
} else {
24-
self.derive_decode_struct()
24+
self.decode_struct()
2525
}
2626
}
2727
}
2828
}
2929

30-
pub(crate) fn derive_decode_enum(&self) -> Result<proc_macro2::TokenStream> {
30+
pub(crate) fn decode_enum(&self) -> Result<proc_macro2::TokenStream> {
3131
let name = &self.ident;
3232

3333
let repr = self.parse_repr()?;
@@ -48,7 +48,7 @@ impl Input {
4848
})
4949
}
5050

51-
pub(crate) fn derive_decode_struct(&self) -> Result<proc_macro2::TokenStream> {
51+
pub(crate) fn decode_struct(&self) -> Result<proc_macro2::TokenStream> {
5252
let name = &self.ident;
5353

5454
let fields = self.parse_fields()?;
@@ -80,7 +80,7 @@ impl Input {
8080
})
8181
}
8282

83-
pub(crate) fn derive_decode_transparent(&self) -> Result<proc_macro2::TokenStream> {
83+
pub(crate) fn decode_transparent(&self) -> Result<proc_macro2::TokenStream> {
8484
let name = &self.ident;
8585

8686
let member = self.transparent_member()?;
@@ -99,20 +99,20 @@ impl Input {
9999
})
100100
}
101101

102-
pub(crate) fn derive_encode(&self) -> Result<proc_macro2::TokenStream> {
102+
pub(crate) fn encode(&self) -> Result<proc_macro2::TokenStream> {
103103
match self.data {
104-
Data::Enum(_) => self.derive_encode_enum(),
104+
Data::Enum(_) => self.encode_enum(),
105105
Data::Struct(_) => {
106106
if self.is_transparent()? {
107-
self.derive_encode_transparent()
107+
self.encode_transparent()
108108
} else {
109-
self.derive_encode_struct()
109+
self.encode_struct()
110110
}
111111
}
112112
}
113113
}
114114

115-
pub(crate) fn derive_encode_enum(&self) -> Result<proc_macro2::TokenStream> {
115+
pub(crate) fn encode_enum(&self) -> Result<proc_macro2::TokenStream> {
116116
let name = &self.ident;
117117

118118
let repr = self.parse_repr()?;
@@ -126,7 +126,7 @@ impl Input {
126126
})
127127
}
128128

129-
pub(crate) fn derive_encode_struct(&self) -> Result<proc_macro2::TokenStream> {
129+
pub(crate) fn encode_struct(&self) -> Result<proc_macro2::TokenStream> {
130130
let name = &self.ident;
131131

132132
let fields = self.parse_fields()?;
@@ -164,7 +164,7 @@ impl Input {
164164
})
165165
}
166166

167-
pub(crate) fn derive_encode_transparent(&self) -> Result<proc_macro2::TokenStream> {
167+
pub(crate) fn encode_transparent(&self) -> Result<proc_macro2::TokenStream> {
168168
let name = &self.ident;
169169

170170
let member = self.transparent_member()?;

crates/filepack-cbor/src/lib.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,22 @@ mod input;
1717
mod parsed_field;
1818

1919
#[proc_macro_derive(Decode, attributes(cbor, n))]
20-
pub fn derive_decode(input: TokenStream) -> TokenStream {
20+
pub fn decode(input: TokenStream) -> TokenStream {
2121
let input = syn::parse_macro_input!(input as DeriveInput);
2222

2323
let input = match Input::from_derive_input(&input) {
2424
Ok(input) => input,
2525
Err(err) => return err.write_errors().into(),
2626
};
2727

28-
match input.derive_decode() {
29-
Ok(tokens) => tokens.into(),
30-
Err(err) => err.to_compile_error().into(),
31-
}
32-
}
33-
34-
#[proc_macro_derive(Encode, attributes(cbor, n))]
35-
pub fn derive_encode(input: TokenStream) -> TokenStream {
36-
let input = syn::parse_macro_input!(input as DeriveInput);
37-
38-
let input = match Input::from_derive_input(&input) {
39-
Ok(input) => input,
40-
Err(err) => return err.write_errors().into(),
41-
};
42-
43-
match input.derive_encode() {
28+
match input.decode() {
4429
Ok(tokens) => tokens.into(),
4530
Err(err) => err.to_compile_error().into(),
4631
}
4732
}
4833

4934
#[proc_macro_derive(DecodeFromStr)]
50-
pub fn derive_decode_from_str(input: TokenStream) -> TokenStream {
35+
pub fn decode_from_str(input: TokenStream) -> TokenStream {
5136
let input = syn::parse_macro_input!(input as DeriveInput);
5237

5338
let name = &input.ident;
@@ -68,8 +53,23 @@ pub fn derive_decode_from_str(input: TokenStream) -> TokenStream {
6853
.into()
6954
}
7055

56+
#[proc_macro_derive(Encode, attributes(cbor, n))]
57+
pub fn encode(input: TokenStream) -> TokenStream {
58+
let input = syn::parse_macro_input!(input as DeriveInput);
59+
60+
let input = match Input::from_derive_input(&input) {
61+
Ok(input) => input,
62+
Err(err) => return err.write_errors().into(),
63+
};
64+
65+
match input.encode() {
66+
Ok(tokens) => tokens.into(),
67+
Err(err) => err.to_compile_error().into(),
68+
}
69+
}
70+
7171
#[proc_macro_derive(EncodeDisplay)]
72-
pub fn derive_encode_display(input: TokenStream) -> TokenStream {
72+
pub fn encode_display(input: TokenStream) -> TokenStream {
7373
let input = syn::parse_macro_input!(input as DeriveInput);
7474

7575
let name = &input.ident;

src/derive.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,43 @@ fn all_required() {
8484
);
8585
}
8686

87+
#[test]
88+
fn decode_from_str() {
89+
#[derive(Debug, DecodeFromStr, PartialEq)]
90+
struct Foo(String);
91+
92+
#[derive(Debug, Snafu)]
93+
#[snafu(display("bar error"))]
94+
struct FooError;
95+
96+
impl FromStr for Foo {
97+
type Err = FooError;
98+
99+
fn from_str(s: &str) -> Result<Self, FooError> {
100+
if s == "foo" {
101+
Ok(Foo(s.to_string()))
102+
} else {
103+
Err(FooError)
104+
}
105+
}
106+
}
107+
108+
assert_eq!(
109+
Foo::decode_from_slice(&[0x63, 0x66, 0x6f, 0x6f]).unwrap(),
110+
Foo("foo".to_string()),
111+
);
112+
113+
let err = Foo::decode_from_slice(&[0x63, 0x62, 0x61, 0x72]).unwrap_err();
114+
115+
assert_matches!(
116+
err,
117+
DecodeError::FromStr {
118+
name: "Foo",
119+
ref source,
120+
} if source.to_string() == "bar error",
121+
);
122+
}
123+
87124
#[test]
88125
fn decode_with_optional() {
89126
fn decode_offset(decoder: &mut Decoder) -> Result<u64, DecodeError> {
@@ -124,6 +161,20 @@ fn decode_with_required() {
124161
);
125162
}
126163

164+
#[test]
165+
fn encode_display() {
166+
#[derive(EncodeDisplay)]
167+
struct Foo;
168+
169+
impl Display for Foo {
170+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
171+
write!(f, "foo")
172+
}
173+
}
174+
175+
assert_eq!(Foo.encode_to_vec(), [0x63, 0x66, 0x6f, 0x6f]);
176+
}
177+
127178
#[test]
128179
fn encode_with_optional() {
129180
struct Foreign(u64);
@@ -171,57 +222,6 @@ fn encode_with_required() {
171222
);
172223
}
173224

174-
#[test]
175-
fn decode_from_str() {
176-
#[derive(Debug, DecodeFromStr, PartialEq)]
177-
struct Foo(String);
178-
179-
#[derive(Debug, Snafu)]
180-
#[snafu(display("bar error"))]
181-
struct FooError;
182-
183-
impl FromStr for Foo {
184-
type Err = FooError;
185-
186-
fn from_str(s: &str) -> Result<Self, FooError> {
187-
if s == "foo" {
188-
Ok(Foo(s.to_string()))
189-
} else {
190-
Err(FooError)
191-
}
192-
}
193-
}
194-
195-
assert_eq!(
196-
Foo::decode_from_slice(&[0x63, 0x66, 0x6f, 0x6f]).unwrap(),
197-
Foo("foo".to_string()),
198-
);
199-
200-
let err = Foo::decode_from_slice(&[0x63, 0x62, 0x61, 0x72]).unwrap_err();
201-
202-
assert_matches!(
203-
err,
204-
DecodeError::FromStr {
205-
name: "Foo",
206-
ref source,
207-
} if source.to_string() == "bar error",
208-
);
209-
}
210-
211-
#[test]
212-
fn encode_display() {
213-
#[derive(EncodeDisplay)]
214-
struct Foo;
215-
216-
impl Display for Foo {
217-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
218-
write!(f, "foo")
219-
}
220-
}
221-
222-
assert_eq!(Foo.encode_to_vec(), [0x63, 0x66, 0x6f, 0x6f]);
223-
}
224-
225225
#[test]
226226
fn enum_invalid_discriminant() {
227227
#[derive(Debug, Decode, FromRepr)]

0 commit comments

Comments
 (0)