Skip to content

Commit d146f5b

Browse files
author
andrew
committed
Missed releasing proc macros
1 parent 73eb53f commit d146f5b

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "structre"
3-
version = "0.0.2"
3+
version = "0.0.3"
44
edition = "2021"
55
license = "ISC"
66
description = "Static-checked parsing of regexes into structs"
@@ -17,4 +17,4 @@ unicode = ["structre_proc_macros/unicode", "regex/unicode"]
1717

1818
[dependencies]
1919
regex = { version = "1.7.0", default-features = false, features = ["std"] }
20-
structre_proc_macros = { path = "src/proc_macros", version = "0.0.1" }
20+
structre_proc_macros = { path = "src/proc_macros", version = "0.0.2" }

src/proc_macros/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "structre_proc_macros"
3-
version = "0.0.1"
3+
version = "0.0.2"
44
edition = "2021"
55
license = "ISC"
66
description = "Static-checked parsing of regexes into structs (helper crate)"
@@ -22,3 +22,6 @@ proc-macro2 = "1.0.47"
2222
quote = "1.0.21"
2323
regex-syntax = "0.6.28"
2424
syn = "1.0.103"
25+
26+
[dev-dependencies]
27+
genemichaels = "0.1.21"

src/proc_macros/mod.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn gen_impls(regex_raw: &str, ast: syn::DeriveInput) -> TokenStream {
168168
}
169169
#vis fn parse(&self, input: &str) -> Result < #name,
170170
String > {
171-
let caps_ = self.0.captures(input).ok_or_else(|| ToString::to_string("No match"))?;
171+
let caps_ = self.0.captures(input).ok_or_else(|| std::string::ToString::to_string("No match"))?;
172172
#value
173173
}
174174
}
@@ -195,69 +195,79 @@ pub fn structre(args: proc_macro::TokenStream, body: proc_macro::TokenStream) ->
195195

196196
#[cfg(test)]
197197
mod tests {
198-
use std::str::FromStr;
198+
use std::{
199+
str::FromStr,
200+
};
201+
use genemichaels::FormatConfig;
199202
use proc_macro2::TokenStream;
200203
use crate::gen_value;
201204
use quote::quote;
202205

206+
fn comp(got: TokenStream, expected: TokenStream) {
207+
let cfg = FormatConfig::default();
208+
let mut s = [got, expected].into_iter().map(|s| genemichaels::format_str(&quote!(fn x() {
209+
#s
210+
}).to_string(), &cfg)).collect::<Vec<_>>();
211+
let got = s.remove(0).expect("Failed to format got code").rendered;
212+
let expected = s.remove(0).expect("Failed to format expected code").rendered;
213+
assert_eq!(got, expected, "Mismatch:\n\nGot:\n{}\n\nExpected:\n{}", got, expected);
214+
}
215+
203216
#[test]
204217
fn newtype_string() {
205-
assert_eq!(
206-
gen_value(
207-
"(a)",
208-
&syn::parse2(TokenStream::from_str("struct Parsed(String);").unwrap()).unwrap(),
209-
).to_string(),
218+
comp(
219+
gen_value("(a)", &syn::parse2(TokenStream::from_str("struct Parsed(String);").unwrap()).unwrap()),
210220
quote!(
211221
Ok(
212222
Parsed(
213-
String::from_str(
223+
<String as std::str::FromStr>::from_str(
214224
caps_.get(1usize).map(|m| m.as_str()).unwrap_or(""),
215-
).context("Failed to parse field 0")?,
225+
).map_err(|e| format!("Failed to parse field 0: {}", e))?,
216226
),
217227
)
218-
).to_string()
228+
),
219229
);
220230
}
221231

222232
#[test]
223233
fn tuple() {
224-
assert_eq!(
234+
comp(
225235
gen_value(
226236
"(a)(b)",
227237
&syn::parse2(TokenStream::from_str("struct Parsed((String, u32));").unwrap()).unwrap(),
228-
).to_string(),
238+
),
229239
quote!(
230240
Ok(
231241
Parsed(
232242
(
233-
String::from_str(
243+
<String as std::str::FromStr>::from_str(
234244
caps_.get(1usize).map(|m| m.as_str()).unwrap_or(""),
235-
).context("Failed to parse field 0")?,
236-
u32::from_str(
245+
).map_err(|e| format!("Failed to parse field 0: {}", e))?,
246+
<u32 as std::str::FromStr>::from_str(
237247
caps_.get(2usize).map(|m| m.as_str()).unwrap_or(""),
238-
).context("Failed to parse field 1")?,
248+
).map_err(|e| format!("Failed to parse field 1: {}", e))?,
239249
),
240250
),
241251
)
242-
).to_string()
252+
),
243253
);
244254
}
245255

246256
#[test]
247257
fn struct_() {
248-
assert_eq!(
258+
comp(
249259
gen_value(
250260
"(?P<a>a)(?P<b>b)",
251261
&syn::parse2(TokenStream::from_str("struct Parsed { b: u32, a: String }").unwrap()).unwrap(),
252-
).to_string(),
262+
),
253263
quote!(Ok(Parsed {
254-
b: u32::from_str(
264+
b: <u32 as std::str::FromStr>::from_str(
255265
caps_.get(2usize).map(|m| m.as_str()).unwrap_or(""),
256-
).context("Failed to parse field b")?,
257-
a: String::from_str(
266+
).map_err(|e| format!("Failed to parse field b: {}", e))?,
267+
a: <String as std::str::FromStr>::from_str(
258268
caps_.get(1usize).map(|m| m.as_str()).unwrap_or(""),
259-
).context("Failed to parse field a")?,
260-
})).to_string()
269+
).map_err(|e| format!("Failed to parse field a: {}", e))?,
270+
})),
261271
);
262272
}
263273
}

0 commit comments

Comments
 (0)