From 70905955052c1a9bef6cd0f4384dea37fec3dae6 Mon Sep 17 00:00:00 2001 From: jzitnik-dev Date: Mon, 9 Feb 2026 16:47:46 +0100 Subject: [PATCH] feat: Reporting --- src/lib.rs | 8 +++++++ src/models.rs | 2 +- src/report.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/report.rs diff --git a/src/lib.rs b/src/lib.rs index 4e08ed0..dcac59b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,11 +3,14 @@ mod models; mod schedule; mod teacher_absence; mod all; +mod report; use std::sync::RwLock; pub use models::*; +use crate::report::{report_impl, ReportLocation}; + uniffi::setup_scaffolding!(); #[derive(uniffi::Object)] @@ -43,4 +46,9 @@ impl JecnaSuplClient { let provider = self.provider_url.read().unwrap(); all::get_all_impl(&provider) } + + pub fn report(&self, content: String, class: String, report_location: ReportLocation) -> Result<(), SuplError> { + let provider = self.provider_url.read().unwrap(); + report_impl(&provider, content, class, report_location) + } } diff --git a/src/models.rs b/src/models.rs index f613047..619c349 100644 --- a/src/models.rs +++ b/src/models.rs @@ -91,7 +91,7 @@ pub struct DailyData { #[serde(rename = "takesPlace")] pub takes_place: String, #[serde(rename = "reservedRooms")] - pub reserved_rooms: Vec, + pub reserved_rooms: Vec>, } #[derive(Deserialize, Debug, uniffi::Record, Clone)] diff --git a/src/report.rs b/src/report.rs new file mode 100644 index 0000000..4cbcdb9 --- /dev/null +++ b/src/report.rs @@ -0,0 +1,61 @@ +use serde::Serialize; + +use crate::SuplError; + +#[derive(uniffi::Enum)] +pub enum ReportLocation { + Timetable, + TeacherAbsence, + TakesPlace +} + +impl ReportLocation { + fn to_str(&self) -> &'static str { + match self { + ReportLocation::Timetable => "TIMETABLE", + ReportLocation::TeacherAbsence => "ABSENCE", + ReportLocation::TakesPlace => "TAKES_PLACE" + } + } +} + +#[derive(Serialize)] +struct ReportRequest { + content: String, + class: String, + location: String, +} + +pub fn report_impl( + provider: &str, + content: String, + class: String, + report_location: ReportLocation, +) -> Result<(), SuplError> { + let trimmed = provider.trim_end_matches('/'); + let url = format!("{}/versioned/v3", trimmed); + + let body = ReportRequest { + content, + class, + location: report_location.to_str().to_string(), + }; + + let json = serde_json::to_string(&body) + .map_err(|e| SuplError::ParseError { reason: e.to_string() })?; + + let response = minreq::post(url) + .with_header("Content-Type", "application/json") + .with_body(json) + .send() + .map_err(|e| SuplError::NetworkError { reason: e.to_string() })?; + + if response.status_code >= 400 { + return Err(SuplError::RuntimeError { + reason: format!("Server returned HTTP {}", response.status_code), + }); + } + + Ok(()) +} +