refactor: refactor providers code

This commit is contained in:
2025-10-02 07:55:07 +04:00
parent df74ab03a1
commit f121a04f1b
10 changed files with 206 additions and 237 deletions

3
Cargo.lock generated
View File

@@ -3094,7 +3094,7 @@ dependencies = [
[[package]] [[package]]
name = "provider-engels-polytechnic" name = "provider-engels-polytechnic"
version = "0.2.2" version = "0.2.3"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"base", "base",
@@ -3106,7 +3106,6 @@ dependencies = [
"regex", "regex",
"reqwest", "reqwest",
"sentry", "sentry",
"serde",
"strsim", "strsim",
"tokio", "tokio",
"tokio-util", "tokio-util",

View File

@@ -3,7 +3,7 @@ members = ["actix-macros", "actix-test", "providers"]
[package] [package]
name = "schedule-parser-rusted" name = "schedule-parser-rusted"
version = "1.3.0" version = "1.3.1"
edition = "2024" edition = "2024"
publish = false publish = false

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "provider-engels-polytechnic" name = "provider-engels-polytechnic"
version = "0.2.2" version = "0.2.3"
edition = "2024" edition = "2024"
[features] [features]
@@ -14,9 +14,7 @@ tokio-util = "0.7.16"
chrono = { version = "0.4.41", features = ["serde"] } chrono = { version = "0.4.41", features = ["serde"] }
serde = { version = "1.0.219", features = ["derive"] } derive_more = { version = "2.0.1", features = ["error", "display", "from"] }
derive_more = { version = "2.0.1", features = ["error", "display"] }
utoipa = { version = "5.4.0", features = ["macros", "chrono"] } utoipa = { version = "5.4.0", features = ["macros", "chrono"] }

View File

@@ -1,4 +1,4 @@
use crate::updater::Updater; pub use crate::updater::{UpdateSource, Updater};
use async_trait::async_trait; use async_trait::async_trait;
use base::{ScheduleProvider, ScheduleSnapshot}; use base::{ScheduleProvider, ScheduleSnapshot};
use std::ops::DerefMut; use std::ops::DerefMut;
@@ -8,8 +8,6 @@ use tokio::sync::RwLock;
use tokio::time::interval; use tokio::time::interval;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
pub use crate::updater::UpdateSource;
mod parser; mod parser;
mod updater; mod updater;
mod xls_downloader; mod xls_downloader;
@@ -27,7 +25,7 @@ pub struct EngelsPolytechnicProvider {
impl EngelsPolytechnicProvider { impl EngelsPolytechnicProvider {
pub async fn get( pub async fn get(
update_source: UpdateSource, update_source: UpdateSource,
) -> Result<Arc<dyn ScheduleProvider>, crate::updater::error::Error> { ) -> Result<Arc<dyn ScheduleProvider>, crate::updater::Error> {
let (updater, snapshot) = Updater::new(update_source).await?; let (updater, snapshot) = Updater::new(update_source).await?;
Ok(Arc::new(Wrapper { Ok(Arc::new(Wrapper {
@@ -65,7 +63,7 @@ impl ScheduleProvider for Wrapper {
this.snapshot = Arc::new(snapshot); this.snapshot = Arc::new(snapshot);
}, },
Err(updater::error::Error::QueryUrlFailed(updater::error::QueryUrlError::UriFetchFailed)) => {}, Err(updater::Error::EmptyUri) => {},
Err(err) => { Err(err) => {
sentry::capture_error(&err); sentry::capture_error(&err);

View File

@@ -0,0 +1,38 @@
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(),
}
}
}
#[derive(Debug, Display, Error, From)]
pub enum Error {
#[from]
BadXls(calamine::XlsError),
#[display("No work sheets found.")]
NoWorkSheets,
#[display("There is no data on work sheet boundaries.")]
UnknownWorkSheetRange,
#[display("Failed to read lesson start and end from {_0}.")]
NoLessonBoundaries(ErrorCell),
#[display("No start and end times matching the lesson (at {_0}) was found.")]
LessonTimeNotFound(CellPos),
}
pub type Result<T> = core::result::Result<T, Error>;

View File

@@ -1,6 +1,7 @@
pub use self::error::{Error, Result};
use crate::or_continue; use crate::or_continue;
use crate::parser::error::{Error, ErrorCell, ErrorCellPos}; use crate::parser::error::ErrorCell;
use crate::parser::worksheet::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;
use base::{ use base::{
@@ -13,82 +14,12 @@ use std::collections::HashMap;
use std::io::Cursor; use std::io::Cursor;
use std::sync::LazyLock; use std::sync::LazyLock;
mod error;
mod macros; mod macros;
mod worksheet; mod worksheet;
pub mod error {
use derive_more::{Display, Error};
use serde::{Serialize, Serializer};
use std::sync::Arc;
use utoipa::ToSchema;
#[derive(Clone, Debug, Display, Error, ToSchema)]
#[display("row {row}, column {column}")]
pub struct ErrorCellPos {
pub row: u32,
pub column: u32,
}
#[derive(Clone, Debug, Display, Error, ToSchema)]
#[display("'{data}' at {pos}")]
pub struct ErrorCell {
pub pos: ErrorCellPos,
pub data: String,
}
impl ErrorCell {
pub fn new(row: u32, column: u32, data: String) -> Self {
Self {
pos: ErrorCellPos { row, column },
data,
}
}
}
#[derive(Clone, Debug, Display, Error, ToSchema)]
pub enum Error {
/// Errors related to reading XLS file.
#[display("{_0:?}: Failed to read XLS file.")]
#[schema(value_type = String)]
BadXLS(Arc<calamine::XlsError>),
/// Not a single sheet was found.
#[display("No work sheets found.")]
NoWorkSheets,
/// There are no data on the boundaries of the sheet.
#[display("There is no data on work sheet boundaries.")]
UnknownWorkSheetRange,
/// Failed to read the beginning and end of the lesson from the cell
#[display("Failed to read lesson start and end from {_0}.")]
LessonBoundaries(ErrorCell),
/// Not found the beginning and the end corresponding to the lesson.
#[display("No start and end times matching the lesson (at {_0}) was found.")]
LessonTimeNotFound(ErrorCellPos),
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Error::BadXLS(_) => serializer.serialize_str("BAD_XLS"),
Error::NoWorkSheets => serializer.serialize_str("NO_WORK_SHEETS"),
Error::UnknownWorkSheetRange => {
serializer.serialize_str("UNKNOWN_WORK_SHEET_RANGE")
}
Error::LessonBoundaries(_) => serializer.serialize_str("GLOBAL_TIME"),
Error::LessonTimeNotFound(_) => serializer.serialize_str("LESSON_TIME_NOT_FOUND"),
}
}
}
}
/// Data cell storing the group name. /// Data cell storing the group name.
pub struct GroupCellInfo { pub struct GroupMarkup {
/// Column index. /// Column index.
pub column: u32, pub column: u32,
@@ -97,7 +28,7 @@ pub struct GroupCellInfo {
} }
/// Data cell storing the line. /// Data cell storing the line.
pub struct DayCellInfo { pub struct DayMarkup {
/// Line index. /// Line index.
pub row: u32, pub row: u32,
@@ -111,8 +42,13 @@ pub struct DayCellInfo {
pub date: DateTime<Utc>, pub date: DateTime<Utc>,
} }
pub struct WorkSheetMarkup {
days: Box<[DayMarkup]>,
groups: Box<[GroupMarkup]>,
}
/// Data on the time of lessons from the second column of the schedule. /// Data on the time of lessons from the second column of the schedule.
pub struct BoundariesCellInfo { pub struct BoundariesData {
/// Temporary segment of the lesson. /// Temporary segment of the lesson.
pub time_range: LessonBoundaries, pub time_range: LessonBoundaries,
@@ -123,23 +59,26 @@ pub struct BoundariesCellInfo {
pub default_index: Option<u32>, pub default_index: Option<u32>,
/// The frame of the cell. /// The frame of the cell.
pub xls_range: ((u32, u32), (u32, u32)), pub range: CellRange,
} }
/// Obtaining a "skeleton" schedule from the working sheet. /// Obtaining a "skeleton" schedule from the working sheet.
fn parse_skeleton( fn parse_markup(worksheet: &WorkSheet) -> Result<WorkSheetMarkup> {
worksheet: &WorkSheet, struct PartialDayMarkup {
) -> Result<(Vec<DayCellInfo>, Vec<GroupCellInfo>), crate::parser::error::Error> { row: u32,
let mut groups: Vec<GroupCellInfo> = Vec::new(); name: String,
let mut days: Vec<(u32, String, Option<DateTime<Utc>>)> = Vec::new(); date: Option<DateTime<Utc>>,
}
let worksheet_start = worksheet let mut groups: Vec<GroupMarkup> = Vec::new();
.start() let mut days: Vec<PartialDayMarkup> = Vec::new();
.ok_or(error::Error::UnknownWorkSheetRange)?;
let worksheet_end = worksheet.end().ok_or(error::Error::UnknownWorkSheetRange)?;
let mut row = worksheet_start.0; let (start_row, start_col) = worksheet.start().ok_or(Error::UnknownWorkSheetRange)?;
let (end_row, end_col) = worksheet.end().ok_or(Error::UnknownWorkSheetRange)?;
while row < worksheet_end.0 { let mut row = start_row;
while row < end_row {
row += 1; row += 1;
let day_full_name = or_continue!(worksheet.get_string_from_cell(row, 0)); let day_full_name = or_continue!(worksheet.get_string_from_cell(row, 0));
@@ -149,8 +88,8 @@ fn parse_skeleton(
// переход на предыдущую строку // переход на предыдущую строку
row -= 1; row -= 1;
for column in (worksheet_start.1 + 2)..=worksheet_end.1 { for column in (start_col + 2)..=end_col {
groups.push(GroupCellInfo { groups.push(GroupMarkup {
column, column,
name: or_continue!(worksheet.get_string_from_cell(row, column)) name: or_continue!(worksheet.get_string_from_cell(row, column))
.replace(" ", ""), .replace(" ", ""),
@@ -183,37 +122,44 @@ fn parse_skeleton(
(name, date) (name, date)
}; };
days.push((row, day_name, day_date)); days.push(PartialDayMarkup {
row,
name: day_name,
date: day_date,
});
} }
// fix unparsable day dates // fix unparsable day dates
let days_max = days.len().min(5); let days_max = days.len().min(5);
for i in 0..days_max { for i in 0..days_max {
if days[i].2.is_none() && days[i + 1].2.is_some() { if days[i].date.is_none() && days[i + 1].date.is_some() {
days[i].2 = Some(days[i + 1].2.unwrap() - Duration::days(1)); days[i].date = Some(days[i + 1].date.unwrap() - Duration::days(1));
} }
} }
for i in 0..days_max { for i in 0..days_max {
let i = days_max - i; let i = days_max - i;
if days[i - 1].2.is_none() && days[i].2.is_some() { if days[i - 1].date.is_none() && days[i].date.is_some() {
days[i - 1].2 = Some(days[i].2.unwrap() - Duration::days(1)); days[i - 1].date = Some(days[i].date.unwrap() - Duration::days(1));
} }
} }
let days = days let days = days
.into_iter() .into_iter()
.map(|day| DayCellInfo { .map(|day| DayMarkup {
row: day.0, row: day.row,
column: 0, column: 0,
name: day.1, name: day.name,
date: day.2.unwrap(), date: day.date.unwrap(),
}) })
.collect(); .collect();
Ok((days, groups)) Ok(WorkSheetMarkup {
days,
groups: groups.into_boxed_slice(),
})
} }
/// The result of obtaining a lesson from the cell. /// The result of obtaining a lesson from the cell.
@@ -258,11 +204,11 @@ fn guess_lesson_type(text: &str) -> Option<LessonType> {
fn parse_lesson( fn parse_lesson(
worksheet: &WorkSheet, worksheet: &WorkSheet,
day: &Day, day: &Day,
day_boundaries: &[BoundariesCellInfo], day_boundaries: &[BoundariesData],
lesson_boundaries: &BoundariesCellInfo, lesson_boundaries: &BoundariesData,
group_column: u32, group_column: u32,
) -> Result<LessonParseResult, crate::parser::error::Error> { ) -> Result<LessonParseResult> {
let row = lesson_boundaries.xls_range.0.0; let row = lesson_boundaries.range.start.row;
let name = { let name = {
let cell_data = match worksheet.get_string_from_cell(row, group_column) { let cell_data = match worksheet.get_string_from_cell(row, group_column) {
@@ -285,15 +231,12 @@ fn parse_lesson(
let (default_range, lesson_time) = { let (default_range, lesson_time) = {
let end_time_arr = day_boundaries let end_time_arr = day_boundaries
.iter() .iter()
.filter(|time| time.xls_range.1.0 == cell_range.1.0) .filter(|time| time.range.end.row == cell_range.end.row)
.collect::<Vec<&BoundariesCellInfo>>(); .collect::<Vec<&BoundariesData>>();
let end_time = end_time_arr let end_time = end_time_arr
.first() .first()
.ok_or(error::Error::LessonTimeNotFound(ErrorCellPos { .ok_or(Error::LessonTimeNotFound(CellPos::new(row, group_column)))?;
row,
column: group_column,
}))?;
let range: Option<[u8; 2]> = if lesson_boundaries.default_index.is_some() { let range: Option<[u8; 2]> = if lesson_boundaries.default_index.is_some() {
let default = lesson_boundaries.default_index.unwrap() as u8; let default = lesson_boundaries.default_index.unwrap() as u8;
@@ -307,8 +250,8 @@ fn parse_lesson(
end: end_time.time_range.end, end: end_time.time_range.end,
}; };
Ok((range, time)) (range, time)
}?; };
let ParsedLessonName { let ParsedLessonName {
name, name,
@@ -319,7 +262,7 @@ fn parse_lesson(
{ {
let cabinets: Vec<String> = parse_cabinets( let cabinets: Vec<String> = parse_cabinets(
worksheet, worksheet,
(cell_range.0.0, cell_range.1.0), (cell_range.start.row, cell_range.end.row),
group_column + 1, group_column + 1,
); );
@@ -421,7 +364,7 @@ struct ParsedLessonName {
//noinspection GrazieInspection //noinspection GrazieInspection
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell. /// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> { fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName> {
// Части названия пары: // Части названия пары:
// 1. Само название. // 1. Само название.
// 2. Список преподавателей и подгрупп. // 2. Список преподавателей и подгрупп.
@@ -486,9 +429,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
} }
}; };
let subgroup_index = capture let subgroup_index = capture.get(2).map(|m| m.as_str().parse::<u32>().unwrap());
.get(2)
.and_then(|m| Some(m.as_str().parse::<u32>().unwrap()));
let subgroup = Some(LessonSubGroup { let subgroup = Some(LessonSubGroup {
cabinet: None, cabinet: None,
@@ -530,7 +471,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
let lesson_type = if let Some(extra) = extra let lesson_type = if let Some(extra) = extra
&& extra.len() > 4 && extra.len() > 4
{ {
let result = guess_lesson_type(&extra); let result = guess_lesson_type(extra);
if result.is_none() { if result.is_none() {
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
@@ -597,8 +538,8 @@ fn parse_day_boundaries(
date: DateTime<Utc>, date: DateTime<Utc>,
row_range: (u32, u32), row_range: (u32, u32),
column: u32, column: u32,
) -> Result<Vec<BoundariesCellInfo>, crate::parser::error::Error> { ) -> Result<Vec<BoundariesData>> {
let mut day_times: Vec<BoundariesCellInfo> = Vec::new(); let mut day_times: Vec<BoundariesData> = Vec::new();
for row in row_range.0..row_range.1 { for row in row_range.0..row_range.1 {
let time_cell = if let Some(str) = worksheet.get_string_from_cell(row, column) { let time_cell = if let Some(str) = worksheet.get_string_from_cell(row, column) {
@@ -608,7 +549,7 @@ fn parse_day_boundaries(
}; };
let lesson_time = parse_lesson_boundaries_cell(&time_cell, date).ok_or( let lesson_time = parse_lesson_boundaries_cell(&time_cell, date).ok_or(
error::Error::LessonBoundaries(ErrorCell::new(row, column, time_cell.clone())), Error::NoLessonBoundaries(ErrorCell::new(row, column, &time_cell)),
)?; )?;
// type // type
@@ -633,11 +574,11 @@ fn parse_day_boundaries(
None None
}; };
day_times.push(BoundariesCellInfo { day_times.push(BoundariesData {
time_range: lesson_time, time_range: lesson_time,
lesson_type, lesson_type,
default_index, default_index,
xls_range: worksheet.get_merge_from_start(row, column), range: worksheet.get_merge_from_start(row, column),
}); });
} }
@@ -652,9 +593,9 @@ fn parse_day_boundaries(
/// * `week_markup`: markup of the current week. /// * `week_markup`: markup of the current week.
fn parse_week_boundaries( fn parse_week_boundaries(
worksheet: &WorkSheet, worksheet: &WorkSheet,
week_markup: &[DayCellInfo], week_markup: &[DayMarkup],
) -> Result<Vec<Vec<BoundariesCellInfo>>, crate::parser::error::Error> { ) -> Result<Vec<Vec<BoundariesData>>> {
let mut result: Vec<Vec<BoundariesCellInfo>> = Vec::new(); let mut result: Vec<Vec<BoundariesData>> = Vec::new();
let worksheet_end_row = worksheet.end().unwrap().0; let worksheet_end_row = worksheet.end().unwrap().0;
let lesson_time_column = week_markup[0].column + 1; let lesson_time_column = week_markup[0].column + 1;
@@ -773,22 +714,21 @@ fn convert_groups_to_teachers(
/// ///
/// * `buffer`: XLS data containing schedule. /// * `buffer`: XLS data containing schedule.
/// ///
/// returns: Result<ParseResult, crate::parser::error::Error> /// returns: Result<ParseResult, Error>
pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule, crate::parser::error::Error> { pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule> {
let cursor = Cursor::new(&buffer); let cursor = Cursor::new(&buffer);
let mut workbook: Xls<_> = let mut workbook: Xls<_> = open_workbook_from_rs(cursor)?;
open_workbook_from_rs(cursor).map_err(|e| error::Error::BadXLS(std::sync::Arc::new(e)))?;
let worksheet = { let worksheet = {
let (worksheet_name, worksheet) = workbook let (worksheet_name, worksheet) = workbook
.worksheets() .worksheets()
.first() .first()
.ok_or(error::Error::NoWorkSheets)? .ok_or(Error::NoWorkSheets)?
.clone(); .clone();
let worksheet_merges = workbook let worksheet_merges = workbook
.worksheet_merge_cells(&worksheet_name) .worksheet_merge_cells(&worksheet_name)
.ok_or(error::Error::NoWorkSheets)?; .ok_or(Error::NoWorkSheets)?;
WorkSheet { WorkSheet {
data: worksheet, data: worksheet,
@@ -796,7 +736,11 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule, crate::parser::erro
} }
}; };
let (week_markup, groups_markup) = parse_skeleton(&worksheet)?; let WorkSheetMarkup {
days: week_markup,
groups: groups_markup,
} = parse_markup(&worksheet)?;
let week_boundaries = parse_week_boundaries(&worksheet, &week_markup)?; let week_boundaries = parse_week_boundaries(&worksheet, &week_markup)?;
let mut groups: HashMap<String, ScheduleEntry> = HashMap::new(); let mut groups: HashMap<String, ScheduleEntry> = HashMap::new();
@@ -849,7 +793,7 @@ pub mod test_utils {
use super::*; use super::*;
use base::ParsedSchedule; use base::ParsedSchedule;
pub fn test_result() -> Result<ParsedSchedule, crate::parser::error::Error> { pub fn test_result() -> Result<ParsedSchedule> {
parse_xls(&include_bytes!("../../../../test-data/engels-polytechnic.xls").to_vec()) parse_xls(&include_bytes!("../../../../test-data/engels-polytechnic.xls").to_vec())
} }
} }

View File

@@ -1,3 +1,4 @@
use derive_more::Display;
use regex::Regex; use regex::Regex;
use std::ops::Deref; use std::ops::Deref;
use std::sync::LazyLock; use std::sync::LazyLock;
@@ -8,6 +9,18 @@ pub struct WorkSheet {
pub merges: Vec<calamine::Dimensions>, pub merges: Vec<calamine::Dimensions>,
} }
#[derive(Clone, Debug, Display, derive_more::Error)]
#[display("row {row}, column {column}")]
pub struct CellPos {
pub row: u32,
pub column: u32,
}
pub struct CellRange {
pub start: CellPos,
pub end: CellPos,
}
impl Deref for WorkSheet { impl Deref for WorkSheet {
type Target = calamine::Range<calamine::Data>; type Target = calamine::Range<calamine::Data>;
@@ -45,14 +58,26 @@ impl WorkSheet {
} }
/// Obtaining the boundaries of the cell along its upper left coordinate. /// Obtaining the boundaries of the cell along its upper left coordinate.
pub fn get_merge_from_start(&self, row: u32, column: u32) -> ((u32, u32), (u32, u32)) { pub fn get_merge_from_start(&self, row: u32, column: u32) -> CellRange {
match self match self
.merges .merges
.iter() .iter()
.find(|merge| merge.start.0 == row && merge.start.1 == column) .find(|merge| merge.start.0 == row && merge.start.1 == column)
{ {
Some(merge) => (merge.start, (merge.end.0 + 1, merge.end.1 + 1)), Some(merge) => CellRange {
None => ((row, column), (row + 1, column + 1)), start: CellPos::new(merge.start.0, merge.start.1),
end: CellPos::new(merge.end.0 + 1, merge.end.1 + 1),
},
None => CellRange {
start: CellPos::new(row, column),
end: CellPos::new(row + 1, column + 1),
},
} }
} }
} }
impl CellPos {
pub fn new(row: u32, column: u32) -> Self {
Self { row, column }
}
}

View File

@@ -0,0 +1,33 @@
use crate::xls_downloader::FetchError;
use derive_more::{Display, Error, From};
#[derive(Debug, Display, Error, From)]
pub enum Error {
/// Occurs when the request to the Yandex Cloud API fails.
///
/// This may be due to network issues, invalid API key, incorrect function ID, or other
/// problems with the Yandex Cloud Function invocation.
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
Reqwest(reqwest::Error),
#[display("Unable to get URI in 3 retries")]
EmptyUri,
/// The ETag is the same (no update needed).
#[display("The ETag is the same.")]
SameETag,
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
#[display("Failed to fetch URL: {_0}")]
ScheduleFetchFailed(FetchError),
/// Downloading the XLS file content failed after successfully obtaining the URL.
#[display("Download failed: {_0}")]
ScheduleDownloadFailed(FetchError),
/// The XLS file could not be parsed into a valid schedule format.
#[from]
InvalidSchedule(crate::parser::Error),
}
pub type Result<T> = core::result::Result<T, Error>;

View File

@@ -1,7 +1,8 @@
pub use self::error::{Error, Result};
use crate::parser::parse_xls; use crate::parser::parse_xls;
use crate::updater::error::{Error, QueryUrlError, SnapshotCreationError};
use crate::xls_downloader::{FetchError, XlsDownloader}; use crate::xls_downloader::{FetchError, XlsDownloader};
use base::ScheduleSnapshot; use base::ScheduleSnapshot;
mod error;
pub enum UpdateSource { pub enum UpdateSource {
Prepared(ScheduleSnapshot), Prepared(ScheduleSnapshot),
@@ -19,59 +20,6 @@ pub struct Updater {
update_source: UpdateSource, update_source: UpdateSource,
} }
pub mod error {
use crate::xls_downloader::FetchError;
use derive_more::{Display, Error};
#[derive(Debug, Display, Error)]
pub enum Error {
/// An error occurred while querying the Yandex Cloud API for a URL.
///
/// This may result from network failures, invalid API credentials, or issues with the Yandex Cloud Function invocation.
/// See [`QueryUrlError`] for more details about specific causes.
QueryUrlFailed(QueryUrlError),
/// The schedule snapshot creation process failed.
///
/// This can happen due to URL conflicts (same URL already in use), failed network requests,
/// download errors, or invalid XLS file content. See [`SnapshotCreationError`] for details.
SnapshotCreationFailed(SnapshotCreationError),
}
/// Errors that may occur when querying the Yandex Cloud API to retrieve a URL.
#[derive(Debug, Display, Error)]
pub enum QueryUrlError {
/// Occurs when the request to the Yandex Cloud API fails.
///
/// This may be due to network issues, invalid API key, incorrect function ID, or other
/// problems with the Yandex Cloud Function invocation.
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
RequestFailed(reqwest::Error),
#[display("Unable to fetch Uri in 3 retries")]
UriFetchFailed,
}
/// Errors that may occur during the creation of a schedule snapshot.
#[derive(Debug, Display, Error)]
pub enum SnapshotCreationError {
/// The ETag is the same (no update needed).
#[display("The ETag is the same.")]
Same,
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
#[display("Failed to fetch URL: {_0}")]
FetchFailed(FetchError),
/// Downloading the XLS file content failed after successfully obtaining the URL.
#[display("Download failed: {_0}")]
DownloadFailed(FetchError),
/// The XLS file could not be parsed into a valid schedule format.
#[display("Schedule data is invalid: {_0}")]
InvalidSchedule(crate::parser::error::Error),
}
}
impl Updater { impl Updater {
/// Constructs a new `ScheduleSnapshot` by downloading and parsing schedule data from the specified URL. /// Constructs a new `ScheduleSnapshot` by downloading and parsing schedule data from the specified URL.
/// ///
@@ -85,40 +33,33 @@ impl Updater {
/// * `url`: The source URL pointing to the XLS file containing schedule data. /// * `url`: The source URL pointing to the XLS file containing schedule data.
/// ///
/// returns: Result<ScheduleSnapshot, SnapshotCreationError> /// returns: Result<ScheduleSnapshot, SnapshotCreationError>
pub async fn new_snapshot( async fn new_snapshot(downloader: &mut XlsDownloader, url: String) -> Result<ScheduleSnapshot> {
downloader: &mut XlsDownloader,
url: String,
) -> Result<ScheduleSnapshot, SnapshotCreationError> {
let head_result = downloader.set_url(&url).await.map_err(|error| { let head_result = downloader.set_url(&url).await.map_err(|error| {
if let FetchError::Unknown(error) = &error { if let FetchError::Reqwest(error) = &error {
sentry::capture_error(&error); sentry::capture_error(&error);
} }
SnapshotCreationError::FetchFailed(error) Error::ScheduleFetchFailed(error)
})?; })?;
if downloader.etag == Some(head_result.etag) { if downloader.etag == Some(head_result.etag) {
return Err(SnapshotCreationError::Same); return Err(Error::SameETag);
} }
let xls_data = downloader let xls_data = downloader
.fetch(false) .fetch(false)
.await .await
.map_err(|error| { .map_err(|error| {
if let FetchError::Unknown(error) = &error { if let FetchError::Reqwest(error) = &error {
sentry::capture_error(&error); sentry::capture_error(&error);
} }
SnapshotCreationError::DownloadFailed(error) Error::ScheduleDownloadFailed(error)
})? })?
.data .data
.unwrap(); .unwrap();
let parse_result = parse_xls(&xls_data).map_err(|error| { let parse_result = parse_xls(&xls_data)?;
sentry::capture_error(&error);
SnapshotCreationError::InvalidSchedule(error)
})?;
Ok(ScheduleSnapshot { Ok(ScheduleSnapshot {
fetched_at: head_result.requested_at, fetched_at: head_result.requested_at,
@@ -144,7 +85,7 @@ impl Updater {
/// Result containing: /// Result containing:
/// - `Ok(String)` - Complete URL constructed from the Function's response /// - `Ok(String)` - Complete URL constructed from the Function's response
/// - `Err(QueryUrlError)` - If the request or response processing fails /// - `Err(QueryUrlError)` - If the request or response processing fails
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> { async fn query_url(api_key: &str, func_id: &str) -> Result<String> {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let uri = { let uri = {
@@ -156,7 +97,7 @@ impl Updater {
loop { loop {
if counter == 3 { if counter == 3 {
return Err(QueryUrlError::UriFetchFailed); return Err(Error::EmptyUri);
} }
counter += 1; counter += 1;
@@ -169,10 +110,10 @@ impl Updater {
.header("Authorization", format!("Api-Key {}", api_key)) .header("Authorization", format!("Api-Key {}", api_key))
.send() .send()
.await .await
.map_err(QueryUrlError::RequestFailed)? .map_err(Error::Reqwest)?
.text() .text()
.await .await
.map_err(QueryUrlError::RequestFailed)?; .map_err(Error::Reqwest)?;
if uri.is_empty() { if uri.is_empty() {
log::warn!("[{}] Unable to get uri! Retrying in 5 seconds...", counter); log::warn!("[{}] Unable to get uri! Retrying in 5 seconds...", counter);
@@ -201,7 +142,7 @@ impl Updater {
/// Returns `Ok(())` if the snapshot was successfully initialized, or an `Error` if: /// Returns `Ok(())` if the snapshot was successfully initialized, or an `Error` if:
/// - URL query to Yandex Cloud failed ([`QueryUrlError`]) /// - URL query to Yandex Cloud failed ([`QueryUrlError`])
/// - Schedule snapshot creation failed ([`SnapshotCreationError`]) /// - Schedule snapshot creation failed ([`SnapshotCreationError`])
pub async fn new(update_source: UpdateSource) -> Result<(Self, ScheduleSnapshot), Error> { pub async fn new(update_source: UpdateSource) -> Result<(Self, ScheduleSnapshot)> {
let mut this = Updater { let mut this = Updater {
downloader: XlsDownloader::new(), downloader: XlsDownloader::new(),
update_source, update_source,
@@ -222,19 +163,14 @@ impl Updater {
yandex_func_id, yandex_func_id,
} => { } => {
log::info!("Obtaining a link using FaaS..."); log::info!("Obtaining a link using FaaS...");
Self::query_url(yandex_api_key, yandex_func_id) Self::query_url(yandex_api_key, yandex_func_id).await?
.await
.map_err(Error::QueryUrlFailed)?
} }
_ => unreachable!(), _ => unreachable!(),
}; };
log::info!("For the initial setup, a link {} will be used", url); log::info!("For the initial setup, a link {} will be used", url);
let snapshot = Self::new_snapshot(&mut this.downloader, url) let snapshot = Self::new_snapshot(&mut this.downloader, url).await?;
.await
.map_err(Error::SnapshotCreationFailed)?;
log::info!("Schedule snapshot successfully created!"); log::info!("Schedule snapshot successfully created!");
Ok((this, snapshot)) Ok((this, snapshot))
@@ -257,7 +193,7 @@ impl Updater {
pub async fn update( pub async fn update(
&mut self, &mut self,
current_snapshot: &ScheduleSnapshot, current_snapshot: &ScheduleSnapshot,
) -> Result<ScheduleSnapshot, Error> { ) -> Result<ScheduleSnapshot> {
if let UpdateSource::Prepared(snapshot) = &self.update_source { if let UpdateSource::Prepared(snapshot) = &self.update_source {
let mut snapshot = snapshot.clone(); let mut snapshot = snapshot.clone();
snapshot.update(); snapshot.update();
@@ -269,21 +205,19 @@ impl Updater {
UpdateSource::GrabFromSite { UpdateSource::GrabFromSite {
yandex_api_key, yandex_api_key,
yandex_func_id, yandex_func_id,
} => Self::query_url(yandex_api_key.as_str(), yandex_func_id.as_str()) } => Self::query_url(yandex_api_key.as_str(), yandex_func_id.as_str()).await?,
.await
.map_err(Error::QueryUrlFailed)?,
_ => unreachable!(), _ => unreachable!(),
}; };
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await { let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
Ok(snapshot) => snapshot, Ok(snapshot) => snapshot,
Err(SnapshotCreationError::Same) => { Err(Error::SameETag) => {
let mut clone = current_snapshot.clone(); let mut clone = current_snapshot.clone();
clone.update(); clone.update();
clone clone
} }
Err(error) => return Err(Error::SnapshotCreationFailed(error)), Err(error) => return Err(error),
}; };
Ok(snapshot) Ok(snapshot)

View File

@@ -14,7 +14,7 @@ pub enum FetchError {
/// Unknown error. /// Unknown error.
#[display("An unknown error occurred while downloading the file.")] #[display("An unknown error occurred while downloading the file.")]
#[schema(value_type = String)] #[schema(value_type = String)]
Unknown(Arc<reqwest::Error>), Reqwest(Arc<reqwest::Error>),
/// Server returned a status code different from 200. /// Server returned a status code different from 200.
#[display("Server returned a status code {status_code}.")] #[display("Server returned a status code {status_code}.")]
@@ -31,7 +31,7 @@ pub enum FetchError {
impl FetchError { impl FetchError {
pub fn unknown(error: Arc<reqwest::Error>) -> Self { pub fn unknown(error: Arc<reqwest::Error>) -> Self {
Self::Unknown(error) Self::Reqwest(error)
} }
pub fn bad_status_code(status_code: u16) -> Self { pub fn bad_status_code(status_code: u16) -> Self {