couchdb_orm/utils/securities/
mod.rs

1// Copyright (C) 2020-2023  OpenToolAdd
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program.  If not, see <http://www.gnu.org/licenses/>.
15// contact: contact@tool-add.com
16
17use std::fs;
18use std::path::{PathBuf};
19use std::str::FromStr;
20
21pub fn root_path(rootdir: &PathBuf) -> PathBuf {
22  rootdir.join("_meta/src/securities/")
23}
24
25pub fn db_list(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
26  Ok(
27    fs::read_dir(root_path(rootdir))?
28      .map(|res| res.expect("path should be a path").path())
29      .filter(|e| e.is_dir())
30      .map(|e| format!("{}", e.file_name().unwrap().to_str().unwrap().to_string()))
31      .collect()
32  )
33}
34
35pub fn dbs(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
36  Ok(
37    db_list(rootdir)?
38    .iter()
39    .map(|db| {
40        format!(
41            include_str!("../../../static/templates/db_securities.tpl"),
42            db,
43            {
44                // capitalize first letter
45                let mut c = db.chars();
46                match c.next() {
47                    None => String::new(),
48                    Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
49                }
50            }
51        )
52    })
53    .collect()
54  )
55}
56
57pub fn available_securities(rootdir: &PathBuf) -> Result<Vec<Vec<PathBuf>>, Box<dyn std::error::Error>> {
58  db_list(rootdir)?
59    .iter()
60    .map(|db| {
61        let securities_path: PathBuf = PathBuf::from_str(&format!(
62            "{}/_meta/src/securities/{}",
63            rootdir.to_str().unwrap(),
64            db
65        )).unwrap();
66        Ok(
67          fs::read_dir(&securities_path)
68            .map(|res| res.map(|e| e.as_ref().expect("could not read reference").path()))
69            .expect("Error in read_dir:  securities_path")
70        )
71    })
72    .map(|a| {
73      a.map(|r|
74        r.filter(|e| e.file_name().unwrap() != "mod.rs")
75        .collect()
76      )
77    }).collect()
78}
79
80pub fn available_securities_enum(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
81  Ok(
82    available_securities(rootdir)?
83        .iter()
84        .map(|a| {
85            a.iter()
86                .map(|e| {
87                    let mut s = e
88                        .file_name()
89                        .unwrap()
90                        .to_str()
91                        .unwrap()
92                        .to_string()
93                        .replace(".rs", ",");
94                    s.push_str("\n");
95                    s
96                })
97                .collect()
98        })
99        .collect()
100  )
101}
102
103pub fn db_actions(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
104  let available_securities_enum: Vec<String> = available_securities_enum(&rootdir)?;
105  Ok(
106    db_list(&rootdir)?
107        .iter()
108        .enumerate()
109        .map(|(index, db)| {
110            format!(
111                include_str!("../../../static/templates/securities_actions.tpl"),
112                db, available_securities_enum[index]
113            )
114        })
115        .collect()
116  )
117}
118
119pub fn db_match(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
120  Ok(
121    available_securities(rootdir)?
122        .iter()
123        .map(|a| {
124            a.iter()
125                .enumerate()
126                .map(|(index, file)| {
127                    format!(
128                        include_str!("../../../static/templates/securities_match.tpl"),
129                        index,
130                        file.file_name()
131                            .unwrap()
132                            .to_str()
133                            .unwrap()
134                            .to_string()
135                            .replace(".rs", "")
136                    )
137                })
138                .collect()
139        })
140        .collect()
141  )
142}
143
144pub fn db_matches(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
145  let db_match = db_match(&rootdir)?;
146  Ok(
147    db_list(&rootdir)?
148        .iter()
149        .enumerate()
150        .map(|(index, db)| {
151            format!(
152                include_str!("../../../static/templates/securities_db_matches.tpl"),
153                index,
154                {
155                    // capitalize first letter
156                    let mut c = db.chars();
157                    match c.next() {
158                        None => String::new(),
159                        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
160                    }
161                },
162                db_match[index]
163            )
164        })
165        .collect()
166  )
167}