Skip to content

Commit

Permalink
Have serde serialization match strum. (#7)
Browse files Browse the repository at this point in the history
* Have serde serialization match strum.

* Fix test bug.

* More bug test fix.

* Update tests/it/code_vsi.rs

Co-authored-by: Jessica Black <[email protected]>

---------

Co-authored-by: Jessica Black <[email protected]>
  • Loading branch information
csasarak and jssblck authored Jun 21, 2024
1 parent 7377867 commit d7b85cb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
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
);
}

0 comments on commit d7b85cb

Please sign in to comment.