Skip to content

Commit b57db89

Browse files
committed
feat: implement Display for M
This is helpful when M is embedded in errors and just generally for users. This representation is for humans and we do not implement FromStr, we could change this in the future without causing a breaking change. See also [this opinion in an old Rust discussion thread][1]: > my final answer to "is changing Display's output breaking" is "no if > it's an Error, yes if it's FromStr, and you're doing it wrong > otherwise." [1]: https://users.rust-lang.org/t/are-changes-to-fmt-display-considered-breaking/59775/7
1 parent 514490b commit b57db89

12 files changed

+166
-1
lines changed

rusqlite_migration/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![doc = include_str!(concat!(env!("OUT_DIR"), "/readme_for_rustdoc.md"))]
1919

2020
use std::borrow::Cow;
21+
use std::fmt::Display;
2122

2223
use log::{debug, info, trace, warn};
2324
use rusqlite::{Connection, Transaction};
@@ -101,6 +102,38 @@ pub struct M<'u> {
101102
comment: Option<&'u str>,
102103
}
103104

105+
impl Display for M<'_> {
106+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107+
let M {
108+
up,
109+
up_hook,
110+
down,
111+
down_hook,
112+
foreign_key_check,
113+
comment,
114+
} = self;
115+
let nl = if f.alternate() { "\n" } else { "" };
116+
let ind = if f.alternate() { "\n " } else { "" };
117+
write!(f, r#"M({ind}up: "{up}""#)?;
118+
if up_hook.is_some() {
119+
write!(f, ", {ind}up hook")?;
120+
}
121+
if let Some(down) = down {
122+
write!(f, r#", {ind}down: "{down}""#)?;
123+
}
124+
if down_hook.is_some() {
125+
write!(f, ", {ind}down hook")?;
126+
}
127+
if *foreign_key_check {
128+
write!(f, ", {ind}foreign key check")?;
129+
}
130+
if let Some(comment) = comment {
131+
write!(f, r#", {ind}comment: "{comment}""#)?;
132+
}
133+
write!(f, "{nl})")
134+
}
135+
}
136+
104137
impl PartialEq for M<'_> {
105138
fn eq(&self, other: &Self) -> bool {
106139
use std::ptr;

rusqlite_migration/src/tests/core.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::{iter::FromIterator, num::NonZeroUsize};
1717

1818
use rusqlite::{Connection, OpenFlags, Transaction};
1919

20-
use crate::tests::helpers::all_valid_down;
20+
use crate::tests::helpers::{all_valid_down, m_valid0_down, m_valid_fk_down};
2121
use crate::{
2222
tests::helpers::{
2323
all_valid_up, m_invalid_fk, m_invalid_fk_down, m_valid0_up, m_valid10_up, m_valid11_up,
@@ -573,3 +573,28 @@ fn test_pending_migrations_errors() -> Result<(), Box<dyn std::error::Error>> {
573573

574574
Ok(())
575575
}
576+
577+
#[test]
578+
fn test_display() {
579+
insta::assert_snapshot!("up_only", m_valid0_up());
580+
insta::assert_snapshot!("up_only_alt", format!("{:#}", m_valid0_up()));
581+
582+
insta::assert_snapshot!("up_down", m_valid0_down());
583+
insta::assert_snapshot!("up_down_alt", format!("{:#}", m_valid0_down()));
584+
585+
insta::assert_snapshot!("up_down_fk", m_valid_fk_down());
586+
insta::assert_snapshot!("up_down_fk_alt", format!("{:#}", m_valid_fk_down()));
587+
588+
let everything = M {
589+
up: "UP",
590+
up_hook: Some(Box::new(|_: &Transaction| Ok(()))),
591+
down: Some("DOWN"),
592+
down_hook: Some(Box::new(|_: &Transaction| Ok(()))),
593+
foreign_key_check: true,
594+
comment: Some("Comment, likely a filename in practice!"),
595+
};
596+
insta::assert_snapshot!("everything", everything);
597+
insta::assert_debug_snapshot!("everything_debug", everything);
598+
insta::assert_compact_debug_snapshot!("everything_compact_debug", everything);
599+
insta::assert_snapshot!("everything_alt", format!("{everything:#}"));
600+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: everything
4+
snapshot_kind: text
5+
---
6+
M(up: "UP", up hook, down: "DOWN", down hook, foreign key check, comment: "Comment, likely a filename in practice!")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: "format!(\"{everything:#}\")"
4+
snapshot_kind: text
5+
---
6+
M(
7+
up: "UP",
8+
up hook,
9+
down: "DOWN",
10+
down hook,
11+
foreign key check,
12+
comment: "Comment, likely a filename in practice!"
13+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: everything
4+
snapshot_kind: text
5+
---
6+
M { up: "UP", up_hook: Some(MigrationHook(<closure>)), down: Some("DOWN"), down_hook: Some(MigrationHook(<closure>)), foreign_key_check: true, comment: Some("Comment, likely a filename in practice!") }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: everything
4+
snapshot_kind: text
5+
---
6+
M {
7+
up: "UP",
8+
up_hook: Some(
9+
MigrationHook(<closure>),
10+
),
11+
down: Some(
12+
"DOWN",
13+
),
14+
down_hook: Some(
15+
MigrationHook(<closure>),
16+
),
17+
foreign_key_check: true,
18+
comment: Some(
19+
"Comment, likely a filename in practice!",
20+
),
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: m_valid0_down()
4+
snapshot_kind: text
5+
---
6+
M(up: "CREATE TABLE m1(a, b); CREATE TABLE m2(a, b, c);", down: "DROP TABLE m1; DROP TABLE m2;")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: "format!(\"{:#}\", m_valid0_down())"
4+
snapshot_kind: text
5+
---
6+
M(
7+
up: "CREATE TABLE m1(a, b); CREATE TABLE m2(a, b, c);",
8+
down: "DROP TABLE m1; DROP TABLE m2;"
9+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: m_valid_fk_down()
4+
snapshot_kind: text
5+
---
6+
M(up: "
7+
CREATE TABLE fk1(a PRIMARY KEY);
8+
CREATE TABLE fk2(
9+
a,
10+
FOREIGN KEY(a) REFERENCES fk1(a)
11+
);
12+
INSERT INTO fk1 (a) VALUES ('foo');
13+
INSERT INTO fk2 (a) VALUES ('foo');
14+
", down: "DELETE FROM fk2; DELETE FROM fk1; DROP TABLE fk2; DROP TABLE fk1;", foreign key check)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: rusqlite_migration/src/tests/core.rs
3+
expression: "format!(\"{:#}\", m_valid_fk_down())"
4+
snapshot_kind: text
5+
---
6+
M(
7+
up: "
8+
CREATE TABLE fk1(a PRIMARY KEY);
9+
CREATE TABLE fk2(
10+
a,
11+
FOREIGN KEY(a) REFERENCES fk1(a)
12+
);
13+
INSERT INTO fk1 (a) VALUES ('foo');
14+
INSERT INTO fk2 (a) VALUES ('foo');
15+
",
16+
down: "DELETE FROM fk2; DELETE FROM fk1; DROP TABLE fk2; DROP TABLE fk1;",
17+
foreign key check
18+
)

0 commit comments

Comments
 (0)