noodles_gff/
line.rs

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
//! GFF line.

mod kind;

use std::io;

pub use self::kind::Kind;
use super::{Directive, Record};

const COMMENT_PREFIX: char = '#';

const DIRECTIVE_START: usize = 2;
const COMMENT_START: usize = 1;

/// A GFF line.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Line(pub(crate) String);

impl Line {
    /// Returns the kind of line.
    pub fn kind(&self) -> Kind {
        if let Some(src) = self.0.strip_prefix(COMMENT_PREFIX) {
            if src.starts_with(COMMENT_PREFIX) {
                Kind::Directive
            } else {
                Kind::Comment
            }
        } else {
            Kind::Record
        }
    }

    /// Returns the line as a directive.
    pub fn as_directive(&self) -> Option<Directive<'_>> {
        match self.kind() {
            Kind::Directive => Some(Directive::new(&self.0[DIRECTIVE_START..])),
            _ => None,
        }
    }

    /// Returns the line as a comment.
    pub fn as_comment(&self) -> Option<&str> {
        match self.kind() {
            Kind::Comment => Some(&self.0[COMMENT_START..]),
            _ => None,
        }
    }

    /// Returns the line as a record.
    pub fn as_record(&self) -> Option<io::Result<Record<'_>>> {
        match self.kind() {
            Kind::Record => Some(Record::try_new(&self.0)),
            _ => None,
        }
    }
}

impl AsRef<str> for Line {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl Default for Line {
    fn default() -> Self {
        Self(String::from("#"))
    }
}