insrcdata 0.2.0

Embed static data as source code
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
// insrcdata : embed tabular data in source code (https://github.com/sebkeim/insrcdata)
// Copyright (c)  2023 Sébastien Keim
// SPDX-License-Identifier: GPL-3.0-or-later
//
// target implementation for Rust language
//

use crate::basetype::BaseType;
use crate::table::JoinTo;
use crate::{aperror, basetype, language, log, table};
use heck::{ToShoutySnakeCase, ToSnakeCase, ToUpperCamelCase};
use std::{fs, io};

struct Rust {}

// rust data types
fn strtype(typ: &basetype::BaseType) -> String {
    String::from(match typ {
        BaseType::Label { name } => return struct_name(name),
        BaseType::Bool => "bool",
        BaseType::I8 => "i8",
        BaseType::I16 => "i16",
        BaseType::I32 => "i32",
        BaseType::I64 => "i64",
        BaseType::U8 => "u8",
        BaseType::U16 => "u16",
        BaseType::U32 => "u32",
        BaseType::U64 => "u64",
        BaseType::Str => "&'static str",
        BaseType::F32 => "f32",
        BaseType::F64 => "f64",
        BaseType::Object { objtype } => objtype,
        BaseType::Join { .. } => "TODO",
        BaseType::Variant => "TODO",
    })
}
fn argtype(typ: &basetype::BaseType) -> String {
    match typ {
        basetype::BaseType::Str => "& str".to_string(),
        _ => strtype(typ),
    }
}
fn modtype(typ: &basetype::BaseType) -> String {
    strtype(typ)
}

// ================================================================================================
// format name to rust conventions
// ================================================================================================
fn struct_name(table_name: &str) -> String {
    table_name.to_upper_camel_case()
}
fn const_name(table_name: &str) -> String {
    table_name.to_shouty_snake_case()
}
fn mod_name(table_name: &str) -> String {
    table_name.to_snake_case()
}
fn field_name(col_name: &str) -> String {
    col_name.to_snake_case()
}
fn table_name(strname: &str) -> String {
    format!("{}::TABLE", mod_name(strname))
}
fn index_name(strname: &str, field: &str) -> String {
    format!("{}::{}_INDEX", mod_name(strname), const_name(field))
}

// ================================================================================================
// Getters
// ================================================================================================
fn cast_to_interface_type(info: &table::ColumnInfo) -> String {
    if info.interface_type == info.table_type {
        String::from("")
    } else {
        format!(" as {}", strtype(&info.interface_type))
    }
}

fn getter_col(
    table: &table::Table,
    col: &dyn table::Column,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let info = col.info();
    let field = field_name(col.name());
    match &info.type_impl() {
        table::TypeImpl::Label => {
            let outtype = strtype(&info.interface_type);
            writeln!(
                output,
                "    pub fn {field}(&self) -> &{outtype} {{ &self.{field}_}}",
            )?;
        }
        table::TypeImpl::Scalar => {
            let outtype = strtype(&info.interface_type);
            let cast = cast_to_interface_type(info);
            writeln!(
                output,
                "    pub fn {field}(&self) -> {outtype} {{ self.{field}_{cast} }}",
            )?;
        }
        table::TypeImpl::Join => {
            let outtype = struct_name(&info.join_table());
            let jointable = table_name(&outtype);
            writeln!(
                output,
                "    pub fn {field}(&self) -> &'static {outtype} {{ &{jointable}[self.{field}_ as usize]}}"
            )?;
        }
        table::TypeImpl::JoinOptional => {
            let outtype = struct_name(&info.join_table());
            let jointable = table_name(&outtype);
            writeln!(
                output,
                "    pub fn {field}(&self) -> Option<&'static {outtype}> {{
        let index = self.{field}_;
        if index==0 {{ None }} else {{ Some(&{jointable}[index as usize -1]) }}
    }}"
            )?;
        }
        table::TypeImpl::Variant => {
            getter_variant(table, col, output)?;
        }
    }
    Ok(())
}

// ================================================================================================
// Iterator definition
// ================================================================================================
fn write_iter_index_struct(
    table: &table::Table,
    strname: &String,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let indextype = strtype(&table.index_type());

    writeln!(
        output,
        "pub struct IndexIter {{
    pub indexes : Box<dyn Iterator<Item=&'static {indextype}>>,
}}

