Skip to content

Commit

Permalink
Generalize for Record<N> using trait stacking
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielSimonetto committed Aug 18, 2022
1 parent 46b4400 commit 658ebed
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
19 changes: 16 additions & 3 deletions noodles-bed/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,25 @@ type Block = (usize, usize);

use serde_with::{serde_as, DisplayFromStr};

// TODO: impl Record<N>
// enum BedRecord

// // TODO: impl Record<N>
// #[serde_as]
// #[derive(Deserialize, Serialize)]
// pub struct AuxiliarBedRecordWrapper<const N: u8> {
// #[serde_as(as = "DisplayFromStr")]
// pub record: Record<N>,
// }

#[serde_as]
#[derive(Deserialize, Serialize)]
pub struct AuxiliarBedRecordWrapper {
pub struct AuxiliarBedRecordWrapper<T>
where
T: BedN<3> + std::str::FromStr + fmt::Display,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
#[serde_as(as = "DisplayFromStr")]
pub record: Record<3>,
pub record: T,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
34 changes: 29 additions & 5 deletions noodles-bed/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{ser, Serialize};

use crate::{
error::{Error, Result},
record::AuxiliarBedRecordWrapper,
record::{AuxiliarBedRecordWrapper, BedN},
Record,
};

Expand All @@ -24,16 +24,24 @@ where
}

// And that we should restrict the types that have access to the to_string() inner function
pub fn vec_record_to_string(vec: Vec<Record<3>>) -> Result<String> {
let input: Vec<AuxiliarBedRecordWrapper> = vec
pub fn vec_record_to_string<T>(vec: Vec<T>) -> Result<String>
where
T: BedN<3> + std::str::FromStr + std::fmt::Display,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
let input: Vec<AuxiliarBedRecordWrapper<T>> = vec
.into_iter()
.map(|record| AuxiliarBedRecordWrapper { record })
.collect();

to_string(&input)
}

pub fn record_to_string(record: Record<3>) -> Result<String> {
pub fn record_to_string<T>(record: T) -> Result<String>
where
T: BedN<3> + std::str::FromStr + std::fmt::Display,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
let abrw = AuxiliarBedRecordWrapper { record };
to_string(&abrw)
}
Expand Down Expand Up @@ -358,7 +366,7 @@ impl<'a> ser::SerializeStructVariant for &'a mut Record3Serializer {
}
#[cfg(test)]
mod serde_tests {
use crate::Record;
use crate::{record::Name, Record};

use super::*;

Expand Down Expand Up @@ -400,6 +408,22 @@ mod serde_tests {
assert_eq!(&result, expected);
}

#[test]
fn test_to_string_single_auxiliar_bed_record_4_wrapper() {
let record = Record::<4>::builder()
.set_reference_sequence_name("sq0")
.set_start_position(noodles_core::Position::try_from(8).unwrap())
.set_end_position(noodles_core::Position::try_from(13).unwrap())
.set_name("ndls1".parse::<Name>().unwrap())
.build()
.unwrap();

let result = record_to_string(record).unwrap();
let expected = "sq0\t7\t13\tndls1\n";

assert_eq!(&result, expected);
}

// TODO
// #[test]
// fn test_to_bytes_single_record() { }
Expand Down

0 comments on commit 658ebed

Please sign in to comment.