use ozra::types::{DataSet, FieldValue};
use serde::{Deserialize, Serialize};
use crate::{ApplicationError, RusaintError};
#[allow(unused)]
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct RegisteredLecture {
pub syllabus: Option<String>,
pub category: String,
pub sub_category: Option<String>,
pub abeek_info: Option<String>,
pub field: Option<String>,
pub code: String,
pub name: String,
pub division: Option<String>,
pub professor: String,
pub time_points: String,
pub schedule_room: String,
pub sm_objid: String,
pub se_objid: String,
pub full_name: String,
pub multi_major_info: String,
pub program_code: String,
pub program_title: String,
pub registration_date: String,
pub registration_time: String,
pub remark: String,
}
fn get_string_field(row: &[(String, FieldValue)], field_name: &str) -> String {
row.iter()
.find(|(name, _)| name == field_name)
.map(|(_, val)| val.to_string_repr())
.unwrap_or_default()
}
fn find_dataset<'a>(datasets: &'a [DataSet], name: &str) -> &'a [Vec<(String, FieldValue)>] {
datasets
.iter()
.find(|(n, _)| n == name)
.map(|(_, rows)| rows.as_slice())
.unwrap_or(&[])
}
impl RegisteredLecture {
pub fn from_datasets(datasets: &[DataSet]) -> Result<Vec<Self>, RusaintError> {
let lectures: Vec<Self> = find_dataset(datasets, "ET_BOOKED")
.iter()
.map(|row| Self {
syllabus: None,
category: get_string_field(row, "CATEGORY"),
sub_category: None,
abeek_info: Some(get_string_field(row, "ABEEK_INFO")).filter(|s| !s.is_empty()),
field: None,
code: get_string_field(row, "SE_SHORT"),
name: get_string_field(row, "SE_STEXT"),
division: Some(get_string_field(row, "BUNBAN")).filter(|s| !s.is_empty()),
professor: get_string_field(row, "PROF_NM"),
time_points: get_string_field(row, "TIME_CREDIT"),
schedule_room: get_string_field(row, "LEC_TIME_ROOM"),
sm_objid: get_string_field(row, "SM_OBJID"),
se_objid: get_string_field(row, "SE_OBJID"),
full_name: get_string_field(row, "LONG_NAME"),
multi_major_info: get_string_field(row, "MULTI"),
program_code: get_string_field(row, "PROGC_VAR"),
program_title: get_string_field(row, "PROGC_VART"),
registration_date: get_string_field(row, "BOOKDATE"),
registration_time: get_string_field(row, "BOOKTIME"),
remark: get_string_field(row, "REMARK"),
})
.collect();
if lectures.is_empty() {
return Err(ApplicationError::NoLectureResult.into());
}
Ok(lectures)
}
}