impl Iterator for IndexIter {{
    type Item = & 'static {strname};

    fn next(&mut self) -> Option<&'static {strname}> {{
        let idx = self.indexes.next();
        match idx {{
            Some(v) => Some(&TABLE[*v as usize]),
            None => None,
        }}
    }}
}}

"
    )
}

// ================================================================================================
// Range iterator
// ================================================================================================
fn iter_col(
    table: &table::Table,
    col: &dyn table::Column,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let info = col.info();

    let field = field_name(col.name());
    let argtype = argtype(&info.interface_type);
    let modname = mod_name(&table.name);
    let tablename = table_name(&table.name);
    let indexname = index_name(&table.name, col.name());
    let cast = cast_to_interface_type(info);

    writeln!(
        output,
        "
    pub fn {field}_range(start:{argtype}, stop:{argtype}) -> {modname}::IndexIter {{
        let mut lo = 0;
        let mut hi = {indexname}.len();
        while lo < hi {{
            let mid = (lo + hi) / 2;
            if start > {tablename}[{indexname}[mid] as usize].{field}_{cast} {{
                 lo = mid + 1;
            }} else {{
                 hi = mid;
            }}
        }}

        let begin = lo;
        hi = {indexname}.len();
        while lo < hi {{
            let mid = (lo + hi) / 2;
            if stop < {tablename}[{indexname}[mid] as usize].{field}_{cast} {{
                hi = mid;
            }} else {{
                lo = mid + 1;
            }}
        }}
        {modname}::IndexIter {{
            indexes: Box::new({indexname}[begin..lo].iter()),
        }}
    }}"
    )
}

// ================================================================================================
// Reverse join
// ================================================================================================
fn reverse_join(table: &table::Table, rj: &JoinTo, output: &mut dyn io::Write) -> io::Result<()> {
    if !table.has_data() {
        log::warning(&format!("{} will crash if used", &rj.reverse_name));
    }

    let info = rj.col.info();
    let field = field_name(rj.col.name());
    let reverse = &rj.reverse_name;
    let srcmod = mod_name(&rj.table.name);
    let srcstruct = struct_name(&rj.table.name);
    let joinmod = mod_name(&table.name);
    let tabletype = strtype(&info.table_type);
    let offset = stroffset(rj.offset as isize);
    let indexname = index_name(&rj.table.name, rj.col.name());

    writeln!(
        output,
        "
    pub fn {reverse}(&self) -> {srcstruct}Iter {{
        let cons = {joinmod}::index_of(self) as {tabletype}{offset};

        // bissect left
        let mut lo = 0;
        let mut hi = {indexname}.len();
        while lo < hi {{
            let mid = (lo + hi) / 2;
            if cons > {srcmod}::TABLE[{indexname}[mid] as usize].{field}_ {{
                lo = mid + 1;
            }} else {{
                hi = mid;
            }}
        }}
        let start = lo;

        // bissect-right
        hi = {indexname}.len();
        while lo < hi {{
            let mid = (lo + hi) / 2;
            if cons < {srcmod}::TABLE[{indexname}[mid] as usize].{field}_  {{
                hi = mid;
            }} else {{
                lo = mid + 1;
            }}
        }}

        {srcmod}::IndexIter {{
            indexes: Box::new({indexname}[start..lo].iter()),
        }}
    }}"
    )
}

// ================================================================================================
// Labels
// ================================================================================================
fn col_labels(
    table: &table::Table,
    col: &dyn table::Column,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let info = col.info();
    let enumname = strtype(&info.interface_type);
    write!(
        output,
        "#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]\npub enum {enumname} {{\n"
    )?;

    for row in 0..info.len {
        let label = col.emit_label(row);
        if !label.is_empty() {
            let upper_label = label.to_upper_camel_case();
            let help = col.emit_label_help(row);
            if help.is_empty() {
                writeln!(output, "    {upper_label} = {row},")?;
            } else {
                writeln!(output, "    /// {help}\n    {upper_label} = {row},")?;
            };
        }
    }
    writeln!(output, "}}")?;

    let strname = struct_name(&table.name);
    let modname = mod_name(&table.name);
    if table.has_data() {
        writeln!(
            output,
            "impl From<{enumname}> for  &'static {strname}{{
    fn from(value:{enumname}) -> Self {{
        &{modname}::TABLE[value as usize]
    }}
}}
impl From<&{enumname}> for  &'static {strname}{{
    fn from(value: &{enumname}) -> Self {{
        &{modname}::TABLE[*value as usize]
    }}
}}
impl PartialEq<{enumname}> for &{strname} {{
    fn eq(&self, other: &{enumname}) -> bool {{
        std::ptr::eq(<&{strname}>::from(other), *self)
    }}
}}
"
        )?;
    }
    Ok(())
}

