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
/*!
generating the source codes that is used by [flood-tide](https://crates.io/crates/flood-tide).

# Features

- generate tables that is used by [flood-tide](https://crates.io/crates/flood-tide)
- easy to use by xtask
- parsing text define format file that has command options like help text
- minimum support rustc 1.57.0 (f1edd0429 2021-11-29)

# Todos

- [ ] adding test codes
- [ ] other define format support  (like yaml,json)

# Supports

- [flood-tide](https://crates.io/crates/flood-tide) - command option parsing
- [aki-gsub](https://crates.io/crates/aki-gsub) - the sample used *flood-tide*

*/
mod gen;

#[allow(deprecated, dead_code)]
pub use gen::{gen_src_help, gen_src_match, parse_input_file, update_file, SrcHelpFlags};

use gen::{gen_src_help0, gen_src_match0, parse_input_file0, update_file0, SrcHelpFlags0};
pub use gen::{MetaType, OptStr};

///
/// read input file and generate out help source, match source file.
///
/// # Examples
/// ## Simple Example
///
/// ```rust
/// use flood_tide_gen::{do_gen_src, FixupType, MetaType, Pasc};
///
/// fn do_gen_cmd() -> anyhow::Result<()> {
///     do_gen_src(Pasc::Void, "xtask/aki-gsub-cmd.txt",
///         Some("src/conf/cmd.help.rs.txt"), Some("src/conf/cmd.match.rs.txt"),
///         |opt_str| {
///             let tup = match opt_str.lon_or_sho() {
///                 "color" => (false, false, MetaType::Other("opt_color_when".into())),
///                 "exp" => (false, true, opt_str.meta_type.clone()),
///                 "format" => (false, true, opt_str.meta_type.clone()),
///                 _ => return None,
///             };
///             Some(FixupType::from_tuple(tup))
///         },
///     )?;
///     Ok(())
/// }
/// ```
///
/// ## Fixup Type Example
///
/// ```rust
/// use flood_tide_gen::{do_gen_src, FixupType, MetaType, Pasc, OptStr};
///
/// fn do_gen_cmd() -> anyhow::Result<()> {
///     do_gen_src(Pasc::Void, "xtask/aki-gsub-cmd.txt",
///         Some("src/conf/cmd.help.rs.txt"), Some("src/conf/cmd.match.rs.txt"),
///         do_fix_type
///     )?;
///     Ok(())
/// }
/// pub fn do_fix_type(opt_str: &OptStr) -> Option<FixupType> {
///     let tup = match opt_str.lon_or_sho() {
///         "color" => (false, false, MetaType::Other("opt_color_when".into())),
///         "exp" => (false, true, opt_str.meta_type.clone()),
///         "format" => (false, true, opt_str.meta_type.clone()),
///         _ => return None,
///     };
///     Some(FixupType::from_tuple(tup))
/// }
/// ```
///
pub fn do_gen_src<F>(
    pas: Pasc,
    in_f: &str,
    out_f_help: Option<&str>,
    out_f_match: Option<&str>,
    f: F,
) -> anyhow::Result<()>
where
    F: Fn(&OptStr) -> Option<FixupType>,
{
    let (mut vec_optstr, vec_line) = parse_input_file0(in_f)?;
    //
    fix_type_ary(&mut vec_optstr, f);
    //
    let flags = match pas {
        Pasc::Void => SrcHelpFlags0::default(),
        Pasc::Parent => SrcHelpFlags0 {
            cmd_opt_conf_has_subcmd: true,
            ..Default::default()
        },
        Pasc::Subcmd => SrcHelpFlags0 {
            subcmd_opt_conf: true,
            ..Default::default()
        },
        Pasc::PareSubc => SrcHelpFlags0 {
            cmd_opt_conf_has_subcmd: true,
            subcmd_opt_conf: true,
            ..Default::default()
        },
    };
    let sss = gen_src_help0(&vec_optstr, &vec_line, flags);
    if let Some(s_out_f_help) = out_f_help {
        update_file0(&sss, s_out_f_help)?;
    }
    //
    let sss = gen_src_match0(&vec_optstr);
    if let Some(s_out_f_match) = out_f_match {
        update_file0(&sss, s_out_f_match)?;
    }
    //
    Ok(())
}

fn fix_type_ary<F>(vec_optstr: &mut [OptStr], f: F)
where
    F: Fn(&OptStr) -> Option<FixupType>,
{
    //
    for v in vec_optstr {
        if let Some(vv) = f(v) {
            v.is_opt = vv.is_opt;
            v.is_vec = vv.is_vec;
            v.meta_type = vv.meta_type;
        }
    }
}

/// The parent or subcommand
#[derive(Debug, Clone, Copy)]
pub enum Pasc {
    /// not parent and not subcommand
    Void,
    /// parent of subcommands
    Parent,
    /// subcommand
    Subcmd,
    /// parent and subcommand
    PareSubc,
}

/// Fixup Type of field
#[derive(Debug)]
pub struct FixupType {
    is_opt: bool,
    is_vec: bool,
    meta_type: MetaType,
}
impl FixupType {
    pub fn new(is_opt: bool, is_vec: bool, meta_type: MetaType) -> FixupType {
        FixupType {
            is_opt,
            is_vec,
            meta_type,
        }
    }
    pub fn from_tuple(a: (bool, bool, MetaType)) -> FixupType {
        FixupType {
            is_opt: a.0,
            is_vec: a.1,
            meta_type: a.2,
        }
    }
}
/*
pub fn extern_fix_type(opt_str: &OptStr) -> Option<FixupType> {
    let tup = match opt_str.lon_or_sho() {
        //"pid" => (false, false, MetaType::I32),
        "otype" => (false, false, MetaType::Other("opt_otype_comp_kind".into())),
        "type" => (false, false, MetaType::Other("opt_type_format".into())),
        "from" => (true, false, MetaType::Other("opt_fromto_date".into())),
        "to" => (true, false, MetaType::Other("opt_fromto_date".into())),
        "digits" => (false, false, MetaType::Usize),
        //
        "X" => (false, true, MetaType::Other("opt_uc_x_param".into())),
        _ => return None,
    };
    Some(FixupType::from_tuple(tup))
}
*/