couchdb_orm/utils/securities/
mod.rs1use 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 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 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}