// ================================================================================================
// Indexes
// ================================================================================================
fn write_index(
    table: &table::Table,
    col: &dyn table::Column,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let indexes = col.indexes();
    let uperfield = const_name(col.name());
    let indextype = strtype(&table.index_type());
    let len = indexes.len();

    write!(
        output,
        "pub static {uperfield}_INDEX : [ {indextype} ; {len} ] = ["
    )?;

    let width = language::digits(indexes.len());
    for (i, v) in indexes.iter().enumerate() {
        if i % 20 == 0 {
            write!(output, "\n    ")?;
        }
        write!(output, "{:width$}, ", v)?;
    }
    write!(output, "\n];\n")
}

// ================================================================================================
// Variants
// ================================================================================================
fn variant_type_name(table: &table::Table, col: &dyn table::Column) -> String {
    let strname = struct_name(&table.name);
    let varname = struct_name(col.name());
    format!("{strname}{varname}")
}
fn write_variant(
    table: &table::Table,
    col: &dyn table::Column,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let Some(variants) = col.variants() else {
        return Ok(());
    };
    let vartypname = variant_type_name(table, col);

    writeln!(
        output,
        "#[derive(Clone, Copy, PartialEq, Eq, Hash)]\npub enum  {vartypname} {{"
    )?;
    for vrn in variants {
        let joinstruct = struct_name(&vrn.name);
        if vrn.is_none {
            writeln!(output, "     {joinstruct},")?;
        } else {
            writeln!(output, "     {joinstruct}(&'static {joinstruct}),")?;
        }
    }

    writeln!(output, "}}\n")
}

fn stroffset(v: isize) -> String {
    match { v } {
        0 => "".to_string(),
        v if v < 0 => format!(" - {}", -v),
        _ => format!(" + {}", v),
    }
}

