feat(error): add more intuitive CellPos formatting and get rid of ErrorCell

This commit is contained in:
2025-10-10 01:27:05 +04:00
parent 191ec36fef
commit 7bac48f8fc
3 changed files with 30 additions and 26 deletions

View File

@@ -1,21 +1,5 @@
use derive_more::{Display, Error, From};
use crate::parser::worksheet::CellPos; use crate::parser::worksheet::CellPos;
use derive_more::{Display, Error, From};
#[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(),
}
}
}
#[derive(Debug, Display, Error, From)] #[derive(Debug, Display, Error, From)]
pub enum Error { pub enum Error {
@@ -28,8 +12,8 @@ pub enum Error {
#[display("There is no data on work sheet boundaries.")] #[display("There is no data on work sheet boundaries.")]
UnknownWorkSheetRange, UnknownWorkSheetRange,
#[display("Failed to read lesson start and end from {_0}.")] #[display("Failed to read lesson start and end of lesson at {_0}.")]
NoLessonBoundaries(ErrorCell), NoLessonBoundaries(CellPos),
#[display("No start and end times matching the lesson (at {_0}) was found.")] #[display("No start and end times matching the lesson (at {_0}) was found.")]
LessonTimeNotFound(CellPos), LessonTimeNotFound(CellPos),

View File

@@ -1,6 +1,5 @@
pub use self::error::{Error, Result}; pub use self::error::{Error, Result};
use crate::or_continue; use crate::or_continue;
use crate::parser::error::ErrorCell;
use crate::parser::worksheet::{CellPos, CellRange, WorkSheet}; use crate::parser::worksheet::{CellPos, CellRange, WorkSheet};
use crate::parser::LessonParseResult::{Lessons, Street}; use crate::parser::LessonParseResult::{Lessons, Street};
use base::LessonType::Break; use base::LessonType::Break;
@@ -548,9 +547,8 @@ fn parse_day_boundaries(
continue; continue;
}; };
let lesson_time = parse_lesson_boundaries_cell(&time_cell, date).ok_or( let lesson_time = parse_lesson_boundaries_cell(&time_cell, date)
Error::NoLessonBoundaries(ErrorCell::new(row, column, &time_cell)), .ok_or(Error::NoLessonBoundaries(CellPos::new(row, column)))?;
)?;
// type // type
let lesson_type = if time_cell.contains("пара") { let lesson_type = if time_cell.contains("пара") {

View File

@@ -1,5 +1,5 @@
use derive_more::Display;
use regex::Regex; use regex::Regex;
use std::fmt::{Display, Formatter};
use std::ops::Deref; use std::ops::Deref;
use std::sync::LazyLock; use std::sync::LazyLock;
@@ -9,13 +9,35 @@ pub struct WorkSheet {
pub merges: Vec<calamine::Dimensions>, pub merges: Vec<calamine::Dimensions>,
} }
#[derive(Clone, Debug, Display, derive_more::Error)] #[derive(Clone, Debug, derive_more::Error)]
#[display("row {row}, column {column}")]
pub struct CellPos { pub struct CellPos {
pub row: u32, pub row: u32,
pub column: 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 struct CellRange {
pub start: CellPos, pub start: CellPos,
pub end: CellPos, pub end: CellPos,