From d45d9b9930ebbc425bc772b2fe70fe90ab7ffd1f Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Wed, 11 Jan 2023 10:17:08 -0600 Subject: [PATCH] vcf/header/record/value/map/format: Change conversion from key value to a reference --- noodles-bcf/src/header/string_maps.rs | 8 ++++---- noodles-vcf/CHANGELOG.md | 3 +++ noodles-vcf/src/header.rs | 4 ++-- noodles-vcf/src/header/builder.rs | 4 ++-- noodles-vcf/src/header/record/value/map/format.rs | 14 +++++++------- noodles-vcf/src/record.rs | 8 ++++---- noodles-vcf/src/record/genotypes.rs | 6 +++--- noodles-vcf/src/record/genotypes/genotype.rs | 14 +++++++------- noodles-vcf/src/record/genotypes/genotype/field.rs | 10 +++++----- .../src/record/genotypes/genotype/field/value.rs | 2 +- 10 files changed, 38 insertions(+), 35 deletions(-) diff --git a/noodles-bcf/src/header/string_maps.rs b/noodles-bcf/src/header/string_maps.rs index 8805304fc..e3e857e59 100644 --- a/noodles-bcf/src/header/string_maps.rs +++ b/noodles-bcf/src/header/string_maps.rs @@ -43,7 +43,7 @@ impl StringMaps { /// let header = vcf::Header::builder() /// .add_info(info::Key::TotalDepth, Map::::from(&info::Key::TotalDepth)) /// .add_filter("q10", Map::::new("Quality below 10")) - /// .add_format(format::Key::ReadDepth, Map::::from(format::Key::ReadDepth)) + /// .add_format(format::Key::ReadDepth, Map::::from(&format::Key::ReadDepth)) /// .add_contig("sq0".parse()?, Map::::new()) /// .build(); /// @@ -78,7 +78,7 @@ impl StringMaps { /// let header = vcf::Header::builder() /// .add_info(info::Key::TotalDepth, Map::::from(&info::Key::TotalDepth)) /// .add_filter("q10", Map::::new("Quality below 10")) - /// .add_format(format::Key::ReadDepth, Map::::from(format::Key::ReadDepth)) + /// .add_format(format::Key::ReadDepth, Map::::from(&format::Key::ReadDepth)) /// .add_contig("sq0".parse()?, Map::::new()) /// .build(); /// @@ -417,11 +417,11 @@ mod tests { .add_filter("q10", Map::::new("Quality below 10")) .add_format( format::Key::Genotype, - Map::::from(format::Key::Genotype), + Map::::from(&format::Key::Genotype), ) .add_format( format::Key::ReadDepth, - Map::::from(format::Key::ReadDepth), + Map::::from(&format::Key::ReadDepth), ) .add_alternative_allele(del, Map::::new("Deletion")) .build(); diff --git a/noodles-vcf/CHANGELOG.md b/noodles-vcf/CHANGELOG.md index 85f3f074a..068c5060f 100644 --- a/noodles-vcf/CHANGELOG.md +++ b/noodles-vcf/CHANGELOG.md @@ -15,6 +15,9 @@ `Map` no longer holds the record ID. Use the collection key instead. + * vcf/header/record/value/map/format: Change conversion from key value to a + reference (`&format::Key`). + * vcf/header/record/value/map/info: Change conversion from key value to a reference (`&info::Key`). diff --git a/noodles-vcf/src/header.rs b/noodles-vcf/src/header.rs index 9ad093c28..ecd6ed4fe 100644 --- a/noodles-vcf/src/header.rs +++ b/noodles-vcf/src/header.rs @@ -217,7 +217,7 @@ impl Header { /// }; /// /// let id = Key::Genotype; - /// let format = Map::::from(id.clone()); + /// let format = Map::::from(&id); /// /// let header = vcf::Header::builder() /// .add_format(id, format.clone()) @@ -244,7 +244,7 @@ impl Header { /// let mut header = vcf::Header::default(); /// /// let id = Key::Genotype; - /// let format = Map::::from(id.clone()); + /// let format = Map::::from(&id); /// header.formats_mut().insert(id, format.clone()); /// /// let formats = header.formats(); diff --git a/noodles-vcf/src/header/builder.rs b/noodles-vcf/src/header/builder.rs index c8e14b0e2..35314486f 100644 --- a/noodles-vcf/src/header/builder.rs +++ b/noodles-vcf/src/header/builder.rs @@ -109,7 +109,7 @@ impl Builder { /// }; /// /// let id = Key::Genotype; - /// let format = Map::::from(id.clone()); + /// let format = Map::::from(&id); /// /// let header = vcf::Header::builder() /// .add_format(id, format.clone()) @@ -399,7 +399,7 @@ mod tests { .add_filter("q10", Map::::new("Quality below 10")) .add_format( FormatKey::Genotype, - Map::::from(FormatKey::Genotype), + Map::::from(&FormatKey::Genotype), ) .add_alternative_allele(del, Map::::new("Deletion")) .set_assembly("file:///assemblies.fasta") diff --git a/noodles-vcf/src/header/record/value/map/format.rs b/noodles-vcf/src/header/record/value/map/format.rs index 0988c17c1..3ac357366 100644 --- a/noodles-vcf/src/header/record/value/map/format.rs +++ b/noodles-vcf/src/header/record/value/map/format.rs @@ -110,13 +110,13 @@ impl fmt::Display for Map { } } -impl From for Map { - fn from(key: Key) -> Self { +impl From<&Key> for Map { + fn from(key: &Key) -> Self { use crate::header::format::key; - let number = key::number(&key).unwrap_or(Number::Count(1)); - let ty = key::ty(&key).unwrap_or(Type::String); - let description = key::description(&key).map(|s| s.into()).unwrap_or_default(); + let number = key::number(key).unwrap_or(Number::Count(1)); + let ty = key::ty(key).unwrap_or(Type::String); + let description = key::description(key).map(|s| s.into()).unwrap_or_default(); Self { inner: Format { @@ -205,7 +205,7 @@ mod tests { #[test] fn test_fmt() { - let map = Map::::from(Key::Genotype); + let map = Map::::from(&Key::Genotype); let expected = r#",Number=1,Type=String,Description="Genotype""#; assert_eq!(map.to_string(), expected); } @@ -218,7 +218,7 @@ mod tests { (String::from("Description"), String::from("Genotype")), ])?; - let expected = Map::::from(Key::Genotype); + let expected = Map::::from(&Key::Genotype); assert_eq!(actual, expected); diff --git a/noodles-vcf/src/record.rs b/noodles-vcf/src/record.rs index 8715592fe..526458065 100644 --- a/noodles-vcf/src/record.rs +++ b/noodles-vcf/src/record.rs @@ -515,8 +515,8 @@ impl Record { /// }; /// /// let header = vcf::Header::builder() - /// .add_format(Key::Genotype, Map::::from(Key::Genotype)) - /// .add_format(Key::ConditionalGenotypeQuality, Map::::from(Key::ConditionalGenotypeQuality)) + /// .add_format(Key::Genotype, Map::::from(&Key::Genotype)) + /// .add_format(Key::ConditionalGenotypeQuality, Map::::from(&Key::ConditionalGenotypeQuality)) /// .build(); /// /// let keys = "GT:GQ".parse()?; @@ -832,10 +832,10 @@ mod tests { }; let header = Header::builder() - .add_format(Key::Genotype, Map::::from(Key::Genotype)) + .add_format(Key::Genotype, Map::::from(&Key::Genotype)) .add_format( Key::ConditionalGenotypeQuality, - Map::::from(Key::ConditionalGenotypeQuality), + Map::::from(&Key::ConditionalGenotypeQuality), ) .build(); diff --git a/noodles-vcf/src/record/genotypes.rs b/noodles-vcf/src/record/genotypes.rs index 8f2f80fef..c3750a89a 100644 --- a/noodles-vcf/src/record/genotypes.rs +++ b/noodles-vcf/src/record/genotypes.rs @@ -39,7 +39,7 @@ impl Genotypes { /// }; /// /// let header = vcf::Header::builder() - /// .add_format(Key::Genotype, Map::::from(Key::Genotype)) + /// .add_format(Key::Genotype, Map::::from(&Key::Genotype)) /// .build(); /// /// let actual = Genotypes::parse("GT:GQ\t0|0:13", &header)?; @@ -232,10 +232,10 @@ mod tests { }; let header = crate::Header::builder() - .add_format(Key::Genotype, Map::::from(Key::Genotype)) + .add_format(Key::Genotype, Map::::from(&Key::Genotype)) .add_format( Key::ConditionalGenotypeQuality, - Map::::from(Key::ConditionalGenotypeQuality), + Map::::from(&Key::ConditionalGenotypeQuality), ) .build(); diff --git a/noodles-vcf/src/record/genotypes/genotype.rs b/noodles-vcf/src/record/genotypes/genotype.rs index 7c1094a4d..2a1438a5b 100644 --- a/noodles-vcf/src/record/genotypes/genotype.rs +++ b/noodles-vcf/src/record/genotypes/genotype.rs @@ -100,10 +100,10 @@ impl Genotype { /// }; /// /// let header = vcf::Header::builder() - /// .add_format(Key::Genotype, Map::::from(Key::Genotype)) + /// .add_format(Key::Genotype, Map::::from(&Key::Genotype)) /// .add_format( /// Key::ConditionalGenotypeQuality, - /// Map::::from(Key::ConditionalGenotypeQuality), + /// Map::::from(&Key::ConditionalGenotypeQuality), /// ) /// .build(); /// @@ -131,7 +131,7 @@ impl Genotype { let field = if let Some(format) = formats.get(key) { Field::from_str_format(raw_field, key, format).map_err(ParseError::InvalidField)? } else { - let format = Map::::from(key.clone()); + let format = Map::::from(key); Field::from_str_format(raw_field, key, &format).map_err(ParseError::InvalidField)? }; @@ -156,10 +156,10 @@ impl Genotype { /// }; /// /// let header = vcf::Header::builder() - /// .add_format(Key::Genotype, Map::::from(Key::Genotype)) + /// .add_format(Key::Genotype, Map::::from(&Key::Genotype)) /// .add_format( /// Key::ConditionalGenotypeQuality, - /// Map::::from(Key::ConditionalGenotypeQuality), + /// Map::::from(&Key::ConditionalGenotypeQuality), /// ) /// .build(); /// @@ -268,10 +268,10 @@ mod tests { #[test] fn test_parse() -> Result<(), Box> { let header = crate::Header::builder() - .add_format(Key::Genotype, Map::::from(Key::Genotype)) + .add_format(Key::Genotype, Map::::from(&Key::Genotype)) .add_format( Key::ConditionalGenotypeQuality, - Map::::from(Key::ConditionalGenotypeQuality), + Map::::from(&Key::ConditionalGenotypeQuality), ) .build(); diff --git a/noodles-vcf/src/record/genotypes/genotype/field.rs b/noodles-vcf/src/record/genotypes/genotype/field.rs index a50482ac0..fed17d4d4 100644 --- a/noodles-vcf/src/record/genotypes/genotype/field.rs +++ b/noodles-vcf/src/record/genotypes/genotype/field.rs @@ -55,7 +55,7 @@ impl Field { /// }; /// /// let key = Key::ConditionalGenotypeQuality; - /// let format = Map::::from(key.clone()); + /// let format = Map::::from(&key); /// /// assert_eq!( /// Field::from_str_format("13", &key, &format), @@ -176,25 +176,25 @@ mod tests { #[test] fn test_from_str_format() -> Result<(), ParseError> { let key = Key::MappingQuality; - let format = Map::::from(key.clone()); + let format = Map::::from(&key); let actual = Field::from_str_format(".", &key, &format)?; assert_eq!(actual.key(), &key); assert_eq!(actual.value(), None); let key = Key::ConditionalGenotypeQuality; - let format = Map::::from(key.clone()); + let format = Map::::from(&key); let actual = Field::from_str_format("13", &key, &format)?; assert_eq!(actual.key(), &key); assert_eq!(actual.value(), Some(&Value::Integer(13))); let key = Key::GenotypeCopyNumberQuality; - let format = Map::::from(key.clone()); + let format = Map::::from(&key); let actual = Field::from_str_format("8.333", &key, &format)?; assert_eq!(actual.key(), &key); assert_eq!(actual.value(), Some(&Value::Float(8.333))); let key = Key::Genotype; - let format = Map::::from(key.clone()); + let format = Map::::from(&key); let actual = Field::from_str_format("0|0", &key, &format)?; assert_eq!(actual.key(), &key); assert_eq!(actual.value(), Some(&Value::String(String::from("0|0")))); diff --git a/noodles-vcf/src/record/genotypes/genotype/field/value.rs b/noodles-vcf/src/record/genotypes/genotype/field/value.rs index d08e6930e..61dec2df0 100644 --- a/noodles-vcf/src/record/genotypes/genotype/field/value.rs +++ b/noodles-vcf/src/record/genotypes/genotype/field/value.rs @@ -161,7 +161,7 @@ impl Value { /// record::genotypes::genotype::field::Value, /// }; /// - /// let format = Map::::from(Key::ConditionalGenotypeQuality); + /// let format = Map::::from(&Key::ConditionalGenotypeQuality); /// assert_eq!(Value::from_str_format("13", &format), Ok(Value::Integer(13))); /// ``` pub fn from_str_format(s: &str, format: &Map) -> Result {