Skip to main content

knuckles_parse/records/
hetnam.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "python")]
5use knuckles_macro::pydefault;
6
7#[cfg(feature = "python")]
8use pyo3::prelude::*;
9
10#[derive(Debug, Clone)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
13#[cfg_attr(feature = "python", pydefault)]
14pub struct HetnamRecord {
15    pub continuation: Option<String>,
16    pub het_id: String,
17    pub text: String,
18}
19
20impl HetnamRecord {
21    pub fn new(line: &str) -> Self {
22        HetnamRecord {
23            continuation: line
24                .get(8..10)
25                .map(|str| str.trim().to_string())
26                .filter(|item| !item.is_empty()),
27            het_id: line[11..15].trim().to_string(),
28            text: line[15..].trim().to_string(), // TODO: This should only parse up to 70 chars but
29                                                 // it may fail if the line is less than 70 chars
30                                                 // using 15..70
31        }
32    }
33}
34
35impl From<&str> for HetnamRecord {
36    fn from(value: &str) -> Self {
37        HetnamRecord::new(value)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_hetnam_new() {
47        let line = "HETNAM     NAG N-ACETYL-D-GLUCOSAMINE";
48        let record = HetnamRecord::new(line);
49        assert_eq!(record.continuation, None);
50        assert_eq!(record.het_id, "NAG");
51        assert_eq!(record.text, "N-ACETYL-D-GLUCOSAMINE");
52
53        let line = "HETNAM  2  SAD DINUCLEOTIDE";
54        let record = HetnamRecord::new(line);
55        assert_eq!(record.continuation, Some("2".to_string()));
56        assert_eq!(record.het_id, "SAD");
57        assert_eq!(record.text, "DINUCLEOTIDE");
58    }
59
60    #[test]
61    fn test_hetnam_from() {
62        let line = "HETNAM     NAG N-ACETYL-D-GLUCOSAMINE";
63        let record = HetnamRecord::from(line);
64        assert_eq!(record.continuation, None);
65        assert_eq!(record.het_id, "NAG");
66        assert_eq!(record.text, "N-ACETYL-D-GLUCOSAMINE");
67        let line = "HETNAM  2  SAD DINUCLEOTIDE";
68        let record = HetnamRecord::from(line);
69        assert_eq!(record.continuation, Some("2".to_string()));
70        assert_eq!(record.het_id, "SAD");
71        assert_eq!(record.text, "DINUCLEOTIDE");
72    }
73}