Skip to content

Commit

Permalink
refactor: refactor excel, parser, and file writer modules
Browse files Browse the repository at this point in the history
  • Loading branch information
albugowy15 committed May 22, 2024
1 parent ced9331 commit 074ba2a
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 232 deletions.
11 changes: 5 additions & 6 deletions src/commands/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use crate::{
prepare_data, Repository,
},
},
utils::{
excel::{Excel, ScheduleParser},
file::OutWriter,
},
excel::Excel,
file_writer::{compare_writer::CompareFileWriter, FileWriter},
parser::schedule_parser::ScheduleParser,
};
use std::{collections::HashMap, path::PathBuf, sync::Arc};

Expand All @@ -36,8 +35,8 @@ pub async fn compare_handler(file: &PathBuf, sheet: &str, outdir: &PathBuf) -> a
deleted.len()
);
println!("Write the result to {:?}", &outdir);
let mut writer = OutWriter::new(outdir).await?;
writer
let mut file_writer = FileWriter::new(outdir).await?;
file_writer
.write_compare_result(&added, &changed, &deleted)
.await?;
println!("Successfully write all schedule changes to {:?}", &outdir);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/find.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use crate::utils::excel::{Excel, FindClassSchedule};
use crate::excel::{find_class::FindClassSchedule, Excel};

pub async fn find_handler(file: &PathBuf, sheet: &str, subject: &str) -> anyhow::Result<()> {
println!("Parse class schedule from Excel");
Expand Down
11 changes: 5 additions & 6 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use crate::{
prepare_data, Repository,
},
},
utils::{
excel::{Excel, ScheduleParser},
file::OutWriter,
},
excel::Excel,
file_writer::{sql_writer::SqlFileWriter, FileWriter},
parser::schedule_parser::ScheduleParser,
};

pub async fn update_handler(
Expand Down Expand Up @@ -65,9 +64,9 @@ async fn write_change_to_output_file(
path_output: PathBuf,
) -> anyhow::Result<()> {
println!("Write {} classes to out directory", list_class.len());
OutWriter::new(&path_output)
FileWriter::new(&path_output)
.await?
.write_output(&list_class)
.write_sql(&list_class)
.await?;
Ok(())
}
34 changes: 34 additions & 0 deletions src/excel/excel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::path::PathBuf;

use anyhow::Context;
use calamine::{open_workbook, Data, Range, Reader, Xlsx};

use crate::db::repository::LecturerSubjectSessionMap;

pub struct Excel {
pub range: Range<Data>,
pub lecturer_subjects_session_map: LecturerSubjectSessionMap,
}

impl Excel {
pub fn new(file_path: &PathBuf, sheet_name: &str) -> anyhow::Result<Self> {
println!("Open excel file at {:?}", file_path);
let mut excel: Xlsx<_> =
open_workbook(file_path).with_context(|| "Cannot open excel file")?;
println!("Open excel sheet from {}", sheet_name);
let range = excel.worksheet_range(sheet_name)?;
println!("Successfully open excel file");
Ok(Self {
range,
lecturer_subjects_session_map: LecturerSubjectSessionMap::default(),
})
}

pub fn with_repo_data(
mut self,
lecturer_subjects_session_map: LecturerSubjectSessionMap,
) -> Excel {
self.lecturer_subjects_session_map = lecturer_subjects_session_map;
self
}
}
10 changes: 7 additions & 3 deletions src/utils/excel/find_class.rs → src/excel/find_class.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use calamine::DataType;

use crate::excel::retrieve::*;
use crate::excel::Excel;
use crate::parser::Parser;
use crate::{db::repository::class::ClassFindSchedule, DAYS};
use calamine::DataType;

use super::{Excel, FindClassSchedule, Parser, Retrieve};
pub trait FindClassSchedule {
fn find_schedule_from_class(&self, subject_name: &str) -> Vec<ClassFindSchedule>;
}

impl FindClassSchedule for Excel {
fn find_schedule_from_class(&self, subject_name: &str) -> Vec<ClassFindSchedule> {
Expand Down
37 changes: 37 additions & 0 deletions src/excel/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub mod find_class;
pub mod retrieve;

use std::path::PathBuf;

use anyhow::Context;
use calamine::{open_workbook, Data, Range, Reader, Xlsx};

use crate::db::repository::LecturerSubjectSessionMap;

pub struct Excel {
pub range: Range<Data>,
pub lecturer_subjects_session_map: LecturerSubjectSessionMap,
}

impl Excel {
pub fn new(file_path: &PathBuf, sheet_name: &str) -> anyhow::Result<Self> {
println!("Open excel file at {:?}", file_path);
let mut excel: Xlsx<_> =
open_workbook(file_path).with_context(|| "Cannot open excel file")?;
println!("Open excel sheet from {}", sheet_name);
let range = excel.worksheet_range(sheet_name)?;
println!("Successfully open excel file");
Ok(Self {
range,
lecturer_subjects_session_map: LecturerSubjectSessionMap::default(),
})
}

pub fn with_repo_data(
mut self,
lecturer_subjects_session_map: LecturerSubjectSessionMap,
) -> Excel {
self.lecturer_subjects_session_map = lecturer_subjects_session_map;
self
}
}
7 changes: 6 additions & 1 deletion src/utils/excel/retrieve.rs → src/excel/retrieve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use super::{Excel, Retrieve};
use crate::excel::Excel;
use calamine::DataType;

pub trait Retrieve {
fn retrieve_class_detail(&self, row_idx: u32, col_idx: u32) -> Option<String>;
fn retrieve_session(&self, row_idx: u32) -> Option<String>;
}

impl Retrieve for Excel {
fn retrieve_class_detail(&self, row_idx: u32, col_idx: u32) -> Option<String> {
self.range
Expand Down
30 changes: 30 additions & 0 deletions src/file_writer/class_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::db::repository::class::ClassFromSchedule;
use crate::file_writer::FileWriter;

pub trait ClassFileWriter {
fn write_class_info(
&mut self,
prefix: &str,
class: &ClassFromSchedule,
) -> impl std::future::Future<Output = anyhow::Result<()>> + Send;
}

impl ClassFileWriter for FileWriter {
async fn write_class_info(
&mut self,
prefix: &str,
class: &ClassFromSchedule,
) -> anyhow::Result<()> {
let query = format!(
"{} {} {}, {} {}, {:?}\n",
prefix,
class.subject_name,
class.class_code,
class.day,
class.session_start,
class.lecturer_code
);
self.write(query).await?;
Ok(())
}
}
Loading

0 comments on commit 074ba2a

Please sign in to comment.