Skip to content

Commit

Permalink
Remove some methods from SnapshotContents (#640)
Browse files Browse the repository at this point in the history
Since the binary snapshots have merged, there are some methods which
return `Some` if the enum is a certain variant. We can just use pattern
matching for that.

(Note that the code is a bit complicated and has lots of repetition of
'kind'-like fields; but wanted to merge the binary snapshots feature
before cleaning up the internals
  • Loading branch information
max-sixty authored Oct 5, 2024
1 parent 915afdc commit d2650c1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
16 changes: 10 additions & 6 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::{env, fs};
use std::{io, process};

use console::{set_colors_enabled, style, Key, Term};
use insta::Snapshot;
use insta::_cargo_insta_support::{
is_ci, SnapshotPrinter, SnapshotUpdate, TestRunner, ToolConfig, UnreferencedSnapshots,
};
use insta::{internals::SnapshotContents, Snapshot};
use itertools::Itertools;
use semver::Version;
use serde::Serialize;
Expand Down Expand Up @@ -1140,11 +1140,15 @@ fn pending_snapshots_cmd(cmd: PendingSnapshotsCommand) -> Result<(), Box<dyn Err
let is_inline = snapshot_container.snapshot_file().is_none();
for snapshot_ref in snapshot_container.iter_snapshots() {
if cmd.as_json {
let old_snapshot = snapshot_ref
.old
.as_ref()
.map(|x| x.contents_string().unwrap());
let new_snapshot = snapshot_ref.new.contents_string().unwrap();
let old_snapshot = snapshot_ref.old.as_ref().map(|x| match x.contents() {
SnapshotContents::Text(x) => x.to_string(),
_ => unreachable!(),
});
let new_snapshot = match snapshot_ref.new.contents() {
SnapshotContents::Text(x) => x.to_string(),
_ => unreachable!(),
};

let info = if is_inline {
SnapshotKey::InlineSnapshot {
path: &target_file,
Expand Down
7 changes: 5 additions & 2 deletions cargo-insta/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};

use insta::Snapshot;
pub(crate) use insta::TextSnapshotKind;
use insta::_cargo_insta_support::{ContentError, PendingInlineSnapshot};
use insta::{internals::SnapshotContents, Snapshot};

use crate::inline::FilePatcher;

Expand Down Expand Up @@ -186,7 +186,10 @@ impl SnapshotContainer {
Operation::Accept => {
patcher.set_new_content(
idx,
snapshot.new.contents().as_string_contents().unwrap(),
match snapshot.new.contents() {
SnapshotContents::Text(c) => c,
_ => unreachable!(),
},
);
did_accept = true;
}
Expand Down
19 changes: 2 additions & 17 deletions insta/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,20 +542,12 @@ impl Snapshot {
}
}

/// The normalized snapshot contents as a String
pub fn contents_string(&self) -> Option<String> {
match self.contents() {
SnapshotContents::Text(contents) => Some(contents.normalize()),
SnapshotContents::Binary(_) => None,
}
}

fn serialize_snapshot(&self, md: &MetaData) -> String {
let mut buf = yaml::to_string(&md.as_content());
buf.push_str("---\n");

if let Some(ref contents_str) = self.contents_string() {
buf.push_str(contents_str);
if let SnapshotContents::Text(ref contents) = self.snapshot {
buf.push_str(&contents.to_string());
buf.push('\n');
}

Expand Down Expand Up @@ -636,13 +628,6 @@ impl SnapshotContents {
pub fn is_binary(&self) -> bool {
matches!(self, SnapshotContents::Binary(_))
}

pub fn as_string_contents(&self) -> Option<&TextSnapshotContents> {
match self {
SnapshotContents::Text(contents) => Some(contents),
SnapshotContents::Binary(_) => None,
}
}
}

impl TextSnapshotContents {
Expand Down

0 comments on commit d2650c1

Please sign in to comment.