Skip to content

Commit

Permalink
fix: update dependencies to recent versions
Browse files Browse the repository at this point in the history
  • Loading branch information
arnimattr committed Aug 31, 2023
1 parent 8d7b2c3 commit 8d1f8f8
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 89 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ license = "MIT"
readme = "README.md"
version = "0.2.1"
authors = ["Leopold Luley <[email protected]>"]
edition = "2021"

[dependencies]
chrono = "0.4"
reqwest = "0.8.5"
reqwest = {version = "0.11.20", features= ["cookies", "json", "blocking"]}
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
22 changes: 12 additions & 10 deletions examples/fetch_all.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
extern crate untis;
extern crate chrono;
extern crate untis;

use chrono::Local;

use untis::Units;

fn main() {
let mut untis = Units::new("server", "school", "user", "password");
let today = Local::today().naive_local();
let today = Local::now().date_naive();

let info = untis.login().expect("Failed to login");

let _statusdata = untis.status_data() .expect("Failed to get status data" );
let _holidays = untis.holidays() .expect("Failed to get holidays" );
let _rooms = untis.rooms() .expect("Failed to get rooms" );
let _classes = untis.classes() .expect("Failed to get classes" );
let _subjects = untis.subjects() .expect("Failed to get subjects" );
let _timetable = untis.timetable(info.class_id, 1, today) .expect("Failed to get timetable" );
let _departments = untis.departments() .expect("Failed to get departments" );
let _statusdata = untis.status_data().expect("Failed to get status data");
let _holidays = untis.holidays().expect("Failed to get holidays");
let _rooms = untis.rooms().expect("Failed to get rooms");
let _classes = untis.classes().expect("Failed to get classes");
let _subjects = untis.subjects().expect("Failed to get subjects");
let _timetable = untis
.timetable(info.class_id, 1, today)
.expect("Failed to get timetable");
let _departments = untis.departments().expect("Failed to get departments");
// teachers

untis.logout().expect("Failed to logout");
Expand Down
36 changes: 22 additions & 14 deletions src/date.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use chrono::{Datelike, Duration, Local, NaiveDate};
use serde::de::Visitor;
use chrono::{NaiveDate, Datelike, Duration, Local};
use std::ops::Deref;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::ops::Deref;

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct UntisDate(NaiveDate);
Expand All @@ -22,11 +22,11 @@ impl UntisDate {
}

pub fn week_begin() -> Self {
Self::week_begin_from(Local::today().naive_local())
Self::week_begin_from(Local::now().date_naive())
}

pub fn week_end() -> Self {
Self::week_end_from(Local::today().naive_local())
Self::week_end_from(Local::now().date_naive())
}
}

Expand Down Expand Up @@ -67,49 +67,57 @@ impl<'de> Visitor<'de> for UntisDateVisitor {
}

fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}

fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}

fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}

fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}

fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}

fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}

fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}

fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisDate(chrono_from_untis_date(value as u32)))
}
Expand All @@ -127,7 +135,7 @@ fn chrono_from_untis_date(value: u32) -> NaiveDate {
let month = string[4..6].parse::<u32>().unwrap();
let day = string[6..8].parse::<u32>().unwrap();

NaiveDate::from_ymd(year, month, day)
NaiveDate::from_ymd_opt(year, month, day).unwrap()
}

#[cfg(test)]
Expand Down
25 changes: 8 additions & 17 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std;
use std::fmt::{self, Display, Formatter};
use std::convert::From;
use reqwest;
use serde_json;
use std;
use std::convert::From;
use std::fmt::{self, Display, Formatter};

#[derive(Debug)]
pub enum Error {
Expand All @@ -14,26 +14,17 @@ pub enum Error {

impl Display for Error {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
let msg = match *self {
Error::Http(ref status) => format!("Http error with status code: {}", status),
_ => std::error::Error::description(self).to_owned(),
let msg = match self {
Self::Reqwest(err) => err.to_string(),
Self::SerdeJSON(err) => err.to_string(),
Self::Http(status) => format!("Http error with status code: {}", status),
Self::NoSession => String::from("Not logged into WebUntis"),
};

formatter.write_str(&msg)
}
}

impl std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Reqwest(ref err) => err.description(),
Error::SerdeJSON(ref err) => err.description(),
Error::Http(_) => "The Http request didn't succeed.",
Error::NoSession => "This method can't be called without a session.",
}
}
}

impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Error::Reqwest(err)
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Library to access [Untis](https://www.untis.at)
//!
//!
//! The core of this crate is the `Untis` struct.
extern crate chrono;
Expand All @@ -10,16 +10,16 @@ extern crate serde_json;
#[macro_use]
extern crate serde_derive;

mod date;
mod error;
mod request;
mod response;
mod untis;
mod date;
mod time;
mod untis;

pub use date::UntisDate;
pub use error::Error;
pub use request::*;
pub use response::*;
pub use untis::Units;
pub use date::UntisDate;
pub use time::UntisTime;
pub use untis::Units;
2 changes: 1 addition & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Serialize;

use chrono::NaiveDate;

use date::UntisDate;
use crate::date::UntisDate;

#[derive(Serialize)]
pub struct RpcRequest<P: Serialize> {
Expand Down
12 changes: 8 additions & 4 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::date::UntisDate;
use crate::time::UntisTime;
use std::collections::HashMap;
use date::UntisDate;
use time::UntisTime;

#[derive(Debug, Deserialize)]
pub struct RpcResponse<R> {
Expand Down Expand Up @@ -141,5 +141,9 @@ pub struct DepartmentItem {
pub long_name: String,
}

fn default_id() -> isize { -1 }
fn default_activity_type() -> String { "undefined".to_owned() }
fn default_id() -> isize {
-1
}
fn default_activity_type() -> String {
"undefined".to_owned()
}
34 changes: 21 additions & 13 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use serde::de::Visitor;
use chrono::NaiveTime;
use std::ops::Deref;
use serde::de::Visitor;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::ops::Deref;

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct UntisTime(NaiveTime);
Expand Down Expand Up @@ -44,49 +44,57 @@ impl<'de> Visitor<'de> for UntisTimeVisitor {
}

fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}

fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}

fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}

fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}

fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}

fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}

fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}

fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where E: serde::de::Error,
where
E: serde::de::Error,
{
Ok(UntisTime(chrono_from_untis_time(value as u32)))
}
Expand All @@ -102,9 +110,9 @@ fn chrono_from_untis_time(value: u32) -> NaiveTime {
let string = format!("{}", value);
let hour_len = if string.len() == 3 { 1 } else { 2 };
let hours = string[0..hour_len].parse::<u32>().unwrap();
let mins = string[hour_len..(hour_len+2)].parse::<u32>().unwrap();
let mins = string[hour_len..(hour_len + 2)].parse::<u32>().unwrap();

NaiveTime::from_hms(hours, mins, 0)
NaiveTime::from_hms_opt(hours, mins, 0).unwrap()
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 8d1f8f8

Please sign in to comment.