iptmlib/models/
protein_vec.rs

1// iptmnet A CLI interface to the IPTMNet Rest API
2// Copyright (C) 2022  Ali Sajid Imami
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17use crate::models::Protein;
18use serde::Deserialize;
19use std::fmt::Display;
20
21#[derive(Default, Clone, Eq, PartialEq, Debug, Deserialize)]
22pub struct ProteinVec {
23    proteins: Vec<Protein>,
24    num_results: usize,
25}
26
27#[allow(dead_code)]
28impl ProteinVec {
29    fn new(proteins: Vec<Protein>) -> Self {
30        let num_results = proteins.len();
31        Self {
32            proteins,
33            num_results,
34        }
35    }
36
37    fn add_protein(&mut self, protein: Protein) {
38        self.proteins.push(protein);
39        self.num_results += 1;
40    }
41
42    fn get_proteins(&self) -> &Vec<Protein> {
43        &self.proteins
44    }
45
46    fn get_num_results(&self) -> usize {
47        self.num_results
48    }
49}
50
51impl Display for ProteinVec {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        write!(f, "Protein List: {} results", self.num_results)
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use crate::models::Organism;
61
62    #[test]
63    fn test_protein_vec() {
64        let mut pv = ProteinVec::default();
65        let human = Organism::new("Homo Sapiens", "9606", "Human");
66        assert_eq!(pv.get_num_results(), 0);
67        let p = Protein::new(
68            String::from("PK1IP_HUMAN"),
69            0,
70            human,
71            false,
72            Vec::new(),
73            0,
74            0,
75            false,
76            false,
77            String::from("PK1IP_HUMAN"),
78            0,
79            String::from("PK1IP_HUMAN"),
80            0,
81            String::from("PK1IP_HUMAN"),
82        );
83        pv.add_protein(p);
84        assert_eq!(pv.get_num_results(), 1);
85    }
86
87    #[test]
88    fn test_protein_vec_new() {
89        let human = Organism::new("Homo Sapiens", "9606", "Human");
90        let p = Protein::new(
91            String::from("PK1IP_HUMAN"),
92            0,
93            human,
94            false,
95            Vec::new(),
96            0,
97            0,
98            false,
99            false,
100            String::from("PK1IP_HUMAN"),
101            0,
102            String::from("PK1IP_HUMAN"),
103            0,
104            String::from("PK1IP_HUMAN"),
105        );
106        let pv = ProteinVec::new(vec![p]);
107        assert_eq!(pv.get_num_results(), 1);
108    }
109
110    #[test]
111    fn test_protein_vec_get_proteins() {
112        let human = Organism::new("Homo Sapiens", "9606", "Human");
113        let p = Protein::new(
114            String::from("PK1IP_HUMAN"),
115            0,
116            human,
117            false,
118            Vec::new(),
119            0,
120            0,
121            false,
122            false,
123            String::from("PK1IP_HUMAN"),
124            0,
125            String::from("PK1IP_HUMAN"),
126            0,
127            String::from("PK1IP_HUMAN"),
128        );
129        let pv = ProteinVec::new(vec![p]);
130        assert_eq!(pv.get_proteins().len(), 1);
131    }
132
133    #[test]
134    fn test_protein_vec_display() {
135        let human = Organism::new("Homo Sapiens", "9606", "Human");
136        let p1 = Protein::new(
137            String::from("PK1IP_HUMAN"),
138            0,
139            human,
140            false,
141            Vec::new(),
142            0,
143            0,
144            false,
145            false,
146            String::from("PK1IP_HUMAN"),
147            0,
148            String::from("PK1IP_HUMAN"),
149            0,
150            String::from("PK1IP_HUMAN"),
151        );
152        let human = Organism::new("Homo Sapiens", "9606", "Human");
153        let p2 = Protein::new(
154            String::from("PK2IP_HUMAN"),
155            0,
156            human,
157            false,
158            Vec::new(),
159            0,
160            0,
161            false,
162            false,
163            String::from("PK2IP_HUMAN"),
164            0,
165            String::from("PK2IP_HUMAN"),
166            0,
167            String::from("PK2IP_HUMAN"),
168        );
169        let pv = ProteinVec::new(vec![p1, p2]);
170        assert_eq!(format!("{}", pv), "Protein List: 2 results");
171    }
172}