finql_data/
asset.rs

1use super::{DataError, DataItem};
2///! Implementation of a container for basic asset data
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct AssetCategory {
7    id: usize,
8    pub name: String,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize, Default)]
12pub struct Asset {
13    pub id: Option<usize>,
14    pub name: String,
15    pub wkn: Option<String>,
16    pub isin: Option<String>,
17    pub note: Option<String>,
18}
19
20impl Asset {
21    pub fn new(
22        id: Option<usize>,
23        name: &str,
24        wkn: Option<String>,
25        isin: Option<String>,
26        note: Option<String>,
27    ) -> Asset {
28        Asset {
29            id,
30            name: name.to_string(),
31            wkn,
32            isin,
33            note,
34        }
35    }
36}
37
38impl DataItem for Asset {
39    // get id or return error if id hasn't been set yet
40    fn get_id(&self) -> Result<usize, DataError> {
41        match self.id {
42            Some(id) => Ok(id),
43            None => Err(DataError::DataAccessFailure(
44                "tried to get id of temporary asset".to_string(),
45            )),
46        }
47    }
48    // set id or return error if id has already been set
49    fn set_id(&mut self, id: usize) -> Result<(), DataError> {
50        match self.id {
51            Some(_) => Err(DataError::DataAccessFailure(
52                "tried to change valid asset id".to_string(),
53            )),
54            None => {
55                self.id = Some(id);
56                Ok(())
57            }
58        }
59    }
60}