perf: Optimize final build size

This commit is contained in:
2026-02-06 16:17:49 +01:00
parent f8e8077963
commit 0176945bd6
3 changed files with 53 additions and 1198 deletions

1101
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,19 +4,26 @@ version = "0.1.1"
edition = "2024"
[dependencies]
chrono = { version = "0.4.43", features = ["serde"] }
futures = "0.3.31"
once_cell = "1.21.3"
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
chrono = { version = "0.4.39", features = ["serde"] }
minreq = { version = "2.13", features = ["json-using-serde", "https-rustls"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "2.0.18"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.44"
tracing-subscriber = "0.3.22"
android_logger = "0.14"
log = "0.4"
uniffi = { version = "0.27", features = ["cli"] }
thiserror = "2.0.11"
uniffi = { version = "0.27" }
[features]
uniffi-cli = ["uniffi/cli"]
[lib]
crate-type = ["cdylib"]
[[bin]]
name = "uniffi-bindgen"
required-features = ["uniffi-cli"]
[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
strip = true

View File

@@ -1,18 +1,10 @@
use chrono::{Datelike, Local, NaiveDate, Weekday};
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::{Once, RwLock};
use tokio::runtime::Runtime;
use tracing::{debug, error, info};
use std::sync::RwLock;
uniffi::setup_scaffolding!();
static RUNTIME: Lazy<Runtime> =
Lazy::new(|| Runtime::new().expect("Failed to create Tokio runtime"));
static INIT_TRACING: Once = Once::new();
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum SuplError {
#[error("Network error: {reason}")]
@@ -63,31 +55,14 @@ pub struct DailySchedule {
#[derive(uniffi::Object)]
pub struct JecnaSuplClient {
provider_url: RwLock<String>,
client: reqwest::Client,
}
#[uniffi::export]
impl JecnaSuplClient {
#[uniffi::constructor]
pub fn new() -> Self {
INIT_TRACING.call_once(|| {
#[cfg(target_os = "android")]
android_logger::init_once(
android_logger::Config::default()
.with_max_level(log::LevelFilter::Debug)
.with_tag("JecnaSuplClient"),
);
#[cfg(not(target_os = "android"))]
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
});
info!("Initializing JecnaSuplClient");
Self {
provider_url: RwLock::new("https://jecnarozvrh.jzitnik.dev".to_string()),
client: reqwest::Client::new(),
}
}
@@ -102,103 +77,50 @@ impl JecnaSuplClient {
let trimmed = provider.trim_end_matches('/');
format!("{}/versioned/v2", trimmed)
};
debug!("Target URL: {}", url);
let client = self.client.clone();
let class_name_clone = class_name.clone();
let (tx, rx) = std::sync::mpsc::channel();
RUNTIME.spawn(async move {
let result = async {
let resp = client.get(&url).send().await.map_err(|e| {
error!("Network request failed: {}", e);
SuplError::NetworkError {
let resp: ApiResponse = minreq::get(&url)
.send()
.map_err(|e| SuplError::NetworkError {
reason: e.to_string(),
}
})?;
let resp_text = resp.text().await.map_err(|e| {
error!("Failed to read response text: {}", e);
SuplError::NetworkError {
})?
.json()
.map_err(|e| SuplError::ParseError {
reason: e.to_string(),
}
})?;
if resp_text.trim().is_empty() {
error!("Received empty response body from {}", url);
return Err(SuplError::ParseError {
reason: "Empty response body".to_string(),
});
}
let resp: ApiResponse = serde_json::from_str(&resp_text).map_err(|e| {
error!("JSON parse error: {}. Position: line {}, col {}. Response content: {}", e, e.line(), e.column(), resp_text);
SuplError::ParseError {
reason: e.to_string(),
}
})?;
debug!("Successfully parsed response");
Ok::<ApiResponse, SuplError>(resp)
}
.await;
let _ = tx.send(result);
});
let result = rx.recv().map_err(|e| {
error!("Mpsc receive error: {}", e);
SuplError::RuntimeError {
reason: "Channel closed".to_string(),
}
})??;
let today = Local::now().date_naive();
let current_weekday = today.weekday();
debug!("Current date: {}, weekday: {:?}", today, current_weekday);
let start_date;
let end_date;
match current_weekday {
Weekday::Sat | Weekday::Sun => {
// Next Monday
let days_until_mon = 7 - current_weekday.num_days_from_monday();
let next_mon = today + chrono::Duration::days(days_until_mon as i64);
start_date = next_mon;
end_date = next_mon + chrono::Duration::days(4); // Mon -> Fri is 4 days diff
info!(
"Weekend detected. Showing next week: {} to {}",
start_date, end_date
);
end_date = next_mon + chrono::Duration::days(4);
}
_ => {
// Current Monday
let days_from_mon = current_weekday.num_days_from_monday();
let curr_mon = today - chrono::Duration::days(days_from_mon as i64);
start_date = curr_mon;
end_date = curr_mon + chrono::Duration::days(4);
info!(
"Weekday detected. Showing current week: {} to {}",
start_date, end_date
);
}
}
let mut output = Vec::new();
for (i, day_prop) in result.props.iter().enumerate() {
for (i, day_prop) in resp.props.iter().enumerate() {
let date = NaiveDate::parse_from_str(&day_prop.date, "%Y-%m-%d").map_err(|e| {
error!("Date parsing error for {}: {}", day_prop.date, e);
SuplError::DateFormatError {
reason: e.to_string(),
}
})?;
if date >= start_date && date <= end_date {
if let Some(day_schedule_map) = result.schedule.get(i) {
if let Some(class_schedule_val) = day_schedule_map.get(&class_name_clone) {
if let Some(day_schedule_map) = resp.schedule.get(i) {
if let Some(class_schedule_val) = day_schedule_map.get(&class_name) {
match serde_json::from_value::<Vec<Option<String>>>(
class_schedule_val.clone(),
) {
@@ -209,30 +131,21 @@ impl JecnaSuplClient {
});
}
Err(e) => {
error!(
"Failed to parse schedule for class {}: {}",
class_name_clone, e
);
return Err(SuplError::ParseError {
reason: format!(
"Invalid schedule format for class {}",
class_name_clone
"Invalid schedule format for class {}: {}",
class_name, e
),
});
}
}
} else {
debug!(
"Class {} not found in schedule for {}",
class_name_clone, date
);
}
}
}
}
Ok(SuplResult {
status: result.status,
status: resp.status,
schedule: output,
})
}