Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions insta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ json = ["serde"]
ron = ["dep:ron", "serde"]
toml = ["dep:toml", "serde"]
yaml = ["serde"]
json-pretty-compact = ["dep:json-pretty-compact", "dep:serde_json", "serde"]

# internal feature exclusive to cargo-insta
_cargo_insta_internal = ["clap"]
Expand All @@ -60,6 +61,8 @@ regex = { version = "1.6.0", default-features = false, optional = true, features
serde = { version = "1.0.117", optional = true }
once_cell = "1.20.2"
clap = { workspace = true, optional = true }
serde_json = { version = "1.0.145", optional = true }
json-pretty-compact = { version = "0.1.2", optional = true }

[dev-dependencies]
rustc_version = "0.4.0"
Expand Down
9 changes: 9 additions & 0 deletions insta/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ macro_rules! assert_compact_json_snapshot {
};
}

#[cfg(feature = "json-pretty-compact")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
#[macro_export]
macro_rules! assert_compact_pretty_json_snapshot {
($($arg:tt)*) => {
$crate::_assert_serialized_snapshot!(format=JsonPrettyCompact, $($arg)*);
};
}

// This macro handles optional trailing commas.
#[doc(hidden)]
#[macro_export]
Expand Down
12 changes: 12 additions & 0 deletions insta/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum SerializationFormat {
Yaml,
Json,
JsonCompact,
#[cfg(feature = "json-pretty-compact")]
JsonPrettyCompact,
}

#[derive(Debug)]
Expand All @@ -40,6 +42,16 @@ pub fn serialize_content(mut content: Content, format: SerializationFormat) -> S
SerializationFormat::Yaml => yaml::to_string(&content)[4..].to_string(),
SerializationFormat::Json => json::to_string_pretty(&content),
SerializationFormat::JsonCompact => json::to_string_compact(&content),
#[cfg(feature = "json-pretty-compact")]
SerializationFormat::JsonPrettyCompact => {
let mut buf = Vec::new();
let mut ser = serde_json::Serializer::with_formatter(
&mut buf,
json_pretty_compact::PrettyCompactFormatter::new(),
);
content.serialize(&mut ser).unwrap();
String::from_utf8(buf).unwrap()
}
#[cfg(feature = "csv")]
SerializationFormat::Csv => {
let mut buf = Vec::with_capacity(128);
Expand Down
Loading