v0.1 init

This commit is contained in:
Pakin 2025-05-26 12:39:09 +07:00
commit 8068810af6
18 changed files with 3859 additions and 0 deletions

97
src/previews/csvf.rs Normal file
View file

@ -0,0 +1,97 @@
// format function
//
// target csv
use log::warn;
#[derive(Debug, Default, Clone)]
pub enum AutoFixSizeStrategy {
#[default]
Strict,
FillHeaderToMatchRow {
default_value: String,
},
FillRowToMatchHeader {
default_value: String,
},
}
#[derive(Debug, Default, Clone)]
pub struct TableConfig {
pub allow_row_length_exceeds_header: bool,
pub auto_fix_size: bool,
pub auto_fix_size_strategy: AutoFixSizeStrategy,
}
#[derive(Default, Clone)]
pub struct Table {
pub header: Vec<String>,
pub rows: Vec<Vec<String>>,
pub config: TableConfig,
}
impl Table {
pub fn new() -> Table {
Table::default()
}
pub fn set_config(&mut self, config: TableConfig) {
self.config = config;
}
pub fn add_header(&mut self, header: &str) {
self.header.push(header.to_string());
}
pub fn set_header(&mut self, header: Vec<&str>) {
self.header = header
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();
}
pub fn get_current_header(self) -> Vec<String> {
self.header
}
pub fn add_row(&mut self, mut row: Vec<String>) {
if (self.clone().config.auto_fix_size) {
match self.clone().config.auto_fix_size_strategy {
AutoFixSizeStrategy::Strict => {
if (row.len() != self.header.len()) {
panic!("Row length does not match header length");
}
}
AutoFixSizeStrategy::FillHeaderToMatchRow { default_value } => {
if (row.len() > self.header.len()) {
warn!("Row length exceeds header length");
}
self.add_header(&default_value);
}
AutoFixSizeStrategy::FillRowToMatchHeader { default_value } => {
if (row.len() < self.header.len()) {
warn!("Row length is less than header length");
}
row.push(default_value.to_string());
}
}
}
self.rows.push(row);
}
pub fn add_column_desciption(&mut self, row_pos: usize, row: Vec<String>) {
self.rows.insert(row_pos, row);
}
pub fn generate_save_file(&mut self, output: &str) {
let mut csv = String::new();
csv.push_str(&self.header.join("\t"));
csv.push('\n');
for row in &self.rows {
csv.push_str(&row.join("\t"));
csv.push('\n');
}
std::fs::write(output, csv).unwrap();
}
}

255
src/previews/header.rs Normal file
View file

@ -0,0 +1,255 @@
// sample header for csvf
pub struct Headers {}
impl Headers {
// will be deprecated by next version
pub fn get_recipe_table_sgp_24022025() -> Vec<&'static str> {
[
"Name",
"ProductCode",
"521001",
"521002",
"521004",
"521005",
"521006",
"521003",
"521007",
"521008",
"521009",
"521101",
"521102",
"521103",
"521104",
"521105",
"521106",
"521107",
"521108",
"521020",
"521021",
"521022",
"521023",
"521024",
"521025",
"521027",
"521028",
"521029",
"521030",
"521200",
"521201",
"521202",
"521203",
"521205",
"521206",
"521207",
"521208",
"521210",
"521211",
"521212",
"521213",
"521214",
"521215",
"521216",
"521301",
"521041",
"521042",
"521043",
"521044",
"521045",
"521046",
"521048",
"521047",
"521049",
"521050",
"521051",
"521052",
"521053",
"521054",
"521055",
"521056",
"521057",
"521058",
"521059",
"521061",
"521060",
"521062",
"521063",
"521064",
"521065",
"521066",
"521067",
"521068",
"521069",
"521070",
"521071",
"521072",
"521073",
"2101",
"1031",
"521032",
"521036",
"521034",
"521035",
"521033",
"529501",
"529505",
"529502",
"529503",
"529506",
"529601",
"529701",
"529702",
"1",
"9100",
"8101",
"8102",
"8111",
"8112",
"8113",
"8114",
"8115",
"8116",
"8117",
"8118",
"8119",
"8120",
"8001",
"8002",
"8888",
"8889",
"200000",
"201001",
"201002",
"100004",
"522201",
"521217",
"521038",
"521037",
]
.to_vec()
}
pub fn get_recipe_table_dubai_16052025() -> Vec<&'static str> {
[
"Name",
"ProductCode",
"531001",
"531002",
"531004",
"531005",
"531006",
"531003",
"531007",
"531008",
"531009",
"531101",
"531102",
"531103",
"531104",
"531105",
"531106",
"531107",
"531108",
"531020",
"531021",
"531022",
"531023",
"531024",
"531025",
"531027",
"531028",
"531029",
"531030",
"531200",
"531201",
"531202",
"531203",
"531205",
"531206",
"531207",
"531208",
"531210",
"531211",
"531212",
"531213",
"531214",
"531215",
"531216",
"531301",
"531041",
"531042",
"531043",
"531044",
"531045",
"531046",
"531048",
"531047",
"531049",
"531050",
"531051",
"531052",
"531053",
"531054",
"531055",
"531056",
"531057",
"531058",
"531059",
"531061",
"531060",
"531062",
"531063",
"531064",
"531065",
"531066",
"531067",
"531068",
"531069",
"531070",
"531071",
"531072",
"531073",
"2101",
"1031",
"531032",
"531036",
"531034",
"531035",
"531033",
"539501",
"539505",
"539502",
"539503",
"539506",
"539601",
"539701",
"539702",
"1",
"9100",
"8101",
"8102",
"8111",
"8112",
"8113",
"8114",
"8115",
"8116",
"8117",
"8118",
"8119",
"8120",
"8001",
"8002",
"8888",
"8889",
"200000",
"201001",
"201002",
"100004",
"532201",
"531217",
"531038",
"531037",
]
.to_vec()
}
}

2
src/previews/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod csvf;
pub mod header;