From a41777e741d9886f204f8de797f752167aea103e Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 5 Oct 2024 12:54:33 -0700 Subject: [PATCH] Remove some methods from `SnapshotContents` 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 --- cargo-insta/src/cli.rs | 16 ++++++++++------ cargo-insta/src/container.rs | 7 +++++-- insta/src/snapshot.rs | 19 ++----------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/cargo-insta/src/cli.rs b/cargo-insta/src/cli.rs index 4d56ed86..f2e5a341 100644 --- a/cargo-insta/src/cli.rs +++ b/cargo-insta/src/cli.rs @@ -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; @@ -1140,11 +1140,15 @@ fn pending_snapshots_cmd(cmd: PendingSnapshotsCommand) -> Result<(), Box 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, diff --git a/cargo-insta/src/container.rs b/cargo-insta/src/container.rs index 776f8dfd..c52a492e 100644 --- a/cargo-insta/src/container.rs +++ b/cargo-insta/src/container.rs @@ -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; @@ -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; } diff --git a/insta/src/snapshot.rs b/insta/src/snapshot.rs index 08037b84..73f43084 100644 --- a/insta/src/snapshot.rs +++ b/insta/src/snapshot.rs @@ -542,20 +542,12 @@ impl Snapshot { } } - /// The normalized snapshot contents as a String - pub fn contents_string(&self) -> Option { - 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'); } @@ -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 {