pipi-gen 0.1.1

Pipi generators
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// this is because not using with-db renders some of the structs below unused
// TODO: should be more properly aligned with extracting out the db-related gen
// code and then feature toggling it
#![allow(dead_code)]
pub use rrgen::{GenResult, RRgen};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
mod controller;
use colored::Colorize;
use std::fmt::Write;
use std::{
    collections::HashMap,
    fs,
    path::{Path, PathBuf},
    sync::OnceLock,
};

#[cfg(feature = "with-db")]
mod infer;
#[cfg(feature = "with-db")]
mod migration;
#[cfg(feature = "with-db")]
mod model;
#[cfg(feature = "with-db")]
mod scaffold;
pub mod template;
pub mod tera_ext;
#[cfg(test)]
mod testutil;

#[derive(Debug)]
pub struct GenerateResults {
    rrgen: Vec<rrgen::GenResult>,
    local_templates: Vec<PathBuf>,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("{0}")]
    Message(String),
    #[error("template {} not found", path.display())]
    TemplateNotFound { path: PathBuf },
    #[error(transparent)]
    RRgen(#[from] rrgen::Error),
    #[error(transparent)]
    IO(#[from] std::io::Error),
    #[error(transparent)]
    Any(#[from] Box<dyn std::error::Error + Send + Sync>),
}

impl Error {
    pub fn msg(err: impl std::error::Error + Send + Sync + 'static) -> Self {
        Self::Message(err.to_string()) //.bt()
    }
}

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Serialize, Deserialize, Debug)]
struct FieldType {
    name: String,
    rust: RustType,
    schema: String,
    col_type: String,
    #[serde(default)]
    arity: usize,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum RustType {
    String(String),
    Map(HashMap<String, String>),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Mappings {
    field_types: Vec<FieldType>,
}
impl Mappings {
    fn error_unrecognized_default_field(&self, field: &str) -> Error {
        Self::error_unrecognized(field, &self.all_names())
    }

    fn error_unrecognized(field: &str, allow_fields: &[&String]) -> Error {
        Error::Message(format!(
            "type: `{}` not found. try any of: `{}`",
            field,
            allow_fields
                .iter()
                .map(|&s| s.clone())
                .collect::<Vec<String>>()
                .join(",")
        ))
    }

    /// Resolves the Rust type for a given field with optional parameters.
    ///
    /// # Errors
    ///
    /// if rust field not exists or invalid parameters
    pub fn rust_field_with_params(&self, field: &str, params: &Vec<String>) -> Result<&str> {
        match field {
            "array" | "array^" | "array!" => {
                if let RustType::Map(ref map) = self.rust_field_kind(field)? {
                    if let [single] = params.as_slice() {
                        let keys: Vec<&String> = map.keys().collect();
                        Ok(map
                            .get(single)
                            .ok_or_else(|| Self::error_unrecognized(field, &keys))?)
                    } else {
                        Err(self.error_unrecognized_default_field(field))
                    }
                } else {
                    Err(Error::Message(
                        "array field should configured as array".to_owned(),
                    ))
                }
            }

            _ => self.rust_field(field),
        }
    }

    /// Resolves the Rust type for a given field.
    ///
    /// # Errors
    ///
    /// When the given field not recognized
    pub fn rust_field_kind(&self, field: &str) -> Result<&RustType> {
        self.field_types
            .iter()
            .find(|f| f.name == field)
            .map(|f| &f.rust)
            .ok_or_else(|| self.error_unrecognized_default_field(field))
    }

    /// Resolves the Rust type for a given field.
    ///
    /// # Errors
    ///
    /// When the given field not recognized
    pub fn rust_field(&self, field: &str) -> Result<&str> {
        self.field_types
            .iter()
            .find(|f| f.name == field)
            .map(|f| &f.rust)
            .ok_or_else(|| self.error_unrecognized_default_field(field))
            .and_then(|rust_type| match rust_type {
                RustType::String(s) => Ok(s),
                RustType::Map(_) => Err(Error::Message(format!(
                    "type `{field}` need params to get the rust field type"
                ))),
            })
            .map(std::string::String::as_str)
    }

    /// Retrieves the schema field associated with the given field.
    ///
    /// # Errors
    ///
    /// When the given field not recognized
    pub fn schema_field(&self, field: &str) -> Result<&str> {
        self.field_types
            .iter()
            .find(|f| f.name == field)
            .map(|f| f.schema.as_str())
            .ok_or_else(|| self.error_unrecognized_default_field(field))
    }

    /// Retrieves the column type field associated with the given field.
    ///
    /// # Errors
    ///
    /// When the given field not recognized
    pub fn col_type_field(&self, field: &str) -> Result<&str> {
        self.field_types
            .iter()
            .find(|f| f.name == field)
            .map(|f| f.col_type.as_str())
            .ok_or_else(|| self.error_unrecognized_default_field(field))
    }

    /// Retrieves the column type arity associated with the given field.
    ///
    /// # Errors
    ///
    /// When the given field not recognized
    pub fn col_type_arity(&self, field: &str) -> Result<usize> {
        self.field_types
            .iter()
            .find(|f| f.name == field)
            .map(|f| f.arity)
            .ok_or_else(|| self.error_unrecognized_default_field(field))
    }

    #[must_use]
    pub fn all_names(&self) -> Vec<&String> {
        self.field_types.iter().map(|f| &f.name).collect::<Vec<_>>()
    }
}

static MAPPINGS: OnceLock<Mappings> = OnceLock::new();

/// Get type mapping for generation
///
/// # Panics
///
/// Panics if loading fails
pub fn get_mappings() -> &'static Mappings {
    MAPPINGS.get_or_init(|| {
        let json_data = include_str!("./mappings.json");
        serde_json::from_str(json_data).expect("JSON was not well-formatted")
    })
}

#[derive(clap::ValueEnum, Clone, Debug)]
pub enum ScaffoldKind {
    Api,
    Html,
    Htmx,
}

#[derive(Debug, Clone)]
pub enum DeploymentKind {
    Docker {
        copy_paths: Vec<PathBuf>,
        is_client_side_rendering: bool,
    },
    Nginx {
        host: String,
        port: i32,
    },
}

#[derive(Debug)]
pub enum Component {
    #[cfg(feature = "with-db")]
    Model {
        /// Name of the thing to generate
        name: String,

        /// Whether to include timestamps (`created_at``updated_at`at columns) in the model
        with_tz: bool,

        /// Model fields, eg. title:string hits:int
        fields: Vec<(String, String)>,
    },
    #[cfg(feature = "with-db")]
    Migration {
        /// Name of the migration file
        name: String,

        /// Whether to include timestamps (`created_at`, `updated_at` columns) in the migration
        with_tz: bool,

        /// Params fields, eg. title:string hits:int
        fields: Vec<(String, String)>,
    },
    #[cfg(feature = "with-db")]
    Scaffold {
        /// Name of the thing to generate
        name: String,

        /// Whether to include timestamps (`created_at``updated_at`at columns) in the scaffold
        with_tz: bool,

        /// Model and params fields, eg. title:string hits:int
        fields: Vec<(String, String)>,

        // k
        kind: ScaffoldKind,
    },
    Controller {
        /// Name of the thing to generate
        name: String,

        /// Action names
        actions: Vec<String>,

        // kind
        kind: ScaffoldKind,
    },
    Task {
        /// Name of the thing to generate
        name: String,
    },
    Scheduler {},
    Worker {
        /// Name of the thing to generate
        name: String,
    },
    Mailer {
        /// Name of the thing to generate
        name: String,
    },
    Data {
        /// Name of the thing to generate
        name: String,
    },
    Deployment {
        kind: DeploymentKind,
    },
}

pub struct AppInfo {
    pub app_name: String,
}

#[must_use]
pub fn new_generator() -> RRgen {
    RRgen::default().add_template_engine(tera_ext::new())
}

/// Generate a component
///
/// # Errors
///
/// This function will return an error if it fails
pub fn generate(rrgen: &RRgen, component: Component, appinfo: &AppInfo) -> Result<GenerateResults> {
    /*
    (1)
    XXX: remove hooks generic from child generator, materialize it here and pass it
         means each generator accepts a [component, config, context] tuple
         this will allow us to test without an app instance
    (2) proceed to test individual generators
     */
    let get_result = match component {
        #[cfg(feature = "with-db")]
        Component::Model {
            name,
            with_tz,
            fields,
        } => model::generate(rrgen, &name, with_tz, &fields, appinfo)?,
        #[cfg(feature = "with-db")]
        Component::Scaffold {
            name,
            with_tz,
            fields,
            kind,
        } => scaffold::generate(rrgen, &name, with_tz, &fields, &kind, appinfo)?,
        #[cfg(feature = "with-db")]
        Component::Migration {
            name,
            with_tz,
            fields,
        } => migration::generate(rrgen, &name, with_tz, &fields, appinfo)?,
        Component::Controller {
            name,
            actions,
            kind,
        } => controller::generate(rrgen, &name, &actions, &kind, appinfo)?,
        Component::Task { name } => {
            let vars = json!({"name": name, "pkg_name": appinfo.app_name});
            render_template(rrgen, Path::new("task"), &vars)?
        }
        Component::Scheduler {} => {
            let vars = json!({"pkg_name": appinfo.app_name});
            render_template(rrgen, Path::new("scheduler"), &vars)?
        }
        Component::Worker { name } => {
            let vars = json!({"name": name, "pkg_name": appinfo.app_name});
            render_template(rrgen, Path::new("worker"), &vars)?
        }
        Component::Mailer { name } => {
            let vars = json!({ "name": name });
            render_template(rrgen, Path::new("mailer"), &vars)?
        }
        Component::Deployment { kind } => match kind {
            DeploymentKind::Docker {
                copy_paths,
                is_client_side_rendering,
            } => {
                let vars = json!({
                    "pkg_name": appinfo.app_name,
                    "copy_paths": copy_paths,
                    "is_client_side_rendering": is_client_side_rendering,
                });
                render_template(rrgen, Path::new("deployment/docker"), &vars)?
            }
            DeploymentKind::Nginx { host, port } => {
                let host = host.replace("http://", "").replace("https://", "");
                let vars = json!({
                    "pkg_name": appinfo.app_name,
                    "domain": host,
                    "port": port
                });
                render_template(rrgen, Path::new("deployment/nginx"), &vars)?
            }
        },
        Component::Data { name } => {
            let vars = json!({ "name": name });
            render_template(rrgen, Path::new("data"), &vars)?
        }
    };

    Ok(get_result)
}

fn render_template(rrgen: &RRgen, template: &Path, vars: &Value) -> Result<GenerateResults> {
    let template_files = template::collect_files_from_path(template)?;

    let mut gen_result = vec![];
    let mut local_templates = vec![];
    for template in template_files {
        let custom_template = Path::new(template::DEFAULT_LOCAL_TEMPLATE).join(template.path());

        if custom_template.exists() {
            let content = fs::read_to_string(&custom_template).map_err(|err| {
                tracing::error!(custom_template = %custom_template.display(), "could not read custom template");
                err
            })?;
            gen_result.push(rrgen.generate(&content, vars)?);
            local_templates.push(custom_template);
        } else {
            let content = template.contents_utf8().ok_or(Error::Message(format!(
                "could not get template content: {}",
                template.path().display()
            )))?;
            gen_result.push(rrgen.generate(content, vars)?);
        }
    }

    Ok(GenerateResults {
        rrgen: gen_result,
        local_templates,
    })
}

#[must_use]
pub fn collect_messages(results: &GenerateResults) -> String {
    let mut messages = String::new();

    for res in &results.rrgen {
        if let rrgen::GenResult::Generated {
            message: Some(message),
        } = res
        {
            let _ = writeln!(messages, "* {message}");
        }
    }

    if !results.local_templates.is_empty() {
        let _ = writeln!(messages);
        let _ = writeln!(
            messages,
            "{}",
            "The following templates were sourced from the local templates:".green()
        );

        for f in &results.local_templates {
            let _ = writeln!(messages, "* {}", f.display());
        }
    }
    messages
}

/// Copies template files to a specified destination directory.
///
/// This function copies files from the specified template path to the
/// destination directory. If the specified path is `/` or `.`, it copies all
/// files from the templates directory. If the path does not exist in the
/// templates, it returns an error.
///
/// # Errors
/// when could not copy the given template path
pub fn copy_template(path: &Path, to: &Path) -> Result<Vec<PathBuf>> {
    let copy_template_path = if path == Path::new("/") || path == Path::new(".") {
        None
    } else if !template::exists(path) {
        return Err(Error::TemplateNotFound {
            path: path.to_path_buf(),
        });
    } else {
        Some(path)
    };

    let copy_files = if let Some(path) = copy_template_path {
        template::collect_files_from_path(path)?
    } else {
        template::collect_files()
    };

    let mut copied_files = vec![];
    for f in copy_files {
        let copy_to = to.join(f.path());
        if copy_to.exists() {
            tracing::debug!(
                template_file = %copy_to.display(),
                "skipping copy template file. already exists"
            );
            continue;
        }
        match copy_to.parent() {
            Some(parent) => {
                fs::create_dir_all(parent)?;
            }
            None => {
                return Err(Error::Message(format!(
                    "could not get parent folder of {}",
                    copy_to.display()
                )))
            }
        }

        fs::write(&copy_to, f.contents())?;
        tracing::trace!(
            template = %copy_to.display(),
            "copy template successfully"
        );
        copied_files.push(copy_to);
    }
    Ok(copied_files)
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::*;

    #[test]
    fn test_template_not_found() {
        let tree_fs = tree_fs::TreeBuilder::default()
            .drop(true)
            .create()
            .expect("create temp file");
        let path = Path::new("nonexistent-template");

        let result = copy_template(path, tree_fs.root.as_path());
        assert!(result.is_err());
        if let Err(Error::TemplateNotFound { path: p }) = result {
            assert_eq!(p, path.to_path_buf());
        } else {
            panic!("Expected TemplateNotFound error");
        }
    }

    #[test]
    fn test_copy_template_valid_folder_template() {
        let temp_fs = tree_fs::TreeBuilder::default()
            .drop(true)
            .create()
            .expect("Failed to create temporary file system");

        let template_dir = template::tests::find_first_dir();

        let copy_result = copy_template(template_dir.path(), temp_fs.root.as_path());
        assert!(
            copy_result.is_ok(),
            "Failed to copy template from directory {:?}",
            template_dir.path()
        );

        let template_files = template::collect_files_from_path(template_dir.path())
            .expect("Failed to collect files from the template directory");

        assert!(
            !template_files.is_empty(),
            "No files found in the template directory"
        );

        for template_file in template_files {
            let copy_file_path = temp_fs.root.join(template_file.path());

            assert!(
                copy_file_path.exists(),
                "Copy file does not exist: {copy_file_path:?}"
            );

            let copy_content =
                fs::read_to_string(&copy_file_path).expect("Failed to read coped file content");

            assert_eq!(
                template_file
                    .contents_utf8()
                    .expect("Failed to get template file content"),
                copy_content,
                "Content mismatch in file: {copy_file_path:?}"
            );
        }
    }

    fn test_mapping() -> Mappings {
        Mappings {
            field_types: vec![
                FieldType {
                    name: "array".to_string(),
                    rust: RustType::Map(HashMap::from([
                        ("string".to_string(), "Vec<String>".to_string()),
                        ("chat".to_string(), "Vec<String>".to_string()),
                        ("int".to_string(), "Vec<i32>".to_string()),
                    ])),
                    schema: "array".to_string(),
                    col_type: "array_null".to_string(),
                    arity: 1,
                },
                FieldType {
                    name: "string^".to_string(),
                    rust: RustType::String("String".to_string()),
                    schema: "string_uniq".to_string(),
                    col_type: "StringUniq".to_string(),
                    arity: 0,
                },
            ],
        }
    }

    #[test]
    fn can_get_all_names_from_mapping() {
        let mapping = test_mapping();
        assert_eq!(
            mapping.all_names(),
            Vec::from([&"array".to_string(), &"string^".to_string()])
        );
    }

    #[test]
    fn can_get_col_type_arity_from_mapping() {
        let mapping = test_mapping();

        assert_eq!(mapping.col_type_arity("array").expect("Get array arity"), 1);
        assert_eq!(
            mapping
                .col_type_arity("string^")
                .expect("Get string^ arity"),
            0
        );

        assert!(mapping.col_type_arity("unknown").is_err());
    }

    #[test]
    fn can_get_col_type_field_from_mapping() {
        let mapping = test_mapping();

        assert_eq!(
            mapping.col_type_field("array").expect("Get array field"),
            "array_null"
        );

        assert!(mapping.col_type_field("unknown").is_err());
    }

    #[test]
    fn can_get_schema_field_from_mapping() {
        let mapping = test_mapping();

        assert_eq!(
            mapping.schema_field("string^").expect("Get string^ schema"),
            "string_uniq"
        );

        assert!(mapping.schema_field("unknown").is_err());
    }

    #[test]
    fn can_get_rust_field_from_mapping() {
        let mapping = test_mapping();

        assert_eq!(
            mapping
                .rust_field("string^")
                .expect("Get string^ rust field"),
            "String"
        );

        assert!(mapping.rust_field("array").is_err());

        assert!(mapping.rust_field("unknown").is_err(),);
    }

    #[test]
    fn can_get_rust_field_kind_from_mapping() {
        let mapping = test_mapping();

        assert!(mapping.rust_field_kind("string^").is_ok());

        assert!(mapping.rust_field_kind("unknown").is_err(),);
    }

    #[test]
    fn can_get_rust_field_with_params_from_mapping() {
        let mapping = test_mapping();

        assert_eq!(
            mapping
                .rust_field_with_params("string^", &vec!["string".to_string()])
                .expect("Get string^ rust field"),
            "String"
        );

        assert_eq!(
            mapping
                .rust_field_with_params("array", &vec!["string".to_string()])
                .expect("Get string^ rust field"),
            "Vec<String>"
        );
        assert!(mapping
            .rust_field_with_params("array", &vec!["unknown".to_string()])
            .is_err());

        assert!(mapping.rust_field_with_params("unknown", &vec![]).is_err());
    }

    #[test]
    fn can_collect_messages() {
        let gen_result = GenerateResults {
            rrgen: vec![
                GenResult::Skipped,
                GenResult::Generated {
                    message: Some("test".to_string()),
                },
                GenResult::Generated {
                    message: Some("test2".to_string()),
                },
                GenResult::Generated { message: None },
            ],
            local_templates: vec![
                PathBuf::from("template").join("scheduler.t"),
                PathBuf::from("template").join("task.t"),
            ],
        };

        let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();

        assert_eq!(
            re.replace_all(&collect_messages(&gen_result), ""),
            r"* test
* test2

The following templates were sourced from the local templates:
* template/scheduler.t
* template/task.t
"
        );
    }
}