-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.rs
57 lines (54 loc) · 1.85 KB
/
interval.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
use crate::position::{Field, FieldError, Value};
use crate::string::String;
/// Interval type is a simple struct that can be used as a default interval type.
/// It has a chromosome, start, and stop field along with a (linear) HashMap of Values.
use linear_map::LinearMap;
use std::fmt::Debug;
#[derive(Debug, Default)]
pub struct Interval {
pub chrom: String,
pub start: u64,
pub stop: u64,
pub fields: LinearMap<String, Value>,
}
impl Interval {
#[inline]
pub fn start(&self) -> u64 {
self.start
}
#[inline]
pub fn stop(&self) -> u64 {
self.stop
}
#[inline]
pub fn chrom(&self) -> &str {
&self.chrom
}
#[inline]
pub fn value(&self, f: Field) -> Result<Value, FieldError> {
match f {
Field::String(name) => match self.fields.get(&name) {
None => Err(FieldError::InvalidFieldName(name)),
Some(v) => match v {
Value::Strings(s) => Ok(Value::Strings(s.clone())),
Value::Ints(i) => Ok(Value::Ints(i.clone())),
Value::Floats(f) => Ok(Value::Floats(f.clone())),
},
},
Field::Int(i) => {
let name = self.fields.keys().nth(i);
match name {
None => Err(FieldError::InvalidFieldIndex(i)),
Some(name) => match self.fields.get(name) {
None => Err(FieldError::InvalidFieldName(name.clone())),
Some(v) => match v {
Value::Strings(s) => Ok(Value::Strings(s.clone())),
Value::Ints(i) => Ok(Value::Ints(i.clone())),
Value::Floats(f) => Ok(Value::Floats(f.clone())),
},
},
}
}
}
}
}