1
- // TODO parse `no_i18n` properly so the user can specify `false`
2
-
3
1
/// An internal macro used for defining a function to get the user's preferred config manager (which requires multiple branches).
4
2
#[ macro_export]
5
3
macro_rules! define_get_config_manager {
@@ -99,15 +97,49 @@ macro_rules! define_get_locales {
99
97
} ;
100
98
}
101
99
100
+ /// An internal macro for defining a function that gets the user's static content aliases (abstracted because it needs multiple
101
+ /// branches).
102
+ #[ macro_export]
103
+ macro_rules! define_get_static_aliases {
104
+ (
105
+ static_aliases: {
106
+ $( $url: literal => $resource: literal) *
107
+ }
108
+ ) => {
109
+ pub fn get_static_aliases( ) -> :: std:: collections:: HashMap <String , String > {
110
+ let mut static_aliases = :: std:: collections:: HashMap :: new( ) ;
111
+ $(
112
+ let resource = $resource. to_string( ) ;
113
+ // We need to move this from being scoped to the app to being scoped for `.perseus/`
114
+ // TODO make sure this works properly on Windows
115
+ let resource = if resource. starts_with( "/" ) {
116
+ // Absolute paths should be left as is
117
+ resource
118
+ } else if resource. starts_with( "./" ) {
119
+ // `./` -> `../`
120
+ format!( ".{}" , resource)
121
+ } else {
122
+ // Anything else (including `../`) gets a `../` prepended
123
+ format!( "../{}" , resource)
124
+ } ;
125
+ static_aliases. insert( $url. to_string( ) , resource) ;
126
+ ) *
127
+ static_aliases
128
+ }
129
+ } ;
130
+ ( ) => {
131
+ pub fn get_static_aliases( ) -> :: std:: collections:: HashMap <String , String > {
132
+ :: std:: collections:: HashMap :: new( )
133
+ }
134
+ } ;
135
+ }
136
+
102
137
/// Defines the components to create an entrypoint for the app. The actual entrypoint is created in the `.perseus/` crate (where we can
103
138
/// get all the dependencies without driving the user's `Cargo.toml` nuts). This also defines the template map. This is intended to make
104
- /// compatibility with the Perseus CLI significantly easier. Perseus makes i18n opt-out, so if you don't intend to use it, set `no_i18n`
105
- /// to `true` in `locales`. Note that you must still specify a default locale for verbosity and correctness. If you specify `no_i18n` and
106
- /// a custom translations manager, the latter will override.
139
+ /// compatibility with the Perseus CLI significantly easier.
107
140
///
108
- /// Warning: all properties must currently be in the correct order (`root`, `templates`, `error_pages`, `locales`, `config_manager`,
109
- /// `translations_manager`).
110
- // TODO make this syntax even more compact and beautiful? (error pages inside templates?)
141
+ /// Warning: all properties must currently be in the correct order (`root`, `templates`, `error_pages`, `locales`, `static_aliases`,
142
+ /// `config_manager`, `translations_manager`).
111
143
#[ macro_export]
112
144
macro_rules! define_app {
113
145
// With locales
@@ -124,6 +156,9 @@ macro_rules! define_app {
124
156
other: [ $( $other_locale: literal) ,* ]
125
157
$( , no_i18n: $no_i18n: literal) ?
126
158
}
159
+ $( , static_aliases: {
160
+ $( $url: literal => $resource: literal) *
161
+ } ) ?
127
162
$( , config_manager: $config_manager: expr) ?
128
163
$( , translations_manager: $translations_manager: expr) ?
129
164
} => {
@@ -140,6 +175,9 @@ macro_rules! define_app {
140
175
// The user doesn't have to define any other locales (but they'll still get locale detection and the like)
141
176
other: [ $( $other_locale) ,* ]
142
177
}
178
+ $( , static_aliases: {
179
+ $( $url => $resource) *
180
+ } ) ?
143
181
$( , config_manager: $config_manager) ?
144
182
$( , translations_manager: $translations_manager) ?
145
183
}
@@ -152,6 +190,9 @@ macro_rules! define_app {
152
190
$( $template: expr) ,+
153
191
] ,
154
192
error_pages: $error_pages: expr
193
+ $( , static_aliases: {
194
+ $( $url: literal => $resource: literal) *
195
+ } ) ?
155
196
$( , config_manager: $config_manager: expr) ?
156
197
$( , translations_manager: $translations_manager: expr) ?
157
198
} => {
@@ -169,6 +210,9 @@ macro_rules! define_app {
169
210
other: [ ] ,
170
211
no_i18n: true
171
212
}
213
+ $( , static_aliases: {
214
+ $( $url => $resource) *
215
+ } ) ?
172
216
$( , config_manager: $config_manager) ?
173
217
$( , translations_manager: $translations_manager) ?
174
218
}
@@ -191,6 +235,9 @@ macro_rules! define_app {
191
235
// If this is defined at all, i18n will be disabled and the default locale will be set to `xx-XX`
192
236
$( , no_i18n: $no_i18n: literal) ?
193
237
}
238
+ $( , static_aliases: {
239
+ $( $url: literal => $resource: literal) *
240
+ } ) ?
194
241
$( , config_manager: $config_manager: expr) ?
195
242
$( , translations_manager: $translations_manager: expr) ?
196
243
}
@@ -235,5 +282,12 @@ macro_rules! define_app {
235
282
pub fn get_error_pages<G : $crate:: GenericNode >( ) -> $crate:: ErrorPages <G > {
236
283
$error_pages
237
284
}
285
+
286
+ /// Gets any static content aliases provided by the user.
287
+ $crate:: define_get_static_aliases!(
288
+ $( static_aliases: {
289
+ $( $url => $resource) *
290
+ } ) ?
291
+ ) ;
238
292
} ;
239
293
}
0 commit comments