1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
//! GFF directive sequence region.
use std::{error, fmt, num, str::FromStr};
use super::PREFIX;
/// A GFF directive sequence region.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SequenceRegion {
reference_sequence_name: String,
start: i32,
end: i32,
}
impl SequenceRegion {
/// Creates a sequence region directive.
///
/// # Examples
///
/// ```
/// use noodles_gff::directive::SequenceRegion;
/// let sequence_region = SequenceRegion::new(String::from("sq0"), 8, 13);
/// ```
pub fn new(reference_sequence_name: String, start: i32, end: i32) -> Self {
Self {
reference_sequence_name,
start,
end,
}
}
/// Returns the reference sequence name.
///
/// # Examples
///
/// ```
/// use noodles_gff::directive::SequenceRegion;
/// let sequence_region = SequenceRegion::new(String::from("sq0"), 8, 13);
/// assert_eq!(sequence_region.reference_sequence_name(), "sq0");
/// ```
pub fn reference_sequence_name(&self) -> &str {
&self.reference_sequence_name
}
/// Returns the start position.
///
/// The start position is 1-based.
///
/// # Examples
///
/// ```
/// use noodles_gff::directive::SequenceRegion;
/// let sequence_region = SequenceRegion::new(String::from("sq0"), 8, 13);
/// assert_eq!(sequence_region.start(), 8);
/// ```
pub fn start(&self) -> i32 {
self.start
}
/// Returns the end position.
///
/// The end position is 1-based.
///
/// # Examples
///
/// ```
/// use noodles_gff::directive::SequenceRegion;
/// let sequence_region = SequenceRegion::new(String::from("sq0"), 8, 13);
/// assert_eq!(sequence_region.end(), 13);
/// ```
pub fn end(&self) -> i32 {
self.end
}
}
impl fmt::Display for SequenceRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}sequence-region {} {} {}",
PREFIX, self.reference_sequence_name, self.start, self.end
)
}
}
/// An error returned when a raw GFF sequence region directive fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
/// The input is empty.
Empty,
/// The reference sequence name is missing.
MissingReferenceSequenceName,
/// The start is missing.
MissingStart,
/// The start is invalid.
InvalidStart(num::ParseIntError),
/// The end is missing.
MissingEnd,
/// The end is invalid.
InvalidEnd(num::ParseIntError),
}
impl error::Error for ParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::InvalidStart(e) | Self::InvalidEnd(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("empty input"),
Self::MissingReferenceSequenceName => f.write_str("missing reference sequence name"),
Self::MissingStart => f.write_str("missing start"),
Self::InvalidStart(_) => f.write_str("invalid start"),
Self::MissingEnd => f.write_str("missing end"),
Self::InvalidEnd(_) => f.write_str("invalid end"),
}
}
}
impl FromStr for SequenceRegion {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Err(ParseError::Empty);
}
let mut args = s.split_ascii_whitespace();
let reference_sequence_name = args
.next()
.ok_or(ParseError::MissingReferenceSequenceName)?;
let start = args
.next()
.ok_or(ParseError::MissingStart)
.and_then(|s| s.parse().map_err(ParseError::InvalidStart))?;
let end = args
.next()
.ok_or(ParseError::MissingEnd)
.and_then(|s| s.parse().map_err(ParseError::InvalidEnd))?;
Ok(Self::new(reference_sequence_name.into(), start, end))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
let sequence_region = SequenceRegion::new(String::from("sq0"), 8, 13);
assert_eq!(sequence_region.to_string(), "##sequence-region sq0 8 13");
}
#[test]
fn test_from_str() -> Result<(), ParseError> {
assert_eq!(
"sq0 8 13".parse::<SequenceRegion>()?,
SequenceRegion::new(String::from("sq0"), 8, 13)
);
assert_eq!("".parse::<SequenceRegion>(), Err(ParseError::Empty));
assert_eq!(
"sq0".parse::<SequenceRegion>(),
Err(ParseError::MissingStart)
);
assert!(matches!(
"sq0 eight".parse::<SequenceRegion>(),
Err(ParseError::InvalidStart(_))
));
assert_eq!(
"sq0 8".parse::<SequenceRegion>(),
Err(ParseError::MissingEnd)
);
assert!(matches!(
"sq0 8 thirteen".parse::<SequenceRegion>(),
Err(ParseError::InvalidEnd(_))
));
Ok(())
}
}