Skip to content

Commit

Permalink
feat: strand: Use +/- rather than (+)/(-) for str (#39)
Browse files Browse the repository at this point in the history
Change the string representation of Strand to "+", "-", ".".
The current string representation is "(+)", "(-)", and "".
  • Loading branch information
sjackman committed Jun 6, 2023
1 parent cf89188 commit 45f518e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
14 changes: 9 additions & 5 deletions src/annot/contig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,21 @@ impl<R, S> Loc for Contig<R, S> {
impl<R, S> Display for Contig<R, S>
where
R: Display,
S: Display,
S: Display + Clone + Into<Strand>,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"{}:{}-{}{}",
"{}:{}-{}",
self.refid,
self.start,
self.start + self.length as isize,
self.strand
)
self.start + self.length as isize
)?;
let strand: Strand = self.strand.clone().into();
if !strand.is_unknown() {
write!(f, "({})", strand)?;
}
Ok(())
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/annot/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,15 @@ where
impl<R, S> Display for Pos<R, S>
where
R: Display,
S: Display,
S: Display + Clone + Into<Strand>,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}:{}{}", self.refid, self.pos, self.strand)
let strand: Strand = self.strand.clone().into();
if strand.is_unknown() {
write!(f, "{}:{}", self.refid, self.pos)
} else {
write!(f, "{}:{}({})", self.refid, self.pos, strand)
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/annot/spliced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ impl<R, S> Loc for Spliced<R, S> {
impl<R, S> Display for Spliced<R, S>
where
R: Display,
S: Display,
S: Display + Clone + Into<Strand>,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}:", self.refid)?;
Expand All @@ -562,7 +562,11 @@ where
)?;
sep = true;
}
write!(f, "{}", self.strand)
let strand: Strand = self.strand.clone().into();
if !strand.is_unknown() {
write!(f, "({})", self.strand)?;
}
Ok(())
}
}

Expand Down
20 changes: 7 additions & 13 deletions src/strand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,17 @@ impl Same for Strand {

impl Display for Strand {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
Strand::Unknown => Ok(()),
_ => write!(f, "({})", self.strand_symbol()),
}
f.write_str(self.strand_symbol())
}
}

impl FromStr for Strand {
type Err = StrandError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"" => Ok(Strand::Unknown),
"(+)" => Ok(Strand::Forward),
"(-)" => Ok(Strand::Reverse),
"+" | "(+)" => Ok(Strand::Forward),
"-" | "(-)" => Ok(Strand::Reverse),
"." | "" => Ok(Strand::Unknown),
_ => Err(StrandError::ParseError),
}
}
Expand Down Expand Up @@ -200,19 +197,16 @@ impl Same for ReqStrand {

impl Display for ReqStrand {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
ReqStrand::Forward => write!(f, "(+)"),
ReqStrand::Reverse => write!(f, "(-)"),
}
f.write_str(self.strand_symbol())
}
}

impl FromStr for ReqStrand {
type Err = StrandError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"(+)" => Ok(ReqStrand::Forward),
"(-)" => Ok(ReqStrand::Reverse),
"+" | "(+)" => Ok(ReqStrand::Forward),
"-" | "(-)" => Ok(ReqStrand::Reverse),
_ => Err(StrandError::ParseError),
}
}
Expand Down

0 comments on commit 45f518e

Please sign in to comment.