#Improving an API for RNA base sequence to codon sequence conversion

6 messages · Page 1 of 1 (latest)

remote yew
#

I'm trying to redo a crate that takes in a series of ASCII bytes representing a sequence of RNA bases and produces a sequence of Codon values. I want to be able to do the conversions using the following API. Are there any improvements, including naming, that I could make to the API?```rust
#[derive(...)]
#[repr(u8)]
pub enum Codon {
Aaa,
// ...
Uuu,
}

impl Codon {
pub fn load_vec<P>(path: P) -> Result<Vec<Codon>, SequenceReadError>
where
P: AsRef<Path>,
{
// ...
}

pub fn load_iter<P>(path: P) -> Result<CodonReadIter, SequenceReadError> {
    // ...
}

// ...

}

trait Read {
fn read_codon(&mut self) -> Result<Codon, SequenceReadError>;
fn to_codon_vec(&mut self) -> Result<Vec<Codon>, SequenceReadError>;
fn codons(&mut self) -> CodonReadIter;
}

impl<R> Read for R
where
R: std::io::Read,
{
// ...
}

trait Ascii {
fn to_codon_vec(&self) -> Result<Vec<Codon>, SequenceParseError>
fn codons(&self) -> CodonStringlikeIter;
}

impl<A> Ascii for A
where
A: AsRef<[u8]>,
{
// ...
}

rough hearth
#

I wouldn't name my trait Read, for starters. That'll conflict with std::io::Read.

#

I also would give two separate traits the same function. to_codon_vec should live in one or the other.

#

Specifically the one in your Read trait can just be dropped in favor of the more flexible r.codons().collect()

remote yew
rough hearth
#

CodonRead sounds great.