fn getter_variant(
    table: &table::Table,
    col: &dyn table::Column,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let variants = col.variants().expect("variant must have variant");
    let vartypname = variant_type_name(table, col);
    let field = field_name(col.name());

    writeln!(
        output,
        "    pub fn {field}(&self) -> {vartypname} {{ 
        let v = self.{field}_ ;
        match v {{"
    )?;

    for vrn in variants {
        if vrn.count == 0 {
            continue;
        }
        let start = vrn.index;
        let end = start + vrn.count - 1;
        let jointable = table_name(&vrn.name);
        let joinstruct = struct_name(&vrn.name);
        if vrn.is_none {
            writeln!(
                output,
                "             {start}..={end} => {vartypname}::{joinstruct},"
            )?;
        } else {
            let offset = stroffset(-(start as isize));
            writeln!(output, "             {start}..={end} => {vartypname}::{joinstruct}(&{jointable}[v as usize {offset}]),")?;
        }
    }

    writeln!(
        output,
        "             _ => panic!(\"insrcdata variant index overflow\"),\n        }}\n    }}"
    )
}

// ================================================================================================
// Table
// ================================================================================================
// define ctor fuction  : const fn r(hello:u8, ) -> Table1 { return Table1{hello_:hello, }; }
fn write_ctor_function(
    strname: &String,
    outcols: &Vec<&dyn table::Column>,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    // interface
    write!(output, "const fn r(")?;
    for col in outcols {
        let info = col.info();
        let typ = modtype(&info.table_type);
        write!(output, "{}:{}, ", field_name(col.name()), typ)?;
    }
    write!(output, ") -> {} ", strname)?;

    // body
    write!(output, "{{\n    {}{{", strname)?;
    for col in outcols {
        let field = field_name(col.name());
        write!(output, "{field}_:{field}, ")?;
    }
    write!(output, "}}\n}}\n\n")
}

fn table_data(
    project: &table::Project,
    table: &table::Table,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    let strname = struct_name(&table.name);
    let modname = mod_name(&strname);

    // define structure
    let datacols: Vec<&dyn table::Column> = table.data_columns();

    writeln!(output, "pub struct {strname} {{")?;
    for col in &datacols {
        let info = col.info();
        let fieldtype = strtype(&info.table_type);
        writeln!(output, "    {}_ : {},", field_name(col.name()), fieldtype)?;
    }
    writeln!(
        output,
        "}}
impl PartialEq<Self> for {strname} {{
    fn eq(&self, other: &Self) -> bool {{
        std::ptr::eq(self, other)
    }}
}}
impl Eq for {strname} {{}}
impl std::hash::Hash for {strname} {{
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {{
        {modname}::index_of(self).hash(state);
    }}
}}
"
    )?;

    // stucture implementation
    writeln!(output, "impl {} {{", strname)?;

    // data column
    for col in &datacols {
        getter_col(table, *col, output)?;
        if col.info().has_iter_range() {
            iter_col(table, *col, output)?;
        }
    }

    let joins_to = project.join_to_columns(table);
    for rj in joins_to {
        reverse_join(table, &rj, output)?;
    }

    if table.get_array {
        let srcstruct = struct_name(&table.name);
        let tablelen = table.len;
        let srcmod = mod_name(&table.name);
        writeln!(
            output,
            "    pub fn array() -> &'static [{srcstruct}; {tablelen}] {{ &{srcmod}::TABLE }}
    pub fn as_index(&self) -> usize {{ {modname}::index_of(self) }}",
        )?;
    }
    write!(output, "}}\n\n")?;

    // begin module private
    writeln!(
        output,
        "mod {modname} {{\
use super::*;"
    )?;
    for import in table.imports() {
        writeln!(output, "use {import};")?;
    }
    writeln!(output,"
pub fn index_of(fic:&{strname}) -> usize {{
    ((fic  as *const _ as usize) - (&TABLE[0]  as *const _ as usize)) / std::mem::size_of::<{strname}>()
}}", )?;

    if project.table_need_iter(table) {
        write_iter_index_struct(table, &strname, output)?;
    }
    write_ctor_function(&strname, &datacols, output)?;

    // table data
    writeln!(
        output,
        "pub static TABLE : [ {} ; {} ] = [",
        strname, table.len
    )?;
    for row in 0..table.len {
        write!(output, "   {{r(")?;
        for col in &datacols {
            write!(output, "{}, ", col.emit_table_cell(row, project.lang))?;
        }
        writeln!(output, ")}},")?;
    }
    writeln!(output, "];")?;

    // indexes
    for col in &datacols {
        if col.iterable() {
            write_index(table, *col, output)?;
        }
    }
    writeln!(output, "\n}} // mod {}\n", modname)?;

    //export
    if project.table_need_iter(table) {
        writeln!(output, "pub use {modname}::IndexIter as {strname}Iter;",)?;
    }

    //
    for col in datacols {
        write_variant(table, col, output)?;
    }

    Ok(())
}

fn emit_table(
    project: &table::Project,
    table: &table::Table,
    output: &mut dyn io::Write,
) -> io::Result<()> {
    // Labels
    let labelcols: Vec<&dyn table::Column> = table.label_columns();
    for col in labelcols {
        col_labels(table, col, output)?;
    }
    if table.has_data() {
        table_data(project, table, output)?;
    }
    Ok(())
}

// ================================================================================================
// Entry point
// ================================================================================================
impl language::Language for Rust {
    fn emit(&self, project: &table::Project) -> aperror::Result<()> {
        let mut outfile =
            aperror::io_error_result(fs::File::create(&project.dst_path), &project.dst_path)?;
        let output = (&mut outfile) as &mut dyn io::Write;
        let notice = language::file_notice();
        writeln!(output, "// {notice}\n")?;

        for table in &project.tables {
            emit_table(project, table, output)?;
        }
        Ok(())
    }

    fn extension(&self) -> String {
        "rs".to_string()
    }

    // support tolabel for label format
    fn to_label(&self) -> bool {
        true
    }

    fn emit_enum(&self, typ: &BaseType, label: &str) -> String {
        let enumstr = strtype(typ);
        let camel = label.to_upper_camel_case();
        format!("{enumstr}::{camel}")
    }
}

const RUST_: Rust = Rust {};
pub const RUST: &'static dyn language::Language = &RUST_;