15 Commits

Author SHA1 Message Date
dependabot[bot]
e7500725d8 Bump tokio from 1.44.1 to 1.45.0
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.44.1 to 1.45.0.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.44.1...tokio-1.45.0)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.45.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-05-26 01:26:54 +00:00
4cf6df379e fix(parser): fix lessons merging 2025-05-26 05:24:13 +04:00
ba8b164b6a refactor(parser): rewrite some parts of code 2025-05-26 05:24:08 +04:00
ff9d7d6c3a fix(cache): fix setting cache_update_required flag in cache status 2025-05-25 17:39:23 +04:00
9090716f87 fix(test): fix test sign_up_invalid_group 2025-05-25 15:57:18 +04:00
ee992f1b55 chore(xls): update schedule xls 2025-05-25 15:49:52 +04:00
7f71fb1616 refactor(env): remove unsave env::set_var call 2025-05-25 15:48:43 +04:00
234055eaeb feat(test): add ability to use test env without schedule 2025-05-25 15:48:10 +04:00
fceffb900d release/v1.0.3 2025-04-18 00:29:04 +04:00
49ce0005dc Исправление работы подключения к сайтам из-за отсутствия сертификатов. 2025-04-18 00:28:55 +04:00
4c738085f2 release/v1.0.2 2025-04-18 00:11:55 +04:00
20602eb863 Улучшенное отображение ошибок при обновлении ссылки расписания. 2025-04-18 00:11:05 +04:00
e04d462223 1.0.1 2025-04-17 23:08:58 +04:00
22af02464d Исправление работы авторизации с помощью VK ID. 2025-04-17 23:07:19 +04:00
9a517519db User-Agent для reqwest теперь устанавливается с помощью переменной окружения. 2025-04-17 22:41:42 +04:00
27 changed files with 621 additions and 484 deletions

View File

@@ -47,6 +47,7 @@ jobs:
JWT_SECRET: "test-secret-at-least-256-bits-used" JWT_SECRET: "test-secret-at-least-256-bits-used"
VKID_CLIENT_ID: 0 VKID_CLIENT_ID: 0
VKID_REDIRECT_URI: "vk0://vk.com/blank.html" VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"
build: build:
name: Build name: Build
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@@ -3,6 +3,7 @@ name: cargo test
on: on:
push: push:
branches: [ "master" ] branches: [ "master" ]
tags-ignore: [ "release/v*" ]
permissions: permissions:
contents: read contents: read
@@ -28,3 +29,4 @@ jobs:
JWT_SECRET: "test-secret-at-least-256-bits-used" JWT_SECRET: "test-secret-at-least-256-bits-used"
VKID_CLIENT_ID: 0 VKID_CLIENT_ID: 0
VKID_REDIRECT_URI: "vk0://vk.com/blank.html" VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"

View File

@@ -4,9 +4,10 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/actix-macros/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/actix-macros/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/actix-test/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/actix-test/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/actix-macros/target" /> <excludeFolder url="file://$MODULE_DIR$/actix-macros/target" />
<excludeFolder url="file://$MODULE_DIR$/actix-test/target" /> <excludeFolder url="file://$MODULE_DIR$/actix-test/target" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />

158
Cargo.lock generated
View File

@@ -696,6 +696,15 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cb3c4a0d3776f7535c32793be81d6d5fec0d48ac70955d9834e643aa249a52f" checksum = "1cb3c4a0d3776f7535c32793be81d6d5fec0d48ac70955d9834e643aa249a52f"
[[package]]
name = "convert_case"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb402b8d4c85569410425650ce3eddc7d698ed96d39a73f941b08fb63082f1e7"
dependencies = [
"unicode-segmentation",
]
[[package]] [[package]]
name = "cookie" name = "cookie"
version = "0.16.2" version = "0.16.2"
@@ -753,25 +762,22 @@ dependencies = [
[[package]] [[package]]
name = "criterion" name = "criterion"
version = "0.5.1" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" checksum = "3bf7af66b0989381bd0be551bd7cc91912a655a58c6918420c9527b1fd8b4679"
dependencies = [ dependencies = [
"anes", "anes",
"cast", "cast",
"ciborium", "ciborium",
"clap", "clap",
"criterion-plot", "criterion-plot",
"is-terminal", "itertools 0.13.0",
"itertools",
"num-traits", "num-traits",
"once_cell",
"oorandom", "oorandom",
"plotters", "plotters",
"rayon", "rayon",
"regex", "regex",
"serde", "serde",
"serde_derive",
"serde_json", "serde_json",
"tinytemplate", "tinytemplate",
"walkdir", "walkdir",
@@ -784,7 +790,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [ dependencies = [
"cast", "cast",
"itertools", "itertools 0.10.5",
] ]
[[package]] [[package]]
@@ -918,6 +924,7 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3" checksum = "bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3"
dependencies = [ dependencies = [
"convert_case",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.100", "syn 2.0.100",
@@ -1404,12 +1411,6 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e"
[[package]] [[package]]
name = "hex" name = "hex"
version = "0.4.3" version = "0.4.3"
@@ -1848,17 +1849,6 @@ version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130"
[[package]]
name = "is-terminal"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "is_terminal_polyfill" name = "is_terminal_polyfill"
version = "1.70.1" version = "1.70.1"
@@ -1874,6 +1864,15 @@ dependencies = [
"either", "either",
] ]
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.15" version = "1.0.15"
@@ -2396,7 +2395,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"itertools", "itertools 0.13.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.100", "syn 2.0.100",
@@ -2441,7 +2440,7 @@ dependencies = [
"rustc-hash", "rustc-hash",
"rustls", "rustls",
"socket2", "socket2",
"thiserror 2.0.12", "thiserror",
"tokio", "tokio",
"tracing", "tracing",
"web-time", "web-time",
@@ -2461,7 +2460,7 @@ dependencies = [
"rustls", "rustls",
"rustls-pki-types", "rustls-pki-types",
"slab", "slab",
"thiserror 2.0.12", "thiserror",
"tinyvec", "tinyvec",
"tracing", "tracing",
"web-time", "web-time",
@@ -2874,17 +2873,30 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "schedule-parser"
version = "0.1.0"
dependencies = [
"calamine",
"chrono",
"criterion",
"derive_more",
"fuzzy-matcher",
"regex",
"serde",
"serde_repr",
"utoipa",
]
[[package]] [[package]]
name = "schedule-parser-rusted" name = "schedule-parser-rusted"
version = "0.8.0" version = "1.0.3"
dependencies = [ dependencies = [
"actix-macros 0.1.0", "actix-macros 0.1.0",
"actix-test", "actix-test",
"actix-web", "actix-web",
"bcrypt", "bcrypt",
"calamine",
"chrono", "chrono",
"criterion",
"derive_more", "derive_more",
"diesel", "diesel",
"diesel-derive-enum", "diesel-derive-enum",
@@ -2892,19 +2904,17 @@ dependencies = [
"env_logger", "env_logger",
"firebase-messaging-rs", "firebase-messaging-rs",
"futures-util", "futures-util",
"fuzzy-matcher",
"hex", "hex",
"jsonwebtoken", "jsonwebtoken",
"mime", "mime",
"objectid", "objectid",
"rand 0.9.0", "rand 0.9.0",
"regex",
"reqwest", "reqwest",
"schedule-parser",
"sentry", "sentry",
"sentry-actix", "sentry-actix",
"serde", "serde",
"serde_json", "serde_json",
"serde_repr",
"serde_with", "serde_with",
"sha1 0.11.0-pre.5", "sha1 0.11.0-pre.5",
"tokio", "tokio",
@@ -2977,13 +2987,14 @@ checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0"
[[package]] [[package]]
name = "sentry" name = "sentry"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "255914a8e53822abd946e2ce8baa41d4cded6b8e938913b7f7b9da5b7ab44335" checksum = "a505499b38861edd82b5a688fa06ba4ba5875bb832adeeeba22b7b23fc4bc39a"
dependencies = [ dependencies = [
"httpdate", "httpdate",
"native-tls", "native-tls",
"reqwest", "reqwest",
"sentry-actix",
"sentry-backtrace", "sentry-backtrace",
"sentry-contexts", "sentry-contexts",
"sentry-core", "sentry-core",
@@ -2996,9 +3007,9 @@ dependencies = [
[[package]] [[package]]
name = "sentry-actix" name = "sentry-actix"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a927aed43cce0e9240f7477ac81cdfa2ffb048e0e2b17000eb5976e14f063993" checksum = "39ad8bfdcfbc6e0d0dacaa5728555085ef459fa9226cfc2fe64eefa4b8038b7f"
dependencies = [ dependencies = [
"actix-http", "actix-http",
"actix-web", "actix-web",
@@ -3009,21 +3020,20 @@ dependencies = [
[[package]] [[package]]
name = "sentry-backtrace" name = "sentry-backtrace"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00293cd332a859961f24fd69258f7e92af736feaeb91020cff84dac4188a4302" checksum = "8dace796060e4ad10e3d1405b122ae184a8b2e71dce05ae450e4f81b7686b0d9"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"once_cell",
"regex", "regex",
"sentry-core", "sentry-core",
] ]
[[package]] [[package]]
name = "sentry-contexts" name = "sentry-contexts"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "961990f9caa76476c481de130ada05614cd7f5aa70fb57c2142f0e09ad3fb2aa" checksum = "87bd9e6b51ffe2bc7188ebe36cb67557cb95749c08a3f81f33e8c9b135e0d1bc"
dependencies = [ dependencies = [
"hostname 0.4.1", "hostname 0.4.1",
"libc", "libc",
@@ -3035,12 +3045,11 @@ dependencies = [
[[package]] [[package]]
name = "sentry-core" name = "sentry-core"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a6409d845707d82415c800290a5d63be5e3df3c2e417b0997c60531dfbd35ef" checksum = "7426d4beec270cfdbb50f85f0bb2ce176ea57eed0b11741182a163055a558187"
dependencies = [ dependencies = [
"once_cell", "rand 0.9.0",
"rand 0.8.5",
"sentry-types", "sentry-types",
"serde", "serde",
"serde_json", "serde_json",
@@ -3048,20 +3057,19 @@ dependencies = [
[[package]] [[package]]
name = "sentry-debug-images" name = "sentry-debug-images"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71ab5df4f3b64760508edfe0ba4290feab5acbbda7566a79d72673065888e5cc" checksum = "9df15c066c04f34c4dfd496a8e76590106b93283f72ef1a47d8fb24d88493424"
dependencies = [ dependencies = [
"findshlibs", "findshlibs",
"once_cell",
"sentry-core", "sentry-core",
] ]
[[package]] [[package]]
name = "sentry-panic" name = "sentry-panic"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "609b1a12340495ce17baeec9e08ff8ed423c337c1a84dffae36a178c783623f3" checksum = "c92beed69b776a162b6d269bef1eaa3e614090b6df45a88d9b239c4fdbffdfba"
dependencies = [ dependencies = [
"sentry-backtrace", "sentry-backtrace",
"sentry-core", "sentry-core",
@@ -3069,9 +3077,9 @@ dependencies = [
[[package]] [[package]]
name = "sentry-tracing" name = "sentry-tracing"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f4e86402d5c50239dc7d8fd3f6d5e048221d5fcb4e026d8d50ab57fe4644cb" checksum = "55c323492795de90824f3198562e33dd74ae3bc852fbb13c0cabec54a1cf73cd"
dependencies = [ dependencies = [
"sentry-backtrace", "sentry-backtrace",
"sentry-core", "sentry-core",
@@ -3081,16 +3089,16 @@ dependencies = [
[[package]] [[package]]
name = "sentry-types" name = "sentry-types"
version = "0.37.0" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d3f117b8755dbede8260952de2aeb029e20f432e72634e8969af34324591631" checksum = "04b6c9287202294685cb1f749b944dbbce8160b81a1061ecddc073025fed129f"
dependencies = [ dependencies = [
"debugid", "debugid",
"hex", "hex",
"rand 0.8.5", "rand 0.9.0",
"serde", "serde",
"serde_json", "serde_json",
"thiserror 1.0.69", "thiserror",
"time 0.3.40", "time 0.3.40",
"url", "url",
"uuid", "uuid",
@@ -3232,7 +3240,7 @@ checksum = "297f631f50729c8c99b84667867963997ec0b50f32b2a7dbcab828ef0541e8bb"
dependencies = [ dependencies = [
"num-bigint", "num-bigint",
"num-traits", "num-traits",
"thiserror 2.0.12", "thiserror",
"time 0.3.40", "time 0.3.40",
] ]
@@ -3355,33 +3363,13 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "thiserror"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl 1.0.69",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "2.0.12" version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [ dependencies = [
"thiserror-impl 2.0.12", "thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.100",
] ]
[[package]] [[package]]
@@ -3484,9 +3472,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.44.1" version = "1.45.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f382da615b842244d4b8738c82ed1275e6c5dd90c459a30941cd07080b06c91a" checksum = "75ef51a33ef1da925cea3e4eb122833cb377c61439ca401b770f54902b806779"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@@ -3722,6 +3710,12 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "unicode-segmentation"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.6" version = "0.2.6"
@@ -4367,7 +4361,7 @@ dependencies = [
"flate2", "flate2",
"indexmap 2.8.0", "indexmap 2.8.0",
"memchr", "memchr",
"thiserror 2.0.12", "thiserror",
"zopfli", "zopfli",
] ]

View File

@@ -1,9 +1,9 @@
[workspace] [workspace]
members = ["actix-macros", "actix-test"] members = ["actix-macros", "actix-test", "schedule-parser"]
[package] [package]
name = "schedule-parser-rusted" name = "schedule-parser-rusted"
version = "0.8.0" version = "1.0.3"
edition = "2024" edition = "2024"
publish = false publish = false
@@ -13,31 +13,28 @@ debug = true
[dependencies] [dependencies]
actix-web = "4.10.2" actix-web = "4.10.2"
actix-macros = { path = "actix-macros" } actix-macros = { path = "actix-macros" }
schedule-parser = { path = "schedule-parser", features = ["test-utils"] }
bcrypt = "0.17.0" bcrypt = "0.17.0"
calamine = "0.26.1"
chrono = { version = "0.4.40", features = ["serde"] } chrono = { version = "0.4.40", features = ["serde"] }
derive_more = "2.0.1" derive_more = { version = "2", features = ["full"] }
diesel = { version = "2.2.8", features = ["postgres"] } diesel = { version = "2.2.8", features = ["postgres"] }
diesel-derive-enum = { git = "https://github.com/Havunen/diesel-derive-enum.git", features = ["postgres"] } diesel-derive-enum = { git = "https://github.com/Havunen/diesel-derive-enum.git", features = ["postgres"] }
dotenvy = "0.15.7" dotenvy = "0.15.7"
env_logger = "0.11.7" env_logger = "0.11.7"
firebase-messaging-rs = { git = "https://github.com/i10416/firebase-messaging-rs.git" } firebase-messaging-rs = { git = "https://github.com/i10416/firebase-messaging-rs.git" }
futures-util = "0.3.31" futures-util = "0.3.31"
fuzzy-matcher = "0.3.7"
jsonwebtoken = { version = "9.3.1", features = ["use_pem"] } jsonwebtoken = { version = "9.3.1", features = ["use_pem"] }
hex = "0.4.3" hex = "0.4.3"
mime = "0.3.17" mime = "0.3.17"
objectid = "0.2.0" objectid = "0.2.0"
regex = "1.11.1"
reqwest = { version = "0.12.15", features = ["json"] } reqwest = { version = "0.12.15", features = ["json"] }
sentry = "0.37.0" sentry = "0.38"
sentry-actix = "0.37.0" sentry-actix = "0.38"
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140" serde_json = "1.0.140"
serde_with = "3.12.0" serde_with = "3.12.0"
serde_repr = "0.1.20"
sha1 = "0.11.0-pre.5" sha1 = "0.11.0-pre.5"
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.45.1", features = ["macros", "rt-multi-thread"] }
rand = "0.9.0" rand = "0.9.0"
utoipa = { version = "5", features = ["actix_extras", "chrono"] } utoipa = { version = "5", features = ["actix_extras", "chrono"] }
utoipa-rapidoc = { version = "6.0.0", features = ["actix-web"] } utoipa-rapidoc = { version = "6.0.0", features = ["actix-web"] }
@@ -46,8 +43,3 @@ uuid = { version = "1.16.0", features = ["v4"] }
[dev-dependencies] [dev-dependencies]
actix-test = { path = "actix-test" } actix-test = { path = "actix-test" }
criterion = "0.5.1"
[[bench]]
name = "parse"
harness = false

View File

@@ -6,7 +6,7 @@ ARG BINARY_NAME
WORKDIR /app/ WORKDIR /app/
RUN apt update && \ RUN apt update && \
apt install -y libpq5 apt install -y libpq5 ca-certificates openssl
COPY ./${BINARY_NAME} /bin/main COPY ./${BINARY_NAME} /bin/main
RUN chmod +x /bin/main RUN chmod +x /bin/main

View File

@@ -0,0 +1,24 @@
[package]
name = "schedule-parser"
version = "0.1.0"
edition = "2024"
[features]
test-utils = []
[dependencies]
calamine = "0.26"
chrono = { version = "0.4", features = ["serde"] }
derive_more = { version = "2", features = ["full"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_repr = "0.1.20"
fuzzy-matcher = "0.3.7"
regex = "1.11.1"
utoipa = { version = "5", features = ["chrono"] }
[dev-dependencies]
criterion = "0.6"
[[bench]]
name = "parse"
harness = false

View File

@@ -1,9 +1,9 @@
use criterion::{Criterion, criterion_group, criterion_main}; use criterion::{Criterion, criterion_group, criterion_main};
use schedule_parser_rusted::parser::parse_xls; use schedule_parser::parse_xls;
pub fn bench_parse_xls(c: &mut Criterion) { pub fn bench_parse_xls(c: &mut Criterion) {
let buffer: Vec<u8> = include_bytes!("../schedule.xls").to_vec(); let buffer: Vec<u8> = include_bytes!("../../schedule.xls").to_vec();
c.bench_function("parse_xls", |b| b.iter(|| parse_xls(&buffer).unwrap())); c.bench_function("parse_xls", |b| b.iter(|| parse_xls(&buffer).unwrap()));
} }

View File

@@ -1,7 +1,7 @@
use crate::parser::LessonParseResult::{Lessons, Street}; use crate::LessonParseResult::{Lessons, Street};
use crate::parser::schema::LessonType::Break; use crate::schema::LessonType::Break;
use crate::parser::schema::{ use crate::schema::{
Day, ErrorCell, ErrorCellPos, Lesson, LessonSubGroup, LessonTime, LessonType, ParseError, Day, ErrorCell, ErrorCellPos, Lesson, LessonBoundaries, LessonSubGroup, LessonType, ParseError,
ParseResult, ScheduleEntry, ParseResult, ScheduleEntry,
}; };
use calamine::{Reader, Xls, open_workbook_from_rs}; use calamine::{Reader, Xls, open_workbook_from_rs};
@@ -11,15 +11,14 @@ use fuzzy_matcher::skim::SkimMatcherV2;
use regex::Regex; use regex::Regex;
use std::collections::HashMap; use std::collections::HashMap;
use std::io::Cursor; use std::io::Cursor;
use std::ops::Deref;
use std::sync::LazyLock; use std::sync::LazyLock;
mod macros;
pub mod schema; pub mod schema;
/// Data cell storing the line. /// Data cell storing the group name.
struct InternalId { struct GroupCellInfo {
/// Line index.
row: u32,
/// Column index. /// Column index.
column: u32, column: u32,
@@ -27,10 +26,25 @@ struct InternalId {
name: String, name: String,
} }
/// Data cell storing the line.
struct DayCellInfo {
/// Line index.
row: u32,
/// Column index.
column: u32,
/// Day name.
name: String,
/// Date of the day.
date: DateTime<Utc>,
}
/// 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.
struct InternalTime { struct BoundariesCellInfo {
/// Temporary segment of the lesson. /// Temporary segment of the lesson.
time_range: LessonTime, time_range: LessonBoundaries,
/// Type of lesson. /// Type of lesson.
lesson_type: LessonType, lesson_type: LessonType,
@@ -42,8 +56,18 @@ struct InternalTime {
xls_range: ((u32, u32), (u32, u32)), xls_range: ((u32, u32), (u32, u32)),
} }
/// Working sheet type alias. struct WorkSheet {
type WorkSheet = calamine::Range<calamine::Data>; pub data: calamine::Range<calamine::Data>,
pub merges: Vec<calamine::Dimensions>,
}
impl Deref for WorkSheet {
type Target = calamine::Range<calamine::Data>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
/// Getting a line from the required cell. /// Getting a line from the required cell.
fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<String> { fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<String> {
@@ -74,92 +98,69 @@ fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<Str
/// Obtaining the boundaries of the cell along its upper left coordinate. /// Obtaining the boundaries of the cell along its upper left coordinate.
fn get_merge_from_start(worksheet: &WorkSheet, row: u32, column: u32) -> ((u32, u32), (u32, u32)) { fn get_merge_from_start(worksheet: &WorkSheet, row: u32, column: u32) -> ((u32, u32), (u32, u32)) {
let worksheet_end = worksheet.end().unwrap(); return match worksheet
.merges
let row_end: u32 = { .iter()
let mut r: u32 = 0; .find(|merge| merge.start.0 == row && merge.start.1 == column)
{
for _r in (row + 1)..worksheet_end.0 { Some(merge) => (merge.start, (merge.end.0 + 1, merge.end.1 + 1)),
r = _r; None => ((row, column), (row + 1, column + 1))
if let Some(_) = worksheet.get((_r as usize, column as usize)) {
break;
}
}
r
}; };
let column_end: u32 = {
let mut c: u32 = 0;
for _c in (column + 1)..worksheet_end.1 {
c = _c;
if let Some(_) = worksheet.get((row as usize, _c as usize)) {
break;
}
}
c
};
((row, column), (row_end, column_end))
} }
/// Obtaining a "skeleton" schedule from the working sheet. /// Obtaining a "skeleton" schedule from the working sheet.
fn parse_skeleton(worksheet: &WorkSheet) -> Result<(Vec<InternalId>, Vec<InternalId>), ParseError> { fn parse_skeleton(
let range = &worksheet; worksheet: &WorkSheet,
) -> Result<(Vec<DayCellInfo>, Vec<GroupCellInfo>), ParseError> {
let mut groups: Vec<GroupCellInfo> = Vec::new();
let mut days: Vec<DayCellInfo> = Vec::new();
let mut is_parsed = false; let worksheet_start = worksheet.start().ok_or(ParseError::UnknownWorkSheetRange)?;
let worksheet_end = worksheet.end().ok_or(ParseError::UnknownWorkSheetRange)?;
let mut groups: Vec<InternalId> = Vec::new(); let mut row = worksheet_start.0;
let mut days: Vec<InternalId> = Vec::new();
let start = range.start().ok_or(ParseError::UnknownWorkSheetRange)?; while row < worksheet_end.0 {
let end = range.end().ok_or(ParseError::UnknownWorkSheetRange)?;
let mut row = start.0;
while row < end.0 {
row += 1; row += 1;
let day_name_opt = get_string_from_cell(&worksheet, row, 0); let day_full_name = or_continue!(get_string_from_cell(&worksheet, row, 0));
if day_name_opt.is_none() {
continue;
}
let day_name = day_name_opt.unwrap();
if !is_parsed {
is_parsed = true;
// parse groups row when days column will found
if groups.is_empty() {
// переход на предыдущую строку
row -= 1; row -= 1;
for column in (start.1 + 2)..=end.1 { for column in (worksheet_start.1 + 2)..=worksheet_end.1 {
let group_name = get_string_from_cell(&worksheet, row, column); groups.push(GroupCellInfo {
if group_name.is_none() {
continue;
}
groups.push(InternalId {
row,
column, column,
name: group_name.unwrap(), name: or_continue!(get_string_from_cell(&worksheet, row, column)),
}); });
} }
// возврат на текущую строку
row += 1; row += 1;
} }
days.push(InternalId { let (day_name, day_date) = {
let space_index = day_full_name.find(' ').unwrap();
let name = day_full_name[..space_index].to_string();
let date_raw = day_full_name[space_index + 1..].to_string();
let date_add = format!("{} 00:00:00", date_raw);
let date =
or_break!(NaiveDateTime::parse_from_str(&*date_add, "%d.%m.%Y %H:%M:%S").ok());
(name, date.and_utc())
};
days.push(DayCellInfo {
row, row,
column: 0, column: 0,
name: day_name.clone(), name: day_name,
date: day_date,
}); });
if days.len() > 2 && day_name.starts_with("Суббота") {
break;
}
} }
Ok((days, groups)) Ok((days, groups))
@@ -238,106 +239,104 @@ fn guess_lesson_type(name: &String) -> Option<(String, LessonType)> {
fn parse_lesson( fn parse_lesson(
worksheet: &WorkSheet, worksheet: &WorkSheet,
day: &mut Day, day: &mut Day,
day_times: &Vec<InternalTime>, day_boundaries: &Vec<BoundariesCellInfo>,
time: &InternalTime, lesson_boundaries: &BoundariesCellInfo,
column: u32, column: u32,
) -> Result<LessonParseResult, ParseError> { ) -> Result<LessonParseResult, ParseError> {
let row = time.xls_range.0.0; let row = lesson_boundaries.xls_range.0.0;
let (name, lesson_type) = { let (name, lesson_type) = {
let raw_name_opt = get_string_from_cell(&worksheet, row, column); let full_name = match get_string_from_cell(&worksheet, row, column) {
if raw_name_opt.is_none() { Some(x) => x,
return Ok(Lessons(Vec::new())); None => return Ok(Lessons(Vec::new())),
} };
let raw_name = raw_name_opt.unwrap();
static OTHER_STREET_RE: LazyLock<Regex> = static OTHER_STREET_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+,?\s?[0-9]+$").unwrap()); LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+,?\s?[0-9]+$").unwrap());
if OTHER_STREET_RE.is_match(&raw_name) { if OTHER_STREET_RE.is_match(&full_name) {
return Ok(Street(raw_name)); return Ok(Street(full_name));
} }
if let Some(guess) = guess_lesson_type(&raw_name) { match guess_lesson_type(&full_name) {
guess Some(x) => x,
} else { None => (full_name, lesson_boundaries.lesson_type.clone()),
(raw_name, time.lesson_type.clone())
} }
}; };
let (default_range, lesson_time) = || -> Result<(Option<[u8; 2]>, LessonTime), ParseError> { let (default_range, lesson_time) = {
// check if multi-lesson
let cell_range = get_merge_from_start(worksheet, row, column); let cell_range = get_merge_from_start(worksheet, row, column);
let end_time_arr = day_times let end_time_arr = day_boundaries
.iter() .iter()
.filter(|time| time.xls_range.1.0 == cell_range.1.0) .filter(|time| time.xls_range.1.0 == cell_range.1.0)
.collect::<Vec<&InternalTime>>(); .collect::<Vec<&BoundariesCellInfo>>();
let end_time = end_time_arr let end_time = end_time_arr
.first() .first()
.ok_or(ParseError::LessonTimeNotFound(ErrorCellPos { row, column }))?; .ok_or(ParseError::LessonTimeNotFound(ErrorCellPos { row, column }))?;
let range: Option<[u8; 2]> = if time.default_index != None { let range: Option<[u8; 2]> = if lesson_boundaries.default_index != None {
let default = time.default_index.unwrap() as u8; let default = lesson_boundaries.default_index.unwrap() as u8;
Some([default, end_time.default_index.unwrap() as u8]) Some([default, end_time.default_index.unwrap() as u8])
} else { } else {
None None
}; };
let time = LessonTime { let time = LessonBoundaries {
start: time.time_range.start, start: lesson_boundaries.time_range.start,
end: end_time.time_range.end, end: end_time.time_range.end,
}; };
Ok((range, time)) Ok((range, time))
}()?; }?;
let (name, mut subgroups) = parse_name_and_subgroups(&name)?; let (name, mut subgroups) = parse_name_and_subgroups(&name)?;
{ {
let cabinets: Vec<String> = parse_cabinets(worksheet, row, column + 1); let cabinets: Vec<String> = parse_cabinets(worksheet, row, column + 1);
// Если количество кабинетов равно 1, назначаем этот кабинет всем подгруппам match cabinets.len() {
if cabinets.len() == 1 { // Если кабинетов нет, но есть подгруппы, назначаем им кабинет "??"
for subgroup in &mut subgroups { 0 => {
subgroup.cabinet = Some(cabinets.get(0).or(Some(&String::new())).unwrap().clone()) for subgroup in &mut subgroups {
subgroup.cabinet = Some("??".to_string());
}
} }
} // Назначаем этот кабинет всем подгруппам
// Если количество кабинетов совпадает с количеством подгрупп, назначаем кабинеты по порядку 1 => {
else if cabinets.len() == subgroups.len() { for subgroup in &mut subgroups {
for subgroup in &mut subgroups { subgroup.cabinet =
subgroup.cabinet = Some( Some(cabinets.get(0).or(Some(&String::new())).unwrap().clone())
cabinets }
.get((subgroup.number - 1) as usize)
.unwrap()
.clone(),
);
}
}
// Если количество кабинетов больше количества подгрупп, делаем ещё одну подгруппу.
else if cabinets.len() > subgroups.len() {
for index in 0..subgroups.len() {
subgroups[index].cabinet = Some(cabinets[index].clone());
} }
len => {
// Если количество кабинетов совпадает с количеством подгрупп, назначаем кабинеты по порядку
if len == subgroups.len() {
for subgroup in &mut subgroups {
subgroup.cabinet = Some(
cabinets
.get((subgroup.number - 1) as usize)
.unwrap()
.clone(),
);
}
// Если количество кабинетов больше количества подгрупп, делаем ещё одну подгруппу.
} else if len > subgroups.len() {
for index in 0..subgroups.len() {
subgroups[index].cabinet = Some(cabinets[index].clone());
}
while cabinets.len() > subgroups.len() { while cabinets.len() > subgroups.len() {
subgroups.push(LessonSubGroup { subgroups.push(LessonSubGroup {
number: (subgroups.len() + 1) as u8, number: (subgroups.len() + 1) as u8,
cabinet: Some(cabinets[subgroups.len()].clone()), cabinet: Some(cabinets[subgroups.len()].clone()),
teacher: "Ошибка в расписании".to_string(), teacher: "Ошибка в расписании".to_string(),
}); });
}
}
} }
} };
// Если кабинетов нет, но есть подгруппы, назначаем им значение "??"
else {
for subgroup in &mut subgroups {
subgroup.cabinet = Some("??".to_string());
}
}
cabinets
}; };
let lesson = Lesson { let lesson = Lesson {
@@ -349,7 +348,7 @@ fn parse_lesson(
group: None, group: None,
}; };
let prev_lesson = if day.lessons.len() == 0 { let prev_lesson = if day.lessons.is_empty() {
return Ok(Lessons(Vec::from([lesson]))); return Ok(Lessons(Vec::from([lesson])));
} else { } else {
&day.lessons[day.lessons.len() - 1] &day.lessons[day.lessons.len() - 1]
@@ -360,7 +359,7 @@ fn parse_lesson(
lesson_type: Break, lesson_type: Break,
default_range: None, default_range: None,
name: None, name: None,
time: LessonTime { time: LessonBoundaries {
start: prev_lesson.time.end, start: prev_lesson.time.end,
end: lesson.time.start, end: lesson.time.start,
}, },
@@ -474,6 +473,122 @@ fn parse_name_and_subgroups(name: &String) -> Result<(String, Vec<LessonSubGroup
Ok((lesson_name, subgroups)) Ok((lesson_name, subgroups))
} }
fn parse_lesson_boundaries_cell(
cell_data: &String,
date: DateTime<Utc>,
) -> Option<LessonBoundaries> {
static TIME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
let parse_res = if let Some(captures) = TIME_RE.captures(cell_data) {
captures
} else {
return None;
};
let start_match = parse_res.get(1).unwrap().as_str();
let start_parts: Vec<&str> = start_match.split(".").collect();
let end_match = parse_res.get(2).unwrap().as_str();
let end_parts: Vec<&str> = end_match.split(".").collect();
static GET_TIME: fn(DateTime<Utc>, &Vec<&str>) -> DateTime<Utc> = |date, parts| {
date + Duration::hours(parts[0].parse::<i64>().unwrap() - 4)
+ Duration::minutes(parts[1].parse::<i64>().unwrap())
};
Some(LessonBoundaries {
start: GET_TIME(date.clone(), &start_parts),
end: GET_TIME(date, &end_parts),
})
}
fn parse_day_boundaries_column(
worksheet: &WorkSheet,
day_markup: &DayCellInfo,
lesson_time_column: u32,
row_distance: u32,
) -> Result<Vec<BoundariesCellInfo>, ParseError> {
let mut day_times: Vec<BoundariesCellInfo> = Vec::new();
for row in day_markup.row..(day_markup.row + row_distance) {
let time_cell = if let Some(str) = get_string_from_cell(&worksheet, row, lesson_time_column)
{
str
} else {
continue;
};
let lesson_time = parse_lesson_boundaries_cell(&time_cell, day_markup.date.clone()).ok_or(
ParseError::LessonBoundaries(ErrorCell::new(
row,
lesson_time_column,
time_cell.clone(),
)),
)?;
// type
let lesson_type = if time_cell.contains("пара") {
LessonType::Default
} else {
LessonType::Additional
};
// lesson index
let default_index = if lesson_type == LessonType::Default {
Some(
time_cell
.chars()
.next()
.unwrap()
.to_string()
.parse::<u32>()
.unwrap(),
)
} else {
None
};
day_times.push(BoundariesCellInfo {
time_range: lesson_time,
lesson_type,
default_index,
xls_range: get_merge_from_start(&worksheet, row, lesson_time_column),
});
}
return Ok(day_times);
}
fn parse_week_boundaries_column(
worksheet: &WorkSheet,
week_markup: &Vec<DayCellInfo>,
) -> Result<Vec<Vec<BoundariesCellInfo>>, ParseError> {
let mut result: Vec<Vec<BoundariesCellInfo>> = Vec::new();
let worksheet_end_row = worksheet.end().unwrap().0;
let lesson_time_column = week_markup[0].column + 1;
for day_index in 0..week_markup.len() {
let day_markup = &week_markup[day_index];
// Если текущий день не последнему, то индекс строки следующего дня минус индекс строки текущего дня.
// Если текущий день - последний, то индекс последней строки документа минус индекс строки текущего дня.
let row_distance = if day_index != week_markup.len() - 1 {
week_markup[day_index + 1].row
} else {
worksheet_end_row
} - day_markup.row;
let day_boundaries =
parse_day_boundaries_column(&worksheet, day_markup, lesson_time_column, row_distance)?;
result.push(day_boundaries);
}
Ok(result)
}
/// Conversion of the list of couples of groups in the list of lessons of teachers. /// Conversion of the list of couples of groups in the list of lessons of teachers.
fn convert_groups_to_teachers( fn convert_groups_to_teachers(
groups: &HashMap<String, ScheduleEntry>, groups: &HashMap<String, ScheduleEntry>,
@@ -562,11 +677,11 @@ fn convert_groups_to_teachers(
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use schedule_parser_rusted::parser::parse_xls; /// use schedule_parser::parse_xls;
/// ///
/// let result = parse_xls(&include_bytes!("../../schedule.xls").to_vec()); /// let result = parse_xls(&include_bytes!("../../schedule.xls").to_vec());
/// ///
/// assert!(result.is_ok()); /// assert!(result.is_ok(), "{}", result.err().unwrap());
/// ///
/// assert_ne!(result.as_ref().unwrap().groups.len(), 0); /// assert_ne!(result.as_ref().unwrap().groups.len(), 0);
/// assert_ne!(result.as_ref().unwrap().teachers.len(), 0); /// assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
@@ -576,19 +691,27 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParseResult, ParseError> {
let mut workbook: Xls<_> = let mut workbook: Xls<_> =
open_workbook_from_rs(cursor).map_err(|e| ParseError::BadXLS(std::sync::Arc::new(e)))?; open_workbook_from_rs(cursor).map_err(|e| ParseError::BadXLS(std::sync::Arc::new(e)))?;
let worksheet: WorkSheet = workbook let worksheet = {
.worksheets() let (worksheet_name, worksheet) = workbook
.first() .worksheets()
.ok_or(ParseError::NoWorkSheets)? .first()
.1 .ok_or(ParseError::NoWorkSheets)?
.to_owned(); .clone();
let (days_markup, groups_markup) = parse_skeleton(&worksheet)?; let worksheet_merges = workbook
.worksheet_merge_cells(&*worksheet_name)
.ok_or(ParseError::NoWorkSheets)?;
WorkSheet {
data: worksheet,
merges: worksheet_merges,
}
};
let (week_markup, groups_markup) = parse_skeleton(&worksheet)?;
let week_boundaries = parse_week_boundaries_column(&worksheet, &week_markup)?;
let mut groups: HashMap<String, ScheduleEntry> = HashMap::new(); let mut groups: HashMap<String, ScheduleEntry> = HashMap::new();
let mut days_times: Vec<Vec<InternalTime>> = Vec::new();
let saturday_end_row = worksheet.end().unwrap().0;
for group_markup in groups_markup { for group_markup in groups_markup {
let mut group = ScheduleEntry { let mut group = ScheduleEntry {
@@ -596,118 +719,28 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParseResult, ParseError> {
days: Vec::new(), days: Vec::new(),
}; };
for day_index in 0..(&days_markup).len() { for day_index in 0..(&week_markup).len() {
let day_markup = &days_markup[day_index]; let day_markup = &week_markup[day_index];
let mut day = { let mut day = Day {
let space_index = day_markup.name.find(' ').unwrap(); name: day_markup.name.clone(),
street: None,
let name = day_markup.name[..space_index].to_string(); date: day_markup.date,
lessons: Vec::new(),
let date_raw = day_markup.name[space_index + 1..].to_string();
let date_add = format!("{} 00:00:00", date_raw);
let date = NaiveDateTime::parse_from_str(&*date_add, "%d.%m.%Y %H:%M:%S");
Day {
name,
street: None,
date: date.unwrap().and_utc(),
lessons: Vec::new(),
}
}; };
let lesson_time_column = days_markup[0].column + 1; let day_boundaries = &week_boundaries[day_index];
let row_distance = if day_index != days_markup.len() - 1 { for lesson_boundaries in day_boundaries {
days_markup[day_index + 1].row
} else {
saturday_end_row
} - day_markup.row;
if days_times.len() != 6 {
let mut day_times: Vec<InternalTime> = Vec::new();
for row in day_markup.row..(day_markup.row + row_distance) {
// time
let time_opt = get_string_from_cell(&worksheet, row, lesson_time_column);
if time_opt.is_none() {
continue;
}
let time = time_opt.unwrap();
// type
let lesson_type = if time.contains("пара") {
LessonType::Default
} else {
LessonType::Additional
};
// lesson index
let default_index = if lesson_type == LessonType::Default {
Some(
time.chars()
.next()
.unwrap()
.to_string()
.parse::<u32>()
.unwrap(),
)
} else {
None
};
// time
let time_range = {
static TIME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
let parse_res = TIME_RE.captures(&time).ok_or(ParseError::GlobalTime(
ErrorCell::new(row, lesson_time_column, time.clone()),
))?;
let start_match = parse_res.get(1).unwrap().as_str();
let start_parts: Vec<&str> = start_match.split(".").collect();
let end_match = parse_res.get(2).unwrap().as_str();
let end_parts: Vec<&str> = end_match.split(".").collect();
static GET_TIME: fn(DateTime<Utc>, &Vec<&str>) -> DateTime<Utc> =
|date, parts| {
date + Duration::hours(parts[0].parse::<i64>().unwrap() - 4)
+ Duration::minutes(parts[1].parse::<i64>().unwrap())
};
LessonTime {
start: GET_TIME(day.date.clone(), &start_parts),
end: GET_TIME(day.date.clone(), &end_parts),
}
};
day_times.push(InternalTime {
time_range,
lesson_type,
default_index,
xls_range: get_merge_from_start(&worksheet, row, lesson_time_column),
});
}
days_times.push(day_times);
}
let day_times = &days_times[day_index];
for time in day_times {
match &mut parse_lesson( match &mut parse_lesson(
&worksheet, &worksheet,
&mut day, &mut day,
&day_times, &day_boundaries,
&time, &lesson_boundaries,
group_markup.column, group_markup.column,
)? { )? {
Lessons(l) => day.lessons.append(l), Lessons(lesson) => day.lessons.append(lesson),
Street(s) => day.street = Some(s.to_owned()), Street(street) => day.street = Some(street.to_owned()),
} }
} }
@@ -723,21 +756,39 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParseResult, ParseError> {
}) })
} }
#[cfg(test)] #[cfg(any(test, feature = "test-utils"))]
pub mod tests { pub mod test_utils {
use super::*; use super::*;
pub fn test_result() -> Result<ParseResult, ParseError> { pub fn test_result() -> Result<ParseResult, ParseError> {
parse_xls(&include_bytes!("../../schedule.xls").to_vec()) parse_xls(&include_bytes!("../../schedule.xls").to_vec())
} }
}
#[cfg(test)]
pub mod tests {
#[test] #[test]
fn read() { fn read() {
let result = test_result(); let result = super::test_utils::test_result();
assert!(result.is_ok()); assert!(result.is_ok(), "{}", result.err().unwrap());
assert_ne!(result.as_ref().unwrap().groups.len(), 0); assert_ne!(result.as_ref().unwrap().groups.len(), 0);
assert_ne!(result.as_ref().unwrap().teachers.len(), 0); assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
} }
#[test]
fn test_split_lesson() {
let result = super::test_utils::test_result();
assert!(result.is_ok(), "{}", result.err().unwrap());
let result = result.unwrap();
assert!(result.groups.contains_key("ИС-214/23"));
let group = result.groups.get("ИС-214/23").unwrap();
let thursday = group.days.get(3).unwrap();
assert_eq!(thursday.lessons.len(), 1);
assert_eq!(thursday.lessons[0].default_range.unwrap()[1], 3);
}
} }

View File

@@ -0,0 +1,25 @@
#[macro_export]
macro_rules! or_continue {
( $e:expr ) => {
{
if let Some(x) = $e {
x
} else {
continue;
}
}
}
}
#[macro_export]
macro_rules! or_break {
( $e:expr ) => {
{
if let Some(x) = $e {
x
} else {
break;
}
}
}
}

View File

@@ -8,7 +8,7 @@ use utoipa::ToSchema;
/// The beginning and end of the lesson. /// The beginning and end of the lesson.
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)] #[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct LessonTime { pub struct LessonBoundaries {
/// The beginning of a lesson. /// The beginning of a lesson.
pub start: DateTime<Utc>, pub start: DateTime<Utc>,
@@ -72,7 +72,7 @@ pub struct Lesson {
pub name: Option<String>, pub name: Option<String>,
/// The beginning and end. /// The beginning and end.
pub time: LessonTime, pub time: LessonBoundaries,
/// List of subgroups. /// List of subgroups.
#[serde(rename = "subGroups")] #[serde(rename = "subGroups")]
@@ -153,9 +153,9 @@ pub enum ParseError {
#[display("There is no data on work sheet boundaries.")] #[display("There is no data on work sheet boundaries.")]
UnknownWorkSheetRange, UnknownWorkSheetRange,
/// Failed to read the beginning and end of the lesson from the line /// Failed to read the beginning and end of the lesson from the cell
#[display("Failed to read lesson start and end times from {_0}.")] #[display("Failed to read lesson start and end from {_0}.")]
GlobalTime(ErrorCell), LessonBoundaries(ErrorCell),
/// Not found the beginning and the end corresponding to the lesson. /// Not found the beginning and the end corresponding to the lesson.
#[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.")]
@@ -173,7 +173,7 @@ impl Serialize for ParseError {
ParseError::UnknownWorkSheetRange => { ParseError::UnknownWorkSheetRange => {
serializer.serialize_str("UNKNOWN_WORK_SHEET_RANGE") serializer.serialize_str("UNKNOWN_WORK_SHEET_RANGE")
} }
ParseError::GlobalTime(_) => serializer.serialize_str("GLOBAL_TIME"), ParseError::LessonBoundaries(_) => serializer.serialize_str("GLOBAL_TIME"),
ParseError::LessonTimeNotFound(_) => serializer.serialize_str("LESSON_TIME_NOT_FOUND"), ParseError::LessonTimeNotFound(_) => serializer.serialize_str("LESSON_TIME_NOT_FOUND"),
} }
} }

Binary file not shown.

View File

@@ -1,4 +1,4 @@
use crate::parser::schema::ParseResult; use schedule_parser::schema::ParseResult;
use crate::utility::hasher::DigestHasher; use crate::utility::hasher::DigestHasher;
use crate::xls_downloader::basic_impl::BasicXlsDownloader; use crate::xls_downloader::basic_impl::BasicXlsDownloader;
use actix_web::web; use actix_web::web;

View File

@@ -1 +0,0 @@
pub mod parser;

View File

@@ -13,7 +13,6 @@ mod app_state;
mod database; mod database;
mod parser;
mod xls_downloader; mod xls_downloader;
mod extractors; mod extractors;
@@ -112,8 +111,6 @@ fn main() -> io::Result<()> {
}, },
)); ));
unsafe { std::env::set_var("RUST_BACKTRACE", "1") };
dotenv().unwrap(); dotenv().unwrap();
env_logger::init(); env_logger::init();

View File

@@ -1,9 +1,6 @@
use crate::utility::jwt::DEFAULT_ALGORITHM;
use jsonwebtoken::errors::ErrorKind; use jsonwebtoken::errors::ErrorKind;
use jsonwebtoken::{decode, DecodingKey, Validation}; use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env;
use std::sync::LazyLock;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct TokenData { struct TokenData {
@@ -17,7 +14,7 @@ struct TokenData {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Claims { struct Claims {
sub: String, sub: i32,
iis: String, iis: String,
jti: i32, jti: i32,
app: i32, app: i32,
@@ -52,17 +49,10 @@ const VK_PUBLIC_KEY: &str = concat!(
"-----END PUBLIC KEY-----" "-----END PUBLIC KEY-----"
); );
static VK_ID_CLIENT_ID: LazyLock<i32> = LazyLock::new(|| { pub fn parse_vk_id(token_str: &String, client_id: i32) -> Result<i32, Error> {
env::var("VK_ID_CLIENT_ID")
.expect("VK_ID_CLIENT_ID must be set")
.parse::<i32>()
.expect("VK_ID_CLIENT_ID must be i32")
});
pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap(); let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
match decode::<Claims>(&token_str, &dkey, &Validation::new(DEFAULT_ALGORITHM)) { match decode::<Claims>(&token_str, &dkey, &Validation::new(Algorithm::RS256)) {
Ok(token_data) => { Ok(token_data) => {
let claims = token_data.claims; let claims = token_data.claims;
@@ -70,13 +60,10 @@ pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
Err(Error::UnknownIssuer(claims.iis)) Err(Error::UnknownIssuer(claims.iis))
} else if claims.jti != 21 { } else if claims.jti != 21 {
Err(Error::UnknownType(claims.jti)) Err(Error::UnknownType(claims.jti))
} else if claims.app != *VK_ID_CLIENT_ID { } else if claims.app != client_id {
Err(Error::UnknownClientId(claims.app)) Err(Error::UnknownClientId(claims.app))
} else { } else {
match claims.sub.parse::<i32>() { Ok(claims.sub)
Ok(sub) => Ok(sub),
Err(_) => Err(Error::InvalidToken),
}
} }
} }
Err(err) => Err(match err.into_kind() { Err(err) => Err(match err.into_kind() {

View File

@@ -71,7 +71,7 @@ pub async fn sign_in_vk(
) -> ServiceResponse { ) -> ServiceResponse {
let data = data_json.into_inner(); let data = data_json.into_inner();
match parse_vk_id(&data.access_token) { match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
Ok(id) => sign_in_combined(Vk(id), &app_state).await.into(), Ok(id) => sign_in_combined(Vk(id), &app_state).await.into(),
Err(_) => ErrorCode::InvalidVkAccessToken.into_response(), Err(_) => ErrorCode::InvalidVkAccessToken.into_response(),
} }
@@ -150,7 +150,7 @@ mod tests {
use std::fmt::Write; use std::fmt::Write;
async fn sign_in_client(data: Request) -> ServiceResponse { async fn sign_in_client(data: Request) -> ServiceResponse {
let app = test_app(test_app_state().await, sign_in).await; let app = test_app(test_app_state(Default::default()).await, sign_in).await;
let req = test::TestRequest::with_uri("/sign-in") let req = test::TestRequest::with_uri("/sign-in")
.method(Method::POST) .method(Method::POST)

View File

@@ -79,7 +79,7 @@ pub async fn sign_up_vk(
) -> ServiceResponse { ) -> ServiceResponse {
let data = data_json.into_inner(); let data = data_json.into_inner();
match parse_vk_id(&data.access_token) { match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
Ok(id) => sign_up_combined( Ok(id) => sign_up_combined(
SignUpData { SignUpData {
username: data.username, username: data.username,
@@ -241,7 +241,9 @@ mod tests {
use crate::database::models::UserRole; use crate::database::models::UserRole;
use crate::routes::auth::sign_up::schema::Request; use crate::routes::auth::sign_up::schema::Request;
use crate::routes::auth::sign_up::sign_up; use crate::routes::auth::sign_up::sign_up;
use crate::test_env::tests::{static_app_state, test_app_state, test_env}; use crate::test_env::tests::{
TestAppStateParams, TestScheduleType, static_app_state, test_app_state, test_env,
};
use actix_test::test_app; use actix_test::test_app;
use actix_web::dev::ServiceResponse; use actix_web::dev::ServiceResponse;
use actix_web::http::Method; use actix_web::http::Method;
@@ -252,10 +254,22 @@ mod tests {
username: String, username: String,
group: String, group: String,
role: UserRole, role: UserRole,
load_schedule: bool,
} }
async fn sign_up_client(data: SignUpPartial) -> ServiceResponse { async fn sign_up_client(data: SignUpPartial) -> ServiceResponse {
let app = test_app(test_app_state().await, sign_up).await; let app = test_app(
test_app_state(TestAppStateParams {
schedule: if data.load_schedule {
TestScheduleType::Local
} else {
TestScheduleType::None
},
})
.await,
sign_up,
)
.await;
let req = test::TestRequest::with_uri("/sign-up") let req = test::TestRequest::with_uri("/sign-up")
.method(Method::POST) .method(Method::POST)
@@ -286,6 +300,7 @@ mod tests {
username: "test::sign_up_valid".to_string(), username: "test::sign_up_valid".to_string(),
group: "ИС-214/23".to_string(), group: "ИС-214/23".to_string(),
role: UserRole::Student, role: UserRole::Student,
load_schedule: false,
}) })
.await; .await;
@@ -305,6 +320,7 @@ mod tests {
username: "test::sign_up_multiple".to_string(), username: "test::sign_up_multiple".to_string(),
group: "ИС-214/23".to_string(), group: "ИС-214/23".to_string(),
role: UserRole::Student, role: UserRole::Student,
load_schedule: false,
}) })
.await; .await;
@@ -314,6 +330,7 @@ mod tests {
username: "test::sign_up_multiple".to_string(), username: "test::sign_up_multiple".to_string(),
group: "ИС-214/23".to_string(), group: "ИС-214/23".to_string(),
role: UserRole::Student, role: UserRole::Student,
load_schedule: false,
}) })
.await; .await;
@@ -329,6 +346,7 @@ mod tests {
username: "test::sign_up_invalid_role".to_string(), username: "test::sign_up_invalid_role".to_string(),
group: "ИС-214/23".to_string(), group: "ИС-214/23".to_string(),
role: UserRole::Admin, role: UserRole::Admin,
load_schedule: false,
}) })
.await; .await;
@@ -344,6 +362,7 @@ mod tests {
username: "test::sign_up_invalid_group".to_string(), username: "test::sign_up_invalid_group".to_string(),
group: "invalid_group".to_string(), group: "invalid_group".to_string(),
role: UserRole::Student, role: UserRole::Student,
load_schedule: true,
}) })
.await; .await;

View File

@@ -39,7 +39,7 @@ pub async fn group(user: SyncExtractor<User>, app_state: web::Data<AppState>) ->
} }
mod schema { mod schema {
use crate::parser::schema::ScheduleEntry; use schedule_parser::schema::ScheduleEntry;
use actix_macros::{IntoResponseErrorNamed, StatusCode}; use actix_macros::{IntoResponseErrorNamed, StatusCode};
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use derive_more::Display; use derive_more::Display;

View File

@@ -2,9 +2,9 @@ mod cache_status;
mod group; mod group;
mod group_names; mod group_names;
mod schedule; mod schedule;
mod schema;
mod teacher; mod teacher;
mod teacher_names; mod teacher_names;
mod schema;
mod update_download_url; mod update_download_url;
pub use cache_status::*; pub use cache_status::*;

View File

@@ -1,5 +1,5 @@
use crate::app_state::{AppState, Schedule}; use crate::app_state::{AppState, Schedule};
use crate::parser::schema::ScheduleEntry; use schedule_parser::schema::ScheduleEntry;
use actix_macros::{IntoResponseErrorNamed, ResponderJson, StatusCode}; use actix_macros::{IntoResponseErrorNamed, ResponderJson, StatusCode};
use actix_web::web; use actix_web::web;
use chrono::{DateTime, Duration, Utc}; use chrono::{DateTime, Duration, Utc};
@@ -99,7 +99,7 @@ impl From<&Schedule> for CacheStatus {
fn from(value: &Schedule) -> Self { fn from(value: &Schedule) -> Self {
Self { Self {
cache_hash: value.hash(), cache_hash: value.hash(),
cache_update_required: (value.fetched_at - Utc::now()) > Duration::minutes(5), cache_update_required: (Utc::now() - value.fetched_at) > Duration::minutes(5),
last_cache_update: value.fetched_at.timestamp(), last_cache_update: value.fetched_at.timestamp(),
last_schedule_update: value.updated_at.timestamp(), last_schedule_update: value.updated_at.timestamp(),
} }

View File

@@ -40,7 +40,7 @@ pub async fn teacher(
} }
mod schema { mod schema {
use crate::parser::schema::ScheduleEntry; use schedule_parser::schema::ScheduleEntry;
use actix_macros::{IntoResponseErrorNamed, StatusCode}; use actix_macros::{IntoResponseErrorNamed, StatusCode};
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use derive_more::Display; use derive_more::Display;

View File

@@ -1,10 +1,10 @@
use self::schema::*; use self::schema::*;
use crate::AppState; use crate::AppState;
use crate::app_state::Schedule; use crate::app_state::Schedule;
use crate::parser::parse_xls; use schedule_parser::parse_xls;
use crate::routes::schedule::schema::CacheStatus; use crate::routes::schedule::schema::CacheStatus;
use crate::routes::schema::{IntoResponseAsError, ResponseError}; use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::xls_downloader::interface::XLSDownloader; use crate::xls_downloader::interface::{FetchError, XLSDownloader};
use actix_web::web::Json; use actix_web::web::Json;
use actix_web::{patch, web}; use actix_web::{patch, web};
use chrono::Utc; use chrono::Utc;
@@ -60,29 +60,32 @@ pub async fn update_download_url(
} }
}, },
Err(error) => { Err(error) => {
eprintln!("Unknown url provided {}", data.url); if let FetchError::Unknown(error) = &error {
eprintln!("{:?}", error); sentry::capture_error(&error);
}
ErrorCode::DownloadFailed.into_response() ErrorCode::DownloadFailed(error).into_response()
} }
} }
} }
Err(error) => { Err(error) => {
eprintln!("Unknown url provided {}", data.url); if let FetchError::Unknown(error) = &error {
eprintln!("{:?}", error); sentry::capture_error(&error);
}
ErrorCode::FetchFailed.into_response() ErrorCode::FetchFailed(error).into_response()
} }
} }
} }
mod schema { mod schema {
use crate::parser::schema::ParseError; use schedule_parser::schema::ParseError;
use crate::routes::schedule::schema::CacheStatus; use crate::routes::schedule::schema::CacheStatus;
use actix_macros::{IntoResponseErrorNamed, StatusCode}; use actix_macros::{IntoResponseErrorNamed, StatusCode};
use derive_more::Display; use derive_more::Display;
use serde::{Deserialize, Serialize, Serializer}; use serde::{Deserialize, Serialize, Serializer};
use utoipa::ToSchema; use utoipa::ToSchema;
use crate::xls_downloader::interface::FetchError;
pub type ServiceResponse = crate::routes::schema::Response<CacheStatus, ErrorCode>; pub type ServiceResponse = crate::routes::schema::Response<CacheStatus, ErrorCode>;
@@ -101,12 +104,12 @@ mod schema {
NonWhitelistedHost, NonWhitelistedHost,
/// Failed to retrieve file metadata. /// Failed to retrieve file metadata.
#[display("Unable to retrieve metadata from the specified URL.")] #[display("Unable to retrieve metadata from the specified URL: {_0}")]
FetchFailed, FetchFailed(FetchError),
/// Failed to download the file. /// Failed to download the file.
#[display("Unable to retrieve data from the specified URL.")] #[display("Unable to retrieve data from the specified URL: {_0}")]
DownloadFailed, DownloadFailed(FetchError),
/// The link leads to an outdated schedule. /// The link leads to an outdated schedule.
/// ///
@@ -127,8 +130,8 @@ mod schema {
{ {
match self { match self {
ErrorCode::NonWhitelistedHost => serializer.serialize_str("NON_WHITELISTED_HOST"), ErrorCode::NonWhitelistedHost => serializer.serialize_str("NON_WHITELISTED_HOST"),
ErrorCode::FetchFailed => serializer.serialize_str("FETCH_FAILED"), ErrorCode::FetchFailed(_) => serializer.serialize_str("FETCH_FAILED"),
ErrorCode::DownloadFailed => serializer.serialize_str("DOWNLOAD_FAILED"), ErrorCode::DownloadFailed(_) => serializer.serialize_str("DOWNLOAD_FAILED"),
ErrorCode::OutdatedSchedule => serializer.serialize_str("OUTDATED_SCHEDULE"), ErrorCode::OutdatedSchedule => serializer.serialize_str("OUTDATED_SCHEDULE"),
ErrorCode::InvalidSchedule(_) => serializer.serialize_str("INVALID_SCHEDULE"), ErrorCode::InvalidSchedule(_) => serializer.serialize_str("INVALID_SCHEDULE"),
} }

View File

@@ -59,13 +59,16 @@ async fn oauth(data: web::Json<Request>, app_state: web::Data<AppState>) -> Serv
return ErrorCode::VkIdError.into_response(); return ErrorCode::VkIdError.into_response();
} }
if let Ok(auth_data) = res.json::<VkIdAuthResponse>().await { match res.json::<VkIdAuthResponse>().await {
Ok(Response { Ok(auth_data) =>
access_token: auth_data.id_token, Ok(Response {
}) access_token: auth_data.id_token,
.into() }).into(),
} else { Err(error) => {
ErrorCode::VkIdError.into_response() sentry::capture_error(&error);
ErrorCode::VkIdError.into_response()
}
} }
} }
Err(_) => ErrorCode::VkIdError.into_response(), Err(_) => ErrorCode::VkIdError.into_response(),

View File

@@ -1,24 +1,47 @@
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use crate::app_state::{AppState, Schedule, app_state}; use crate::app_state::{AppState, Schedule, app_state};
use crate::parser::tests::test_result; use schedule_parser::test_utils::test_result;
use crate::utility::mutex::MutexScope;
use actix_web::web; use actix_web::web;
use std::default::Default;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
pub fn test_env() { pub fn test_env() {
dotenvy::from_path(".env.test").expect("Failed to load test environment file"); dotenvy::from_path(".env.test").expect("Failed to load test environment file");
} }
pub async fn test_app_state() -> web::Data<AppState> { pub enum TestScheduleType {
let state = app_state().await; None,
let mut schedule_lock = state.schedule.lock().unwrap(); Local,
}
*schedule_lock = Some(Schedule { pub struct TestAppStateParams {
etag: "".to_string(), pub schedule: TestScheduleType,
fetched_at: Default::default(), }
updated_at: Default::default(),
parsed_at: Default::default(), impl Default for TestAppStateParams {
data: test_result().unwrap(), fn default() -> Self {
Self {
schedule: TestScheduleType::None,
}
}
}
pub async fn test_app_state(params: TestAppStateParams) -> web::Data<AppState> {
let state = app_state().await;
state.schedule.scope(|schedule| {
*schedule = match params.schedule {
TestScheduleType::None => None,
TestScheduleType::Local => Some(Schedule {
etag: "".to_string(),
fetched_at: Default::default(),
updated_at: Default::default(),
parsed_at: Default::default(),
data: test_result().unwrap(),
}),
}
}); });
state.clone() state.clone()
@@ -27,6 +50,9 @@ pub(crate) mod tests {
pub async fn static_app_state() -> web::Data<AppState> { pub async fn static_app_state() -> web::Data<AppState> {
static STATE: OnceCell<web::Data<AppState>> = OnceCell::const_new(); static STATE: OnceCell<web::Data<AppState>> = OnceCell::const_new();
STATE.get_or_init(|| test_app_state()).await.clone() STATE
.get_or_init(|| test_app_state(Default::default()))
.await
.clone()
} }
} }

View File

@@ -1,11 +1,14 @@
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader}; use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use std::env;
use std::sync::Arc;
pub struct BasicXlsDownloader { pub struct BasicXlsDownloader {
pub url: Option<String>, pub url: Option<String>,
user_agent: String,
} }
async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchResult { async fn fetch_specified(url: &String, user_agent: &String, head: bool) -> FetchResult {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let response = if head { let response = if head {
@@ -13,14 +16,14 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
} else { } else {
client.get(url) client.get(url)
} }
.header("User-Agent", user_agent) .header("User-Agent", user_agent.clone())
.send() .send()
.await; .await;
match response { match response {
Ok(r) => { Ok(r) => {
if r.status().as_u16() != 200 { if r.status().as_u16() != 200 {
return Err(FetchError::BadStatusCode); return Err(FetchError::BadStatusCode(r.status().as_u16()));
} }
let headers = r.headers(); let headers = r.headers();
@@ -30,11 +33,18 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
let last_modified = headers.get("last-modified"); let last_modified = headers.get("last-modified");
let date = headers.get("date"); let date = headers.get("date");
if content_type.is_none() || etag.is_none() || last_modified.is_none() || date.is_none() if content_type.is_none() {
{ Err(FetchError::BadHeaders("Content-Type".to_string()))
Err(FetchError::BadHeaders) } else if etag.is_none() {
Err(FetchError::BadHeaders("ETag".to_string()))
} else if last_modified.is_none() {
Err(FetchError::BadHeaders("Last-Modified".to_string()))
} else if date.is_none() {
Err(FetchError::BadHeaders("Date".to_string()))
} else if content_type.unwrap() != "application/vnd.ms-excel" { } else if content_type.unwrap() != "application/vnd.ms-excel" {
Err(FetchError::BadContentType) Err(FetchError::BadContentType(
content_type.unwrap().to_str().unwrap().to_string(),
))
} else { } else {
let etag = etag.unwrap().to_str().unwrap().to_string(); let etag = etag.unwrap().to_str().unwrap().to_string();
let last_modified = let last_modified =
@@ -49,13 +59,16 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
}) })
} }
} }
Err(_) => Err(FetchError::Unknown), Err(error) => Err(FetchError::Unknown(Arc::new(error))),
} }
} }
impl BasicXlsDownloader { impl BasicXlsDownloader {
pub fn new() -> Self { pub fn new() -> Self {
BasicXlsDownloader { url: None } BasicXlsDownloader {
url: None,
user_agent: env::var("REQWEST_USER_AGENT").expect("USER_AGENT must be set"),
}
} }
} }
@@ -64,17 +77,12 @@ impl XLSDownloader for BasicXlsDownloader {
if self.url.is_none() { if self.url.is_none() {
Err(FetchError::NoUrlProvided) Err(FetchError::NoUrlProvided)
} else { } else {
fetch_specified( fetch_specified(self.url.as_ref().unwrap(), &self.user_agent, head).await
self.url.as_ref().unwrap(),
"t.me/polytechnic_next".to_string(),
head,
)
.await
} }
} }
async fn set_url(&mut self, url: String) -> FetchResult { async fn set_url(&mut self, url: String) -> FetchResult {
let result = fetch_specified(&url, "t.me/polytechnic_next".to_string(), true).await; let result = fetch_specified(&url, &self.user_agent, true).await;
if let Ok(_) = result { if let Ok(_) = result {
self.url = Some(url); self.url = Some(url);
@@ -86,7 +94,7 @@ impl XLSDownloader for BasicXlsDownloader {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::xls_downloader::basic_impl::{BasicXlsDownloader, fetch_specified}; use crate::xls_downloader::basic_impl::{fetch_specified, BasicXlsDownloader};
use crate::xls_downloader::interface::{FetchError, XLSDownloader}; use crate::xls_downloader::interface::{FetchError, XLSDownloader};
#[tokio::test] #[tokio::test]
@@ -95,8 +103,8 @@ mod tests {
let user_agent = String::new(); let user_agent = String::new();
let results = [ let results = [
fetch_specified(&url, user_agent.clone(), true).await, fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, user_agent.clone(), false).await, fetch_specified(&url, &user_agent, false).await,
]; ];
assert!(results[0].is_err()); assert!(results[0].is_err());
@@ -109,21 +117,17 @@ mod tests {
let user_agent = String::new(); let user_agent = String::new();
let results = [ let results = [
fetch_specified(&url, user_agent.clone(), true).await, fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, user_agent.clone(), false).await, fetch_specified(&url, &user_agent, false).await,
]; ];
assert!(results[0].is_err()); assert!(results[0].is_err());
assert!(results[1].is_err()); assert!(results[1].is_err());
assert_eq!( let expected_error = FetchError::BadStatusCode(404);
*results[0].as_ref().err().unwrap(),
FetchError::BadStatusCode assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
); assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
assert_eq!(
*results[1].as_ref().err().unwrap(),
FetchError::BadStatusCode
);
} }
#[tokio::test] #[tokio::test]
@@ -132,15 +136,17 @@ mod tests {
let user_agent = String::new(); let user_agent = String::new();
let results = [ let results = [
fetch_specified(&url, user_agent.clone(), true).await, fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, user_agent.clone(), false).await, fetch_specified(&url, &user_agent, false).await,
]; ];
assert!(results[0].is_err()); assert!(results[0].is_err());
assert!(results[1].is_err()); assert!(results[1].is_err());
assert_eq!(*results[0].as_ref().err().unwrap(), FetchError::BadHeaders); let expected_error = FetchError::BadHeaders("ETag".to_string());
assert_eq!(*results[1].as_ref().err().unwrap(), FetchError::BadHeaders);
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
} }
#[tokio::test] #[tokio::test]
@@ -149,21 +155,12 @@ mod tests {
let user_agent = String::new(); let user_agent = String::new();
let results = [ let results = [
fetch_specified(&url, user_agent.clone(), true).await, fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, user_agent.clone(), false).await, fetch_specified(&url, &user_agent, false).await,
]; ];
assert!(results[0].is_err()); assert!(results[0].is_err());
assert!(results[1].is_err()); assert!(results[1].is_err());
assert_eq!(
*results[0].as_ref().err().unwrap(),
FetchError::BadContentType
);
assert_eq!(
*results[1].as_ref().err().unwrap(),
FetchError::BadContentType
);
} }
#[tokio::test] #[tokio::test]
@@ -172,8 +169,8 @@ mod tests {
let user_agent = String::new(); let user_agent = String::new();
let results = [ let results = [
fetch_specified(&url, user_agent.clone(), true).await, fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, user_agent.clone(), false).await, fetch_specified(&url, &user_agent, false).await,
]; ];
assert!(results[0].is_ok()); assert!(results[0].is_ok());

View File

@@ -1,22 +1,38 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use derive_more::Display;
use std::mem::discriminant;
use std::sync::Arc;
use utoipa::ToSchema;
/// XLS data retrieval errors. /// XLS data retrieval errors.
#[derive(PartialEq, Debug)] #[derive(Clone, Debug, ToSchema, Display)]
pub enum FetchError { pub enum FetchError {
/// File url is not set. /// File url is not set.
#[display("The link to the timetable was not provided earlier.")]
NoUrlProvided, NoUrlProvided,
/// Unknown error. /// Unknown error.
Unknown, #[display("An unknown error occurred while downloading the file.")]
#[schema(value_type = String)]
Unknown(Arc<reqwest::Error>),
/// Server returned a status code different from 200. /// Server returned a status code different from 200.
BadStatusCode, #[display("Server returned a status code {_0}.")]
BadStatusCode(u16),
/// The url leads to a file of a different type. /// The url leads to a file of a different type.
BadContentType, #[display("The link leads to a file of type '{_0}'.")]
BadContentType(String),
/// Server doesn't return expected headers. /// Server doesn't return expected headers.
BadHeaders, #[display("Server doesn't return expected header(s) '{_0}'.")]
BadHeaders(String),
}
impl PartialEq for FetchError {
fn eq(&self, other: &Self) -> bool {
discriminant(self) == discriminant(other)
}
} }
/// Result of XLS data retrieval. /// Result of XLS data retrieval.