Update dependencies + fix check / clippy feedback.

This commit is contained in:
Orne Brocaar 2024-03-19 12:15:48 +00:00
parent 9de0354f13
commit c426e48b8d
22 changed files with 564 additions and 531 deletions

930
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -21,4 +21,4 @@ chirpstack_api = { path = "../api/rust", default-features = false, features = ["
# Development and testing
[dev-dependencies]
httpmock = "0.7.0-rc.1"
httpmock = "0.7.0"

View File

@ -137,7 +137,7 @@ pin-project = "1.1"
# Development and testing
[dev-dependencies]
httpmock = "0.7.0-rc.1"
httpmock = "0.7.0"
bytes = "1.4"
dotenv = "0.15"

View File

@ -24,7 +24,7 @@ fn is_default<T: Default + PartialEq>(t: &T) -> bool {
impl AuthClaim {
pub fn new_for_user(id: &Uuid) -> Self {
let nbf: DateTime<Utc> = Utc::now();
let exp = nbf.add(Duration::days(1));
let exp = nbf.add(Duration::try_days(1).unwrap());
AuthClaim {
aud: "chirpstack".to_string(),
@ -75,7 +75,7 @@ pub mod test {
let key_id = Uuid::new_v4();
let nbf: DateTime<Utc> = Utc::now();
let exp = nbf.add(-Duration::days(1));
let exp = nbf.add(-Duration::try_days(1).unwrap());
let claim = AuthClaim::new_for_api_key(&key_id);
assert_eq!("key", claim.typ);

View File

@ -263,7 +263,8 @@ impl GatewayService for Gateway {
state: {
if let Some(ts) = gw.last_seen_at {
if (Utc::now() - ts)
> Duration::seconds((gw.stats_interval_secs * 2).into())
> Duration::try_seconds((gw.stats_interval_secs * 2).into())
.unwrap_or_default()
{
api::GatewayState::Offline
} else {

View File

@ -165,7 +165,7 @@ async fn store_verifier(
let key = redis_key(format!("auth:oauth2:{}", token.secret()));
redis::cmd("PSETEX")
.arg(key)
.arg(Duration::minutes(5).num_milliseconds())
.arg(Duration::try_minutes(5).unwrap().num_milliseconds())
.arg(verifier.secret())
.query_async(&mut get_async_redis_conn().await?)
.await?;

View File

@ -147,7 +147,7 @@ async fn store_nonce(state: &CsrfToken, nonce: &Nonce) -> Result<()> {
redis::cmd("PSETEX")
.arg(key)
.arg(Duration::minutes(5).num_milliseconds())
.arg(Duration::try_minutes(5).unwrap().num_milliseconds())
.arg(nonce.secret())
.query_async(&mut get_async_redis_conn().await?)
.await?;

View File

@ -249,7 +249,7 @@ pub fn ul_meta_data_to_rx_info(ul_meta_data: &ULMetaData) -> Result<Vec<gw::Upli
fine_time_since_gps_epoch: if gw_info.fine_recv_time.is_some() {
let ts = ul_meta_data
.recv_time
.duration_round(Duration::seconds(1))?;
.duration_round(Duration::try_seconds(1).unwrap_or_default())?;
let ts = ts + Duration::nanoseconds(gw_info.fine_recv_time.unwrap() as i64);
Some(ts.to_gps_time().to_std()?.into())

View File

@ -8,16 +8,17 @@ use tracing::debug;
use lrwn::DevAddr;
lazy_static! {
static ref BEACON_PERIOD: Duration = Duration::seconds(128);
static ref BEACON_RESERVED: Duration = Duration::milliseconds(2120);
static ref BEACON_GUARD: Duration = Duration::seconds(3);
static ref BEACON_WINDOW: Duration = Duration::milliseconds(122880);
static ref BEACON_PERIOD: Duration = Duration::try_seconds(128).unwrap();
static ref BEACON_RESERVED: Duration = Duration::try_milliseconds(2120).unwrap();
static ref BEACON_GUARD: Duration = Duration::try_seconds(3).unwrap();
static ref BEACON_WINDOW: Duration = Duration::try_milliseconds(122880).unwrap();
static ref PING_PERIOD_BASE: usize = 1 << 12;
static ref SLOT_LEN: Duration = Duration::milliseconds(30);
static ref SLOT_LEN: Duration = Duration::try_milliseconds(30).unwrap();
}
pub fn get_beacon_start(ts: Duration) -> Duration {
Duration::seconds(ts.num_seconds() - (ts.num_seconds() % BEACON_PERIOD.num_seconds()))
Duration::try_seconds(ts.num_seconds() - (ts.num_seconds() % BEACON_PERIOD.num_seconds()))
.unwrap_or_default()
}
pub fn get_ping_offset(beacon_ts: Duration, dev_addr: &DevAddr, ping_nb: usize) -> Result<usize> {
@ -76,7 +77,7 @@ pub fn get_next_ping_slot_after(
}
}
beacon_start_ts = beacon_start_ts + *BEACON_PERIOD;
beacon_start_ts += *BEACON_PERIOD;
}
}
@ -103,7 +104,11 @@ pub mod test {
// Multiple of 128 seconds.
assert_eq!(
0,
start_ts.num_nanoseconds().unwrap() % Duration::seconds(128).num_nanoseconds().unwrap()
start_ts.num_nanoseconds().unwrap()
% Duration::try_seconds(128)
.unwrap()
.num_nanoseconds()
.unwrap()
);
// Les than 128 seconds ago.
@ -142,39 +147,40 @@ pub mod test {
after: Duration::zero(),
dev_addr: DevAddr::from_be_bytes([0, 0, 0, 0]),
ping_nb: 1,
expected_ping_slot_ts: Duration::minutes(1)
+ Duration::seconds(14)
+ Duration::milliseconds(300),
expected_ping_slot_ts: Duration::try_minutes(1).unwrap()
+ Duration::try_seconds(14).unwrap()
+ Duration::try_milliseconds(300).unwrap(),
},
Test {
after: Duration::minutes(2),
after: Duration::try_minutes(2).unwrap(),
dev_addr: DevAddr::from_be_bytes([0, 0, 0, 0]),
ping_nb: 1,
expected_ping_slot_ts: Duration::minutes(3)
+ Duration::seconds(5)
+ Duration::milliseconds(620),
expected_ping_slot_ts: Duration::try_minutes(3).unwrap()
+ Duration::try_seconds(5).unwrap()
+ Duration::try_milliseconds(620).unwrap(),
},
Test {
after: Duration::zero(),
dev_addr: DevAddr::from_be_bytes([0, 0, 0, 0]),
ping_nb: 2,
expected_ping_slot_ts: Duration::seconds(12) + Duration::milliseconds(860),
expected_ping_slot_ts: Duration::try_seconds(12).unwrap()
+ Duration::try_milliseconds(860).unwrap(),
},
Test {
after: Duration::seconds(13),
after: Duration::try_seconds(13).unwrap(),
dev_addr: DevAddr::from_be_bytes([0, 0, 0, 0]),
ping_nb: 2,
expected_ping_slot_ts: Duration::minutes(1)
+ Duration::seconds(14)
+ Duration::milliseconds(300),
expected_ping_slot_ts: Duration::try_minutes(1).unwrap()
+ Duration::try_seconds(14).unwrap()
+ Duration::try_milliseconds(300).unwrap(),
},
Test {
after: Duration::seconds(124),
after: Duration::try_seconds(124).unwrap(),
dev_addr: DevAddr::from_be_bytes([0, 0, 0, 0]),
ping_nb: 128,
expected_ping_slot_ts: Duration::minutes(2)
+ Duration::seconds(4)
+ Duration::milliseconds(220),
expected_ping_slot_ts: Duration::try_minutes(2).unwrap()
+ Duration::try_seconds(4).unwrap()
+ Duration::try_milliseconds(220).unwrap(),
},
];

View File

@ -1709,7 +1709,7 @@ impl Data {
v.clone().try_into().map_err(anyhow::Error::msg)?;
if last_req
< Utc::now()
.checked_sub_signed(chrono::Duration::hours(24))
.checked_sub_signed(chrono::Duration::try_hours(24).unwrap())
.unwrap()
&& counter < max_count
{
@ -2465,7 +2465,7 @@ impl Data {
}
// set timing
let now_gps_ts = Utc::now().to_gps_time() + chrono::Duration::seconds(1);
let now_gps_ts = Utc::now().to_gps_time() + chrono::Duration::try_seconds(1).unwrap();
let ping_slot_ts = classb::get_next_ping_slot_after(
now_gps_ts,
&self.device.get_dev_addr()?,
@ -4325,7 +4325,7 @@ mod test {
index: 1,
w_f_cnt_last_request: Some(
Utc::now()
.checked_sub_signed(chrono::Duration::hours(48))
.checked_sub_signed(chrono::Duration::try_hours(48).unwrap())
.unwrap()
.into(),
),

View File

@ -282,7 +282,8 @@ impl TxAck {
qi.is_pending = true;
if dev.enabled_class == DeviceClass::C {
let timeout = Utc::now() + Duration::seconds(dp.class_c_timeout as i64);
let timeout =
Utc::now() + Duration::try_seconds(dp.class_c_timeout as i64).unwrap_or_default();
qi.timeout_after = Some(timeout);
}

View File

@ -5,75 +5,75 @@ lazy_static! {
static ref LEAP_SECONDS_TABLE: Vec<(DateTime<Utc>, Duration)> = vec![
(
Utc.with_ymd_and_hms(1981, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1982, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1983, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1985, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1987, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1989, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1990, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1992, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1993, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1994, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1995, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1997, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(1998, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(2005, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(2008, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(2012, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(2015, 6, 30, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
(
Utc.with_ymd_and_hms(2016, 12, 31, 23, 59, 59).unwrap(),
Duration::seconds(1)
Duration::try_seconds(1).unwrap()
),
];
}
@ -91,7 +91,7 @@ impl ToGpsTime for DateTime<Utc> {
let mut offset = Duration::zero();
for ls in LEAP_SECONDS_TABLE.iter() {
if &ls.0 < self {
offset = offset + ls.1;
offset += ls.1;
}
}
@ -129,19 +129,19 @@ pub mod test {
},
Test {
time: Utc.with_ymd_and_hms(2010, 1, 28, 16, 36, 24).unwrap(),
time_since_gps_epoch: Duration::seconds(948731799),
time_since_gps_epoch: Duration::try_seconds(948731799).unwrap(),
},
Test {
time: Utc.with_ymd_and_hms(2025, 7, 14, 0, 0, 0).unwrap(),
time_since_gps_epoch: Duration::seconds(1436486418),
time_since_gps_epoch: Duration::try_seconds(1436486418).unwrap(),
},
Test {
time: Utc.with_ymd_and_hms(2012, 6, 30, 23, 59, 59).unwrap(),
time_since_gps_epoch: Duration::seconds(1025136014),
time_since_gps_epoch: Duration::try_seconds(1025136014).unwrap(),
},
Test {
time: Utc.with_ymd_and_hms(2012, 7, 1, 0, 0, 0).unwrap(),
time_since_gps_epoch: Duration::seconds(1025136016),
time_since_gps_epoch: Duration::try_seconds(1025136016).unwrap(),
},
];

View File

@ -15,10 +15,8 @@ pub fn get_root_certs(ca_file: Option<String>) -> Result<rustls::RootCertStore>
let f = File::open(ca_file).context("Open CA certificate")?;
let mut reader = BufReader::new(f);
let certs = rustls_pemfile::certs(&mut reader);
for cert in certs {
if let Ok(cert) = cert {
roots.add(cert)?;
}
for cert in certs.flatten() {
roots.add(cert)?;
}
}

View File

@ -104,7 +104,7 @@ impl Integration {
};
// Compensate for gnss scanning time and uplink.
let ts = ts - Duration::seconds(6);
let ts = ts - Duration::try_seconds(6).unwrap();
Some(ts.num_seconds() as f64)
}
},
@ -454,11 +454,12 @@ impl Integration {
let di = pl.device_info.as_ref().unwrap();
let dev_eui = EUI64::from_str(&di.dev_eui)?;
let ttl = Duration::seconds(
let ttl = Duration::try_seconds(
self.config
.modem_geolocation_services
.geolocation_buffer_ttl as i64,
);
)
.unwrap_or_default();
let mut buf = vec![pl.rx_info.clone()];
buf.extend_from_slice(&buffer::get_geoloc_buffer(&dev_eui, ttl).await?);

View File

@ -972,14 +972,14 @@ pub mod test {
.await
.unwrap();
qi.is_pending = true;
qi.timeout_after = Some(Utc::now() + Duration::seconds(10));
qi.timeout_after = Some(Utc::now() + Duration::try_seconds(10).unwrap());
qi = device_queue::update_item(qi).await.unwrap();
let res = get_with_class_b_c_queue_items(10).await.unwrap();
assert_eq!(0, res.len());
// device in class C / downlink is pending but has expired.
qi.is_pending = true;
qi.timeout_after = Some(Utc::now() - Duration::seconds(10));
qi.timeout_after = Some(Utc::now() - Duration::try_seconds(10).unwrap());
let _ = device_queue::update_item(qi).await.unwrap();
let res = get_with_class_b_c_queue_items(10).await.unwrap();
assert_eq!(1, res.len());

View File

@ -189,7 +189,7 @@ pub async fn get(
while ts.le(&end) {
timestamps.push(ts);
keys.push(get_key(name, a, ts));
ts += ChronoDuration::hours(1);
ts += ChronoDuration::try_hours(1).unwrap();
}
}
Aggregation::DAY => {
@ -204,11 +204,11 @@ pub async fn get(
timestamps.push(ts);
keys.push(get_key(name, a, ts));
ts = {
if (ts + ChronoDuration::days(1)).day() == ts.day() {
if (ts + ChronoDuration::try_days(1).unwrap()).day() == ts.day() {
// In case of DST to non-DST transition, the ts is incremented with less
// than 24h and we end up with the same day. Therefore we increment by two
// days.
(ts + ChronoDuration::days(2))
(ts + ChronoDuration::try_days(2).unwrap())
.date_naive()
.and_hms_opt(0, 0, 0)
.unwrap()
@ -217,7 +217,7 @@ pub async fn get(
} else {
// Make sure that the timestamp stays at midnight in case of non-DST to DST
// change.
(ts + ChronoDuration::days(1))
(ts + ChronoDuration::try_days(1).unwrap())
.date_naive()
.and_hms_opt(0, 0, 0)
.unwrap()

View File

@ -426,7 +426,7 @@ pub async fn enqueue(
// Get timestamp after which we must generate the next ping-slot.
let ping_slot_after_gps_time = match res {
Some(v) => Duration::milliseconds(v),
Some(v) => Duration::try_milliseconds(v).unwrap_or_default(),
None => (Utc::now()
+ Duration::from_std(
conf.network.scheduler.multicast_class_b_margin,
@ -993,7 +993,7 @@ pub mod test {
// get
let qi_get = get_queue_item(&ids[0]).await.unwrap();
assert!((Utc::now() - qi_get.scheduler_run_after) < Duration::seconds(1)); // ~ Utc::now()
assert!((Utc::now() - qi_get.scheduler_run_after) < Duration::try_seconds(1).unwrap()); // ~ Utc::now()
assert!(qi_get.emit_at_time_since_gps_epoch.is_none());
assert_eq!(10, qi_get.f_cnt);
assert_eq!(vec![3, 2, 1], qi_get.data);

View File

@ -31,7 +31,7 @@ pub async fn save(ds: &internal::PassiveRoamingDeviceSession) -> Result<()> {
};
let lifetime = lifetime - Utc::now();
if lifetime <= Duration::seconds(0) {
if lifetime <= Duration::zero() {
debug!("Not saving passive-roaming device-session, lifetime of passive-roaming session expired");
return Ok(());
}

View File

@ -283,7 +283,7 @@ async fn test_downlink_scheduler() {
..Default::default()
};
let now_gps_ts = chrono::Utc::now().to_gps_time() + chrono::Duration::seconds(1);
let now_gps_ts = chrono::Utc::now().to_gps_time() + chrono::Duration::try_seconds(1).unwrap();
let ping_slot_ts = classb::get_next_ping_slot_after(
now_gps_ts,
&DevAddr::from_slice(&ds.dev_addr).unwrap(),

View File

@ -205,7 +205,7 @@ impl Data {
if lt == 0 {
None
} else {
Some((Utc::now() + Duration::seconds(lt)).into())
Some((Utc::now() + Duration::try_seconds(lt).unwrap_or_default()).into())
}
},
f_nwk_s_int_key: match &pr_start_ans.f_nwk_s_int_key {

View File

@ -124,7 +124,7 @@ pub fn get_time_since_gps_epoch_chrono(rx_info: &[gw::UplinkRxInfo]) -> Option<c
for rxi in rx_info {
if let Some(gps_time) = &rxi.time_since_gps_epoch {
return Some(
chrono::Duration::seconds(gps_time.seconds)
chrono::Duration::try_seconds(gps_time.seconds).unwrap_or_default()
+ chrono::Duration::nanoseconds(gps_time.nanos as i64),
);
}

View File

@ -179,7 +179,7 @@ impl JoinRequest {
if lt == 0 {
None
} else {
Some((Utc::now() + Duration::seconds(lt)).into())
Some((Utc::now() + Duration::try_seconds(lt).unwrap_or_default()).into())
}
},
lorawan_1_1: pr_start_ans.f_nwk_s_int_key.is_some(),