Skip to content

Commit 45bda57

Browse files
committed
Fix templating of missing submatches
1 parent 3609ccc commit 45bda57

3 files changed

Lines changed: 6 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "structex"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
edition = "2024"
55
authors = ["sminez <innes.andersonmorrison@gmail.com>"]
66
license = "MIT"

src/template.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,7 @@ impl Template {
398398

399399
Fragment::Cap(n) => match caps.submatch_text(*n) {
400400
Some(slice) => slice.write_to(w).map_err(RenderError::Io)?,
401-
None => w
402-
.write(format!("{{{n}}}").as_bytes())
403-
.map_err(RenderError::Io)?,
401+
None => 0, // missing submatches map to an empty string
404402
},
405403

406404
Fragment::Var(from, to) => {
@@ -465,9 +463,7 @@ impl Template {
465463
.map_err(RenderError::Io)?,
466464
Fragment::Cap(n) => match caps.submatch_text(*n) {
467465
Some(slice) => slice.write_to(w).map_err(RenderError::Io)?,
468-
None => w
469-
.write(format!("{{{n}}}").as_bytes())
470-
.map_err(RenderError::Io)?,
466+
None => 0, // missing submatches map to an empty string
471467
},
472468
Fragment::Var(from, to) => match ctx.render_var(&self.raw[*from..*to], w) {
473469
Some(res) => res.map_err(RenderError::Io)?,
@@ -558,7 +554,7 @@ mod tests {
558554
#[test_case("{1} {2}", "foo bar"; "both submatches")]
559555
#[test_case("{2} {1}", "bar foo"; "flipped submatches")]
560556
#[test_case("{1}\\n{2}", "foo\nbar"; "submatches and newline")]
561-
#[test_case("{3}", "{3}"; "unknown capture")]
557+
#[test_case("{3}", ""; "unknown capture")]
562558
#[test]
563559
fn render_works(s: &str, expected: &str) {
564560
let caps: TaggedCaptures<&str> = TaggedCaptures {
@@ -590,7 +586,7 @@ mod tests {
590586
#[test_case("{1} {2}", "foo bar"; "both submatches")]
591587
#[test_case("{2} {1}", "bar foo"; "flipped submatches")]
592588
#[test_case("{1}\\n{2}", "foo\nbar"; "submatches and newline")]
593-
#[test_case("{3}", "{3}"; "unknown capture")]
589+
#[test_case("{3}", ""; "unknown capture")]
594590
#[test_case("{unknown}", "from context"; "variable without context")]
595591
#[test]
596592
fn render_with_context_works(s: &str, expected: &str) {

0 commit comments

Comments
 (0)