couchdb_orm/utils/design_docs/
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/design_docs/")
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_design_docs.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_design_docs(rootdir: &PathBuf) -> Result<Vec<Vec<PathBuf>>, Box<dyn std::error::Error>> {
58  db_list(rootdir)?
59    .iter()
60    .map(|db| {
61        let design_docs_path: PathBuf = PathBuf::from_str(&format!(
62            "{}/_meta/src/design_docs/{}",
63            rootdir.to_str().unwrap(),
64            db
65        )).unwrap();
66        Ok(
67          fs::read_dir(&design_docs_path)
68            .map(|res| res.map(|e| e.as_ref().expect("could not read reference").path()))?
69            .filter(|e| e.is_dir())
70            .filter(|e| e.file_name().unwrap() != "mod.rs")
71            .collect()
72        )
73    }).collect()
74}
75
76pub fn available_design_docs_enum(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
77  Ok(
78    available_design_docs(rootdir)?
79        .iter()
80        .map(|a| {
81            a.iter()
82                .map(|e| {
83                    let mut s = e
84                        .file_name()
85                        .unwrap()
86                        .to_str()
87                        .unwrap()
88                        .to_string()
89                        .replace(".rs", ",");
90                    s.push_str("\n");
91                    s
92                })
93                .collect()
94        })
95        .collect()
96  )
97}
98
99pub fn db_actions(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
100  let available_design_docs_enum: Vec<String> = available_design_docs_enum(&rootdir)?;
101  Ok(
102    db_list(&rootdir)?
103        .iter()
104        .enumerate()
105        .map(|(index, db)| {
106            format!(
107                include_str!("../../../static/templates/design_docs_actions.tpl"),
108                db, available_design_docs_enum[index]
109            )
110        })
111        .collect()
112  )
113}
114
115pub fn db_match(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
116  Ok(
117    available_design_docs(rootdir)?
118        .iter()
119        .map(|a| {
120            a.iter()
121                .enumerate()
122                .map(|(index, file)| {
123                    format!(
124                        include_str!("../../../static/templates/design_docs_match.tpl"),
125                        index,
126                        file.file_name()
127                            .unwrap()
128                            .to_str()
129                            .unwrap()
130                            .to_string()
131                            .replace(".rs", "")
132                    )
133                })
134                .collect()
135        })
136        .collect()
137  )
138}
139
140pub fn db_matches(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
141  let db_match = db_match(&rootdir)?;
142  Ok(
143    db_list(&rootdir)?
144        .iter()
145        .enumerate()
146        .map(|(index, db)| {
147            format!(
148                include_str!("../../../static/templates/design_docs_db_matches.tpl"),
149                index,
150                {
151                    // capitalize first letter
152                    let mut c = db.chars();
153                    match c.next() {
154                        None => String::new(),
155                        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
156                    }
157                },
158                db_match[index]
159            )
160        })
161        .collect()
162  )
163}