Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have serde serialization match strum. #7

Merged
merged 4 commits into from
Jun 21, 2024
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v3.0.0

- Changes the Serde serialization of `Kind` to match Strum's.

# v2.1.0

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fingerprint"
version = "2.1.0"
version = "3.0.0"
edition = "2021"

[features]
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum Kind {
/// generally require specific circumstances: `CommentStrippedSHA256` requires that the file is text, and
/// hypothetical future fingerprint kinds such as something based on an AST would require that the file is source code.
#[strum(serialize = "sha_256")]
#[serde(rename = "sha_256")]
RawSha256,

/// Represents a fingerprint derived by hashing the contents of a file with the SHA256 algorithm
Expand All @@ -113,6 +114,7 @@ pub enum Kind {
/// - Any sequence of multiple contiguous `\n` bytes are collapsed to a single `\n` byte.
/// - The final `\n` byte is removed from the end of the stream if present.
#[strum(serialize = "comment_stripped:sha_256")]
#[serde(rename = "comment_stripped:sha_256")]
CommentStrippedSha256,

/// Represents a fingerprint derived by hashing the raw contents of a JAR file with the SHA256 algorithm
Expand All @@ -123,6 +125,7 @@ pub enum Kind {
/// - All the contents of these files are then hashed using SHA256.
/// - If the contents of the files are text, `\r\n` sequences are converted to `\n`.
#[strum(serialize = "v1.raw.jar")]
#[serde(rename = "v1.raw.jar")]
JarRawV1,

/// Represents a fingerprint derived by hashing the raw contents of a JAR file in the same manner
Expand All @@ -132,6 +135,7 @@ pub enum Kind {
/// Specifically:
/// - The content of the JAR file is hashed as-is using the sha1 algorithm.
#[strum(serialize = "v1.mavencentral.jar")]
#[serde(rename = "v1.mavencentral.jar")]
JarMavenCentralV1,

/// Represents a fingerprint derived by hashing the raw contents of a JAR file with the SHA256 algorithm
Expand All @@ -142,6 +146,7 @@ pub enum Kind {
/// - All the contents of these files are then hashed using SHA256.
/// - If the contents of the files are text, `\r\n` sequences are converted to `\n`.
#[strum(serialize = "v1.class.jar")]
#[serde(rename = "v1.class.jar")]
JarClassV1,
}

Expand Down
45 changes: 45 additions & 0 deletions tests/it/code_vsi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Tests for plain code files using legacy VSI fingerprints.

use std::collections::HashSet;

use pretty_assertions::assert_eq;

use fingerprint::*;
use strum::IntoEnumIterator;

/// Assert fingerprint for the named kind is the same as the fingerprint for the provided content.
/// Comparison content (the "expected" fingerprint) is generated with [`hash`].
Expand Down Expand Up @@ -128,3 +131,45 @@ int main() {
);
assert_eq!(Some(&expected), combined.get(Kind::CommentStrippedSha256));
}

#[test]
fn strum_serde_fingerprint_kind_serialization_matches() {
for kind in Kind::iter() {
let json_value = serde_json::to_value(kind).expect("serde serialization");
let json = json_value.as_str().expect("get string");
let strum = kind.to_string();

assert_eq!(json, strum);
}
}

#[test]
fn serde_serialization_matches_external_contract() {
let expected_serializations_for_kinds = vec![
"v1.class.jar",
"v1.mavencentral.jar",
"v1.raw.jar",
"comment_stripped:sha_256",
"sha_256",
]
.into_iter()
.map(|s| s.to_string())
.collect::<HashSet<String>>();

let mut actual_serializations_for_kinds = HashSet::new();

for kind in Kind::iter() {
let json_value = serde_json::to_value(kind).expect("serde serialization");
let json = json_value.as_str().expect("get string").to_string();

assert!(
actual_serializations_for_kinds.insert(json),
"serialized value for kind {kind:?} is duplicate"
);
}

assert_eq!(
expected_serializations_for_kinds,
actual_serializations_for_kinds
);
}