From 7bac48f8fc4655a72cdd44ba8317799f22bd4c4b Mon Sep 17 00:00:00 2001 From: n08i40k Date: Fri, 10 Oct 2025 01:27:05 +0400 Subject: [PATCH] feat(error): add more intuitive CellPos formatting and get rid of ErrorCell --- .../src/parser/error.rs | 22 ++------------- .../src/parser/mod.rs | 6 ++-- .../src/parser/worksheet.rs | 28 +++++++++++++++++-- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/providers/provider-engels-polytechnic/src/parser/error.rs b/providers/provider-engels-polytechnic/src/parser/error.rs index 0635bd9..2b8df5c 100644 --- a/providers/provider-engels-polytechnic/src/parser/error.rs +++ b/providers/provider-engels-polytechnic/src/parser/error.rs @@ -1,21 +1,5 @@ -use derive_more::{Display, Error, From}; use crate::parser::worksheet::CellPos; - -#[derive(Clone, Debug, Display, Error)] -#[display("'{data}' at {pos}")] -pub struct ErrorCell { - pub pos: CellPos, - pub data: String, -} - -impl ErrorCell { - pub fn new(row: u32, column: u32, data: &str) -> Self { - Self { - pos: CellPos { row, column }, - data: data.to_string(), - } - } -} +use derive_more::{Display, Error, From}; #[derive(Debug, Display, Error, From)] pub enum Error { @@ -28,8 +12,8 @@ pub enum Error { #[display("There is no data on work sheet boundaries.")] UnknownWorkSheetRange, - #[display("Failed to read lesson start and end from {_0}.")] - NoLessonBoundaries(ErrorCell), + #[display("Failed to read lesson start and end of lesson at {_0}.")] + NoLessonBoundaries(CellPos), #[display("No start and end times matching the lesson (at {_0}) was found.")] LessonTimeNotFound(CellPos), diff --git a/providers/provider-engels-polytechnic/src/parser/mod.rs b/providers/provider-engels-polytechnic/src/parser/mod.rs index 0ac915f..a40312e 100644 --- a/providers/provider-engels-polytechnic/src/parser/mod.rs +++ b/providers/provider-engels-polytechnic/src/parser/mod.rs @@ -1,6 +1,5 @@ pub use self::error::{Error, Result}; use crate::or_continue; -use crate::parser::error::ErrorCell; use crate::parser::worksheet::{CellPos, CellRange, WorkSheet}; use crate::parser::LessonParseResult::{Lessons, Street}; use base::LessonType::Break; @@ -548,9 +547,8 @@ fn parse_day_boundaries( continue; }; - let lesson_time = parse_lesson_boundaries_cell(&time_cell, date).ok_or( - Error::NoLessonBoundaries(ErrorCell::new(row, column, &time_cell)), - )?; + let lesson_time = parse_lesson_boundaries_cell(&time_cell, date) + .ok_or(Error::NoLessonBoundaries(CellPos::new(row, column)))?; // type let lesson_type = if time_cell.contains("пара") { diff --git a/providers/provider-engels-polytechnic/src/parser/worksheet.rs b/providers/provider-engels-polytechnic/src/parser/worksheet.rs index adc70b6..0ad5573 100644 --- a/providers/provider-engels-polytechnic/src/parser/worksheet.rs +++ b/providers/provider-engels-polytechnic/src/parser/worksheet.rs @@ -1,5 +1,5 @@ -use derive_more::Display; use regex::Regex; +use std::fmt::{Display, Formatter}; use std::ops::Deref; use std::sync::LazyLock; @@ -9,13 +9,35 @@ pub struct WorkSheet { pub merges: Vec, } -#[derive(Clone, Debug, Display, derive_more::Error)] -#[display("row {row}, column {column}")] +#[derive(Clone, Debug, derive_more::Error)] pub struct CellPos { pub row: u32, pub column: u32, } +fn format_column_index(index: u32) -> String { + // https://stackoverflow.com/a/297214 + let quotient = index / 26; + + let char = char::from((65 + (index % 26)) as u8); + + if quotient > 0 { + return format!("{}{}", format_column_index(quotient - 1), char); + } + + return char.to_string(); +} + +impl Display for CellPos { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!( + "column {}, row {}", + format_column_index(self.column), + self.row + 1, + )) + } +} + pub struct CellRange { pub start: CellPos, pub end: CellPos,