From 3732ed9728484dd0040e6ab8862762473056bc29 Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Wed, 26 Jul 2023 14:08:30 +0200 Subject: [PATCH] tag::Score: Rename (unchecked) constructor --- crates/core-json/src/tag/mod.rs | 6 ++--- crates/core-json/src/tag/tests.rs | 10 ++++----- crates/core/src/tag/score/mod.rs | 4 ++-- crates/core/src/tag/score/tests.rs | 12 +++++++--- crates/core/src/tag/tests.rs | 22 +++++++++---------- crates/media-file/src/util/gigtag/tests.rs | 2 +- crates/repo-sqlite/src/db/track_tag/models.rs | 2 +- 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/crates/core-json/src/tag/mod.rs b/crates/core-json/src/tag/mod.rs index 6ec3d228..04f333cb 100644 --- a/crates/core-json/src/tag/mod.rs +++ b/crates/core-json/src/tag/mod.rs @@ -212,7 +212,7 @@ impl<'de> Visitor<'de> for ScoreVisitor { where E: serde::de::Error, { - let score = _core::Score::new(value); + let score = _core::Score::new_unchecked(value); if !score.is_valid() { return Err(serde::de::Error::invalid_value( serde::de::Unexpected::Float(value), @@ -259,12 +259,12 @@ impl From for _core::PlainTag<'static> { ..Default::default() }, IntScoreFallback(iscore) => _core::PlainTag { - score: _core::Score::new(iscore as f64), + score: _core::Score::new_unchecked(iscore as f64), ..Default::default() }, LabelIntScoreFallback(label, iscore) => _core::PlainTag { label: Some(label.into()), - score: _core::Score::new(iscore as f64), + score: _core::Score::new_unchecked(iscore as f64), }, LabelScore(label, score) => _core::PlainTag { label: Some(label.into()), diff --git a/crates/core-json/src/tag/tests.rs b/crates/core-json/src/tag/tests.rs index 7c29acd1..c8800ad2 100644 --- a/crates/core-json/src/tag/tests.rs +++ b/crates/core-json/src/tag/tests.rs @@ -23,7 +23,7 @@ fn should_fail_to_deserialize_plain_tag_from_single_element_array_with_label() { #[test] fn deserialize_plain_tag_score_integer_one() { - let score = _core::Score::new(1.0); + let score = _core::Score::new_unchecked(1.0); let tag: PlainTag = serde_json::from_str("1").unwrap(); assert_eq!( _core::PlainTag::from(PlainTag::Score(score.into())), @@ -34,7 +34,7 @@ fn deserialize_plain_tag_score_integer_one() { #[test] fn deserialize_plain_tag_score_integer_zero() { - let score = _core::Score::new(0.0); + let score = _core::Score::new_unchecked(0.0); let tag: PlainTag = serde_json::from_str("0").unwrap(); assert_eq!( _core::PlainTag::from(PlainTag::Score(score.into())), @@ -46,7 +46,7 @@ fn deserialize_plain_tag_score_integer_zero() { #[test] fn deserialize_plain_tag_label_score() { let label = _core::Label::from_unchecked("label"); - let score = _core::Score::new(0.5); + let score = _core::Score::new_unchecked(0.5); let json = format!("[\"{label}\",{}]", score.value()); let tag: PlainTag = serde_json::from_str(&json).unwrap(); assert_eq!(PlainTag::LabelScore(label.into(), score.into()), tag); @@ -57,7 +57,7 @@ fn deserialize_plain_tag_label_score() { fn deserialize_plain_tag_label_score_integer_zero() { let expected_tag = _core::PlainTag { label: Some(_core::Label::from_unchecked("label")), - score: _core::Score::new(0.0), + score: _core::Score::new_unchecked(0.0), }; // Ensure to parse score from literal 0, not 0.0! let json = format!("[\"{}\",0]", expected_tag.label.as_ref().unwrap()); @@ -70,7 +70,7 @@ fn deserialize_plain_tag_label_score_integer_zero() { fn deserialize_plain_tag_label_score_integer_one() { let expected_tag = _core::PlainTag { label: Some(_core::Label::from_unchecked("label")), - score: _core::Score::new(1.0), + score: _core::Score::new_unchecked(1.0), }; // Ensure to parse score from literal 1, not 1.0! let json = format!("[\"{}\",1]", expected_tag.label.as_ref().unwrap()); diff --git a/crates/core/src/tag/score/mod.rs b/crates/core/src/tag/score/mod.rs index ce53960c..c5cfe064 100644 --- a/crates/core/src/tag/score/mod.rs +++ b/crates/core/src/tag/score/mod.rs @@ -33,11 +33,11 @@ impl Score { } pub fn clamp_from(value: impl Into) -> Self { - Self::new(Self::clamp_value(value)) + Self::new_unchecked(Self::clamp_value(value)) } #[must_use] - pub const fn new(value: ScoreValue) -> Self { + pub const fn new_unchecked(value: ScoreValue) -> Self { Self(value) } diff --git a/crates/core/src/tag/score/tests.rs b/crates/core/src/tag/score/tests.rs index d7a4be8c..6de333f1 100644 --- a/crates/core/src/tag/score/tests.rs +++ b/crates/core/src/tag/score/tests.rs @@ -7,9 +7,15 @@ use super::*; fn validate() { assert!(Score::MIN.validate().is_ok()); assert!(Score::MAX.validate().is_ok()); - assert!(Score::new(Score::MIN.0 + Score::MAX.0).validate().is_ok()); - assert!(Score::new(Score::MIN.0 - Score::MAX.0).validate().is_err()); - assert!(Score::new(Score::MAX.0 + Score::MAX.0).validate().is_err()); + assert!(Score::new_unchecked(Score::MIN.0 + Score::MAX.0) + .validate() + .is_ok()); + assert!(Score::new_unchecked(Score::MIN.0 - Score::MAX.0) + .validate() + .is_err()); + assert!(Score::new_unchecked(Score::MAX.0 + Score::MAX.0) + .validate() + .is_err()); } #[test] diff --git a/crates/core/src/tag/tests.rs b/crates/core/src/tag/tests.rs index 8c406248..7f2b37fa 100644 --- a/crates/core/src/tag/tests.rs +++ b/crates/core/src/tag/tests.rs @@ -24,7 +24,7 @@ fn canonical_unique_labels_and_score() { plain: vec![ PlainTag { label: Some(Label::from_unchecked("label1")), - score: Score::new(0.5), + score: Score::new_unchecked(0.5), }, PlainTag { label: Some(Label::from_unchecked("label2")), @@ -43,7 +43,7 @@ fn duplicate_labels_same_score() { plain: vec![ PlainTag { label: Some(Label::from_unchecked("label1")), - score: Score::new(0.5), + score: Score::new_unchecked(0.5), }, PlainTag { label: Some(Label::from_unchecked("label2")), @@ -51,7 +51,7 @@ fn duplicate_labels_same_score() { }, PlainTag { label: Some(Label::from_unchecked("label1")), - score: Score::new(0.5), + score: Score::new_unchecked(0.5), }, ], ..Default::default() @@ -66,7 +66,7 @@ fn duplicate_labels_differing_score() { plain: vec![ PlainTag { label: Some(Label::from_unchecked("label1")), - score: Score::new(0.7), + score: Score::new_unchecked(0.7), }, PlainTag { label: Some(Label::from_unchecked("label2")), @@ -74,7 +74,7 @@ fn duplicate_labels_differing_score() { }, PlainTag { label: Some(Label::from_unchecked("label1")), - score: Score::new(0.5), + score: Score::new_unchecked(0.5), }, ], ..Default::default() @@ -191,11 +191,11 @@ fn duplicate_facets_and_labels() { tags: vec![ PlainTag { label: Some(Label::from_unchecked("label2")), - score: Score::new(0.5), + score: Score::new_unchecked(0.5), }, PlainTag { label: Some(Label::from_unchecked("label2")), - score: Score::new(1.0), + score: Score::new_unchecked(1.0), }, ], }, @@ -246,15 +246,15 @@ fn duplicate_facets_and_labels() { tags: vec![ PlainTag { label: Some(Label::from_unchecked("label2")), - score: Score::new(0.5), + score: Score::new_unchecked(0.5), }, PlainTag { label: Some(Label::from_unchecked("label2")), - score: Score::new(0.75), + score: Score::new_unchecked(0.75), }, PlainTag { label: Some(Label::from_unchecked("label2")), - score: Score::new(0.25), + score: Score::new_unchecked(0.25), }, ], }, @@ -267,7 +267,7 @@ fn duplicate_facets_and_labels() { facet_id: FacetId::from_unchecked("facet2"), tags: vec![PlainTag { label: Some(Label::from_unchecked("label2")), - score: Score::new(0.75), + score: Score::new_unchecked(0.75), },], })); } diff --git a/crates/media-file/src/util/gigtag/tests.rs b/crates/media-file/src/util/gigtag/tests.rs index ad4bd80c..f5fc985a 100644 --- a/crates/media-file/src/util/gigtag/tests.rs +++ b/crates/media-file/src/util/gigtag/tests.rs @@ -76,7 +76,7 @@ fn plain_tag_with_label_and_score<'a>( fn try_import_tags() { let label_value = "DJ"; let date_like_facet = facet_from_str("@20220703"); - let score = aoide_core::tag::Score::new(0.75); + let score = aoide_core::tag::Score::new_unchecked(0.75); // Label let tag = Tag { diff --git a/crates/repo-sqlite/src/db/track_tag/models.rs b/crates/repo-sqlite/src/db/track_tag/models.rs index d543712e..4ca796bf 100644 --- a/crates/repo-sqlite/src/db/track_tag/models.rs +++ b/crates/repo-sqlite/src/db/track_tag/models.rs @@ -26,7 +26,7 @@ impl From for (RecordId, Record) { debug_assert!(facet_id.as_ref().map_or(true, FacetId::is_valid)); let label = label.map(Label::from_unchecked); debug_assert!(label.as_ref().map_or(true, Label::is_valid)); - let score = Score::new(score); + let score = Score::new_unchecked(score); debug_assert!(score.is_valid()); let record = Record { track_id: track_id.into(),