-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
137 lines (129 loc) · 3.64 KB
/
build.rs
File metadata and controls
137 lines (129 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use convert_case::{Case, Casing};
use std::io::Write;
use std::{fs::File, io, path::Path};
use serde::Deserialize;
#[derive(Deserialize)]
struct AppPermissions {
app_permissions: Vec<Route>,
}
#[derive(Deserialize)]
struct Route {
route: String,
sub_route: Vec<SubRoute>,
}
#[derive(Deserialize)]
struct SubRoute {
path: String,
permissions: Vec<Permissions>,
}
#[derive(Deserialize)]
struct Permissions {
method: String,
role: String,
}
fn main() -> io::Result<()> {
let out = Path::new("./src").join("application_path_gen.rs");
let mut out = File::create(out).unwrap();
let permission_file = File::open("path_permission_cfg.json").unwrap();
let app_permissions: AppPermissions = serde_json::from_reader(permission_file).unwrap();
writeln!(out, "// auto generated application permissions")?;
writeln!(
out,
"#[macro_export]
macro_rules! impl_application_path {{
($n:ident) => {{
impl $crate::server::path_control::ApplicationPath for $n {{
fn root_path(&self) -> String {{
self.route.clone()
}}
fn inject_auth_router(
self,
router: axum::Router<$crate::server::AppState>,
) -> axum::Router<$crate::server::AppState> {{
let cloned =
std::sync::Arc::new(self) as std::sync::Arc<dyn $crate::server::path_control::ApplicationPath>;
router.route_layer(axum::middleware::from_fn_with_state(
cloned,
$crate::server::middleware::auth,
))
}}
fn get_matcher(
&self,
) -> &matchit::Router<
std::collections::HashMap<axum::http::Method, $crate::db::auth::UserRole>,
> {{
&self.matcher
}}
}}
}};
}}"
)?;
let mut struct_names = vec![];
for route in app_permissions.app_permissions {
let path_struct_name = route.route.trim_start_matches('/').to_case(Case::Pascal)
+ String::from("Path").as_str();
struct_names.push((
route.route.trim_start_matches('/').to_owned(),
path_struct_name.clone(),
));
write!(out, "#[derive(Clone)]
pub struct {path_struct_name} {{
pub route: String,
matcher: matchit::Router<std::collections::HashMap<axum::http::Method, crate::db::auth::UserRole>>
}}
impl Default for {path_struct_name} {{
fn default() -> Self {{
let mut matcher = matchit::Router::new();
")?;
for sub in route.sub_route {
write!(
out,
" matcher
.insert(
\"{}\",
std::collections::HashMap::from([",
sub.path
)?;
for permission in sub.permissions {
write!(
out,
"
(axum::http::Method::{},crate::db::auth::UserRole::{}),",
permission.method,
&permission.role.to_case(Case::Pascal)
)?;
}
writeln!(
out,
"
]),
).unwrap();"
)?;
}
writeln!(
out,
"
Self {{
route: String::from(\"{}\"),
matcher
}}
}}
}}
impl_application_path!({path_struct_name});
",
route.route
)?;
}
write!(
out,
"
#[derive(Default)]
pub struct PrivatePath {{
"
)?;
for (field, struct_name) in struct_names {
writeln!(out, " pub {field}_path:{struct_name},")?;
}
writeln!(out, "}}")?;
Ok(())
}