quickform 0.1.0

A flexible templating and operation execution framework for Rust
Documentation
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! QuickForm: A flexible templating and operation execution framework
//!
//! This library provides a type-safe, state-aware templating system that can execute
//! async operations and render their results using templates. It's designed to be
//! flexible and extensible while maintaining strong type safety.
//!
//! # Examples
//!
//! ```rust
//! use quickform::{App};
//!
//! // Define some state
//! #[derive(Clone)]
//! struct User {
//!     name: String,
//!     age: u32,
//! }
//!
//! // Define an async operation
//! async fn process_user(user: Data<User>) -> String {
//!     format!("Hello, {}!", user.name)
//! }
//!
//! // Create and run the app
//! let app = App::new()
//!     .with_templates("templates/")
//!     .with_state(User {
//!         name: "Alice".to_string(),
//!         age: 30,
//!     })
//!     .operation("greet.txt", process_user);
//! ```
//!
//! # Features
//!
//! - **State Management**: Support for multiple state types using a type-safe API
//! - **Async Operations**: Execute async functions with state-aware parameters
//! - **Template Rendering**: Integrate with template files for output generation
//! - **Builder Pattern**: Fluent API for configuration and setup
//!
//! # Type Parameters
//!
//! - `T`: The type of state stored in the App. Can be:
//!   - `NoData`: For apps with no state
//!   - `Data<S>`: For apps with a single state type
//!   - `(Data<S1>, Data<S2>, ...)`: For apps with multiple state types
mod context;
mod error;
mod fs;
mod loader;
mod operation;
mod state;
mod template;

use std::future::Future;
use std::path::Path;
use std::pin::Pin;
use serde::Serialize;
use tokio::sync::RwLock;
use std::sync::Arc;

use context::Context;
use error::Error;
use fs::MemFS;
use operation::{FunctionSignature, Operation, OperationKind};
use state::{Data, IntoFunctionParams, NoData};
use template::TemplateEngine;

/// A type alias for Results returned by this library
type Result<T> = std::result::Result<T, Error>;

/// The main application struct that manages state, operations, and template rendering
///
/// # Type Parameters
///
/// * `T` - The type of state stored in the App
pub struct App<T> {
    state: T,
    operations: Vec<OperationKind>,
    fs: Arc<RwLock<MemFS>>,
    engine: TemplateEngine<'static>,
}

impl Default for App<NoData> {
    fn default() -> Self {
        Self {
            state: NoData,
            operations: Vec::new(),
            fs: Arc::new(RwLock::new(MemFS::new())),
            engine: TemplateEngine::new(),
        }
    }
}

impl App<NoData> {
    /// Configures the app with templates from a directory
    ///
    /// # Arguments
    ///
    /// * `template_dir` - Path to the directory containing templates
    ///
    /// # Returns
    ///
    /// * `Result<Self>` - The configured App or an error if template loading fails
    pub fn from_dir<P: AsRef<Path>>(template_dir: P) -> Self {
        let fs = MemFS::read_from_disk(template_dir).unwrap_or_default();
        let engine = TemplateEngine::from_memfs(fs.clone());
        Self {
            engine,
            fs: Arc::new(RwLock::new(fs)),
            ..Self::default()
        }
    }

    /// Adds state to the application
    ///
    /// # Type Parameters
    ///
    /// * `S` - The type of state to add
    ///
    /// # Arguments
    ///
    /// * `state` - The state instance to add
    pub fn with_state<S>(self, state: S) -> App<Data<S>> {
        App {
            state: Data::new(state),
            operations: self.operations,
            fs: self.fs,
            engine: self.engine,
        }
    }
}

impl<S1: Send + Sync + 'static> App<Data<S1>> {
    pub fn with_state<S2>(self, state: S2) -> App<(Data<S1>, Data<S2>)> {
        App {
            state: (self.state, Data::new(state)),
            operations: self.operations,
            fs: self.fs,
            engine: self.engine,
        }
    }
}

macro_rules! impl_app_with_state {
    (($($idx:tt),*); $($prev:ident),*; $next:ident) => {
        impl<$($prev: Send + Sync + 'static,)*> App<($(Data<$prev>,)*)> {
            pub fn with_state<$next>(self, state: $next) -> App<($(Data<$prev>,)* Data<$next>)> {
                App {
                    state: ($(self.state.$idx,)* Data::new(state)),
                    operations: self.operations,
                    fs: self.fs,
                    engine: self.engine,
                }
            }
        }
    };
}

impl_app_with_state!((0); S1; S2);
impl_app_with_state!((0, 1); S1, S2; S3);
impl_app_with_state!((0, 1, 2); S1, S2, S3; S4);

impl<T: Send + Sync + Clone + 'static> App<T> {
    /// Registers a render operation with the application
    ///
    /// # Type Parameters
    ///
    /// * `FSig` - The function signature of the operation
    /// * `F` - The operation type
    ///
    /// # Arguments
    ///
    /// * `template_path` - The path to the template file
    /// * `operation` - The operation function to register
    ///
    /// # Returns
    ///
    /// The App instance with the new operation registered
    pub fn render_operation<FSig, F>(mut self, template_path: &str, operation: F) -> Self
    where
        FSig: FunctionSignature + 'static,
        F: Operation<FSig> + Copy + Send + Sync + 'static,
        F::Future: Send + 'static,
        FSig::Output: Serialize,
        T: IntoFunctionParams<FSig>,
    {
        let state = self.state.clone();
        let wrapped_op = move || {
            let params = state.clone().into_params();
            let fut = operation.invoke(params);
            Box::pin(async move {
                let result = fut.await;
                Box::new(result) as Box<dyn Context>
            }) as Pin<Box<dyn Future<Output = _> + Send>>
        };

        self.operations.push(OperationKind::Render(
            template_path.to_string(),
            Box::new(wrapped_op),
        ));
        self
    }

    /// Registers a state operation with the application
    ///
    /// # Type Parameters
    ///
    /// * `FSig` - The function signature of the operation
    /// * `F` - The operation type
    ///
    /// # Arguments
    ///
    /// * `operation` - The operation function to register
    ///
    /// # Returns
    ///
    /// The App instance with the new operation registered
    pub fn state_operation<FSig, F>(mut self, operation: F) -> Self
    where
        FSig: FunctionSignature + 'static,
        F: Operation<FSig> + Copy + Send + Sync + 'static,
        F::Future: Send + 'static,
        FSig::Output: Send + 'static,
        T: IntoFunctionParams<FSig>,
    {
        let state = self.state.clone();
        let wrapped_op = move || {
            let params = state.clone().into_params();
            let fut = operation.invoke(params);
            Box::pin(async move {
                fut.await;
                ()
            }) as Pin<Box<dyn Future<Output = ()> + Send>>
        };

        self.operations.push(OperationKind::State(Box::new(wrapped_op)));
        self
    }

    /// Executes all registered operations and renders their results
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Success or an error if any operation fails
    pub async fn run<P: AsRef<Path>>(&self, output_dir: P) -> Result<()> {
        for operation in &self.operations {
            match operation {
                OperationKind::Render(template_path, op) => {
                    let context = op().await;
                    let rendered = self.engine.render(template_path, &context.to_value())?;
                    self.fs.write().await.write_file(template_path, rendered.as_bytes().to_vec())?;
                }
                OperationKind::State(op) => {
                    op().await;
                }
            }
        }
        
        self.fs.write().await.write_to_disk(output_dir.as_ref())?;
        Ok(())
    }
}

// Test implementation
#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;
    use std::collections::HashMap;

    #[derive(Clone, serde::Serialize)]
    struct User {
        name: String,
        age: u32,
    }

    #[derive(Clone, serde::Serialize)]
    struct Config {
        timeout: Duration,
    }

    #[tokio::test]
    async fn test_no_params() {
        async fn get_default_name() -> HashMap<String, String> {
            let mut map = HashMap::new();
            map.insert("value".to_string(), "Default".to_string());
            map
        }

        let tmp_dir = tempdir::TempDir::new("test").unwrap();
        let template_path = tmp_dir.path().join("get_default.jinja");
        std::fs::write(&template_path, "{{ value }}").unwrap();

        let app = App::from_dir(&tmp_dir.path())
            .render_operation("get_default.jinja", get_default_name);

        let output_dir = tmp_dir.path().join("output");
        app.run(&output_dir).await.unwrap();
        assert!(output_dir.join("get_default.jinja").exists());
        assert_eq!(std::fs::read_to_string(output_dir.join("get_default.jinja")).unwrap(), "Default");
    }

    #[tokio::test]
    async fn test_from_dir() {
        async fn double_age(user: Data<User>) -> User {
            let user = user.clone_inner().await;
            User {
                name: user.name,
                age: user.age * 2,
            }
        }

        async fn codify_name(user: Data<User>) -> User {
            let user = user.clone_inner().await;
            let new_name = user
                .name
                .into_bytes()
                .iter()
                .map(|b| format!("{:02x}", b))
                .collect::<Vec<String>>()
                .join("-");
            User {
                name: new_name,
                age: user.age,
            }
        }

        let tmp_dir = tempdir::TempDir::new("test").unwrap();

        // Create child directory
        let child_dir = tmp_dir.path().join("child");
        std::fs::create_dir(&child_dir).unwrap();

        let template_path_double_age = tmp_dir.path().join("double_age.jinja");
        let template_path_codify_name = child_dir.join("codify_name.jinja");

        std::fs::write(&template_path_double_age, "Age: {{ age }}").unwrap();
        std::fs::write(&template_path_codify_name, "Name: {{ name }}").unwrap();

        let app = App::from_dir(&tmp_dir.path())
            .with_state(User {
                name: "Alice".to_string(),
                age: 30,
            })
            .render_operation("double_age.jinja", double_age)
            .render_operation("child/codify_name.jinja", codify_name);

        let output_dir = tmp_dir.path().join("output");
        app.run(&output_dir).await.unwrap();
        assert!(output_dir.join("double_age.jinja").exists());
        assert_eq!(std::fs::read_to_string(output_dir.join("double_age.jinja")).unwrap(), "Age: 60");
        assert!(output_dir.join("child/codify_name.jinja").exists());
        assert_eq!(std::fs::read_to_string(output_dir.join("child/codify_name.jinja")).unwrap(), "Name: 41-6c-69-63-65");
    }

    #[tokio::test]
    async fn test_multiple_params() {
        async fn get_user_with_timeout(
            user: Data<User>,
            config: Data<Config>,
        ) -> HashMap<String, String> {
            let mut map = HashMap::new();
            map.insert("user".to_string(), user.clone_inner().await.name);
            map.insert("timeout".to_string(), config.clone_inner().await.timeout.as_secs().to_string());
            map
        }

        let tmp_dir = tempdir::TempDir::new("test").unwrap();
        let template_path = tmp_dir.path().join("multiple_params.jinja");
        std::fs::write(&template_path, "{{ timeout }} {{ user }}").unwrap();

        let app = App::from_dir(&tmp_dir.path())
            .with_state(User {
                name: "Bob".to_string(),
                age: 25,
            })
            .with_state(Config {
                timeout: Duration::from_secs(30),
            })
            .render_operation("multiple_params.jinja", get_user_with_timeout);

        let output_dir = tmp_dir.path().join("output");
        app.run(&output_dir).await.unwrap();
        assert!(output_dir.join("multiple_params.jinja").exists());
        assert_eq!(std::fs::read_to_string(output_dir.join("multiple_params.jinja")).unwrap(), "30 Bob");
    }

    #[tokio::test]
    async fn test_simple_params() {
        async fn three_params(x: Data<i32>, y: Data<i32>, z: Data<i32>) -> HashMap<String, i32> {
            let x = x.clone_inner().await;
            let y = y.clone_inner().await;
            let z = z.clone_inner().await;
            let mut map = HashMap::new();
            map.insert("sum".to_string(), x + y + z);
            map
        }

        let tmp_dir = tempdir::TempDir::new("test").unwrap();
        let template_path = tmp_dir.path().join("simple_params.jinja");
        std::fs::write(&template_path, "{{ sum }}").unwrap();

        let app = App::from_dir(&tmp_dir.path())
            .with_state(1)
            .with_state(2)
            .with_state(3)
            .render_operation("simple_params.jinja", three_params);

        let output_dir = tmp_dir.path().join("output");
        app.run(&output_dir).await.unwrap();
        assert!(output_dir.join("simple_params.jinja").exists());
        assert_eq!(std::fs::read_to_string(output_dir.join("simple_params.jinja")).unwrap(), "6");
    }

    #[tokio::test]
    async fn test_state_operation_single_state() {
        let app = App::default()
            .with_state(User {
                name: "Alice".to_string(),
                age: 30,
            })
            .state_operation(|user: Data<User>| async move {
                user.update(|u| u.name = "Bob".to_string()).await;
            });

        // Run the app
        let tmp_dir = tempdir::TempDir::new("test").unwrap();
        app.run(tmp_dir.path()).await.unwrap();

        // Verify the state was updated
        assert_eq!(
            app.state.clone_inner().await.name,
            "Bob"
        );
    }

    #[tokio::test]
    async fn test_state_operation_multiple_states() {
        let app = App::default()
            .with_state(User {
                name: "Alice".to_string(),
                age: 30,
            })
            .with_state(Config {
                timeout: Duration::from_secs(30),
            })
            .state_operation(|user: Data<User>, config: Data<Config>| async move {
                user.update(|u| u.name = "Bob".to_string()).await;
                config.update(|c| c.timeout = Duration::from_secs(60)).await;
            });

        // Run the app
        let tmp_dir = tempdir::TempDir::new("test").unwrap();
        app.run(tmp_dir.path()).await.unwrap();

        // Verify both states were updated
        assert_eq!(
            app.state.0.clone_inner().await.name,
            "Bob"
        );
        assert_eq!(
            app.state.1.clone_inner().await.timeout,
            Duration::from_secs(60)
        );
    }

    #[tokio::test]
    async fn test_state_operation_chain() {
        let app = App::default()
            .with_state(User {
                name: "Alice".to_string(),
                age: 30,
            })
            .state_operation(|user: Data<User>| async move {
                user.update(|u| u.name = "Bob".to_string()).await;
            })
            .state_operation(|user: Data<User>| async move {
                let current = user.clone_inner().await;
                user.update(|u| u.name = format!("{}-modified", current.name)).await;
            });

        // Run the app
        let tmp_dir = tempdir::TempDir::new("test").unwrap();
        app.run(tmp_dir.path()).await.unwrap();

        // Verify the state was updated by both operations
        assert_eq!(
            app.state.clone_inner().await.name,
            "Bob-modified"
        );
    }

    #[tokio::test]
    async fn test_mixed_operations() {
        let tmp_dir = tempdir::TempDir::new("test").unwrap();
        let template_path = tmp_dir.path().join("user.jinja");
        std::fs::write(&template_path, "Name: {{ name }}").unwrap();

        let app = App::from_dir(&tmp_dir.path())
            .with_state(User {
                name: "Alice".to_string(),
                age: 30,
            })
            .state_operation(|user: Data<User>| async move {
                user.update(|u| u.name = "Bob".to_string()).await;
            })
            .render_operation("user.jinja", |user: Data<User>| async move {
                user.clone_inner().await
            });

        let output_dir = tmp_dir.path().join("output");
        app.run(&output_dir).await.unwrap();

        // Verify the state was updated
        assert_eq!(
            app.state.clone_inner().await.name,
            "Bob"
        );

        // Verify the template was rendered with the updated state
        assert_eq!(
            std::fs::read_to_string(output_dir.join("user.jinja")).unwrap(),
            "Name: Bob"
        );
    }
}