Skip to content

Commit

Permalink
Replace time crate with jiff
Browse files Browse the repository at this point in the history
  • Loading branch information
oherrala committed Mar 10, 2025
1 parent 35cf085 commit 28d9fc3
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 25 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ignore = "0.4.23"
im-rc = "15.1.0"
indexmap = "2.7.1"
itertools = "0.14.0"
jiff = { version = "0.2.3", default-features = false, features = [ "std" ] }
jiff = { version = "0.2.3", default-features = false, features = [ "std", "serde" ] }
jobserver = "0.1.32"
lazycell = "1.3.0"
libc = "0.2.169"
Expand Down Expand Up @@ -102,7 +102,6 @@ snapbox = { version = "0.6.21", features = ["diff", "dir", "term-svg", "regex",
tar = { version = "0.4.43", default-features = false }
tempfile = "3.16.0"
thiserror = "2.0.11"
time = { version = "0.3.37", features = ["parsing", "formatting", "serde"] }
toml = "0.8.20"
toml_edit = { version = "0.22.23", features = ["serde"] }
tracing = { version = "0.1.41", default-features = false, features = ["std"] } # be compatible with rustc_log: https://github.com/rust-lang/rust/blob/e51e98dde6a/compiler/rustc_log/Cargo.toml#L9
Expand Down Expand Up @@ -208,7 +207,6 @@ supports-unicode.workspace = true
tar.workspace = true
tempfile.workspace = true
thiserror.workspace = true
time.workspace = true
toml.workspace = true
toml_edit.workspace = true
tracing = { workspace = true, features = ["attributes"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ flate2.workspace = true
git2.workspace = true
glob.workspace = true
itertools.workspace = true
jiff.workspace = true
pasetors.workspace = true
regex.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
snapbox.workspace = true
tar.workspace = true
time.workspace = true
toml.workspace = true
url.workspace = true
walkdir.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use cargo_util::paths::append;
use cargo_util::Sha256;
use flate2::write::GzEncoder;
use flate2::Compression;
use jiff::{SignedDuration, Timestamp};
use pasetors::keys::{AsymmetricPublicKey, AsymmetricSecretKey};
use pasetors::paserk::FormatAsPaserk;
use pasetors::token::UntrustedToken;
Expand All @@ -58,10 +59,9 @@ use std::fs::{self, File};
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::thread::{self, JoinHandle};
use tar::{Builder, Header};
use time::format_description::well_known::Rfc3339;
use time::{Duration, OffsetDateTime};
use url::Url;

/// Path to the local index for psuedo-crates.io.
Expand Down Expand Up @@ -910,9 +910,9 @@ impl HttpServer {
v: Option<u8>,
}
let message: Message<'_> = t!(serde_json::from_str(trusted_token.payload()).ok());
let token_time = t!(OffsetDateTime::parse(message.iat, &Rfc3339).ok());
let now = OffsetDateTime::now_utc();
if (now - token_time) > Duration::MINUTE {
let token_time = t!(Timestamp::from_str(message.iat).ok());
let now = Timestamp::now();
if now.duration_since(token_time) > SignedDuration::from_mins(1) {
return false;
}
if private_key_subject.as_deref() != message.sub {
Expand Down
2 changes: 1 addition & 1 deletion credential/cargo-credential/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ description = "A library to assist writing Cargo credential helpers."

[dependencies]
anyhow.workspace = true
jiff.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
thiserror.workspace = true
time.workspace = true

[target.'cfg(unix)'.dependencies]
libc.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions credential/cargo-credential/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]

use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use std::{fmt::Display, io};
use time::OffsetDateTime;

mod error;
mod secret;
Expand Down Expand Up @@ -202,8 +202,8 @@ pub enum CacheControl {
Never,
/// Cache this result and use it for subsequent requests in the current Cargo invocation until the specified time.
Expires {
#[serde(with = "time::serde::timestamp")]
expiration: OffsetDateTime,
#[serde(with = "jiff::fmt::serde::timestamp::second::required")]
expiration: Timestamp
},
/// Cache this result and use it for all subsequent requests in the current Cargo invocation.
Session,
Expand Down Expand Up @@ -320,7 +320,7 @@ mod tests {
#[test]
fn cache_control() {
let cc = CacheControl::Expires {
expiration: OffsetDateTime::from_unix_timestamp(1693928537).unwrap(),
expiration: Timestamp::from_second(1693928537).unwrap(),
};
let json = serde_json::to_string(&cc).unwrap();
assert_eq!(json, r#"{"cache":"expires","expiration":1693928537}"#);
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/util/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use cargo_credential::{
};

use core::fmt;
use jiff::Timestamp;
use serde::Deserialize;
use std::error::Error;
use time::{Duration, OffsetDateTime};
use std::time::Duration;
use url::Url;

use crate::core::SourceId;
Expand Down Expand Up @@ -619,7 +620,7 @@ fn auth_token_optional(
if let Some(cached_token) = cache.get(url) {
if cached_token
.expiration
.map(|exp| OffsetDateTime::now_utc() + Duration::minutes(1) < exp)
.map(|exp| Timestamp::now() + Duration::from_secs(60) < exp)
.unwrap_or(true)
{
if cached_token.operation_independent || matches!(operation, Operation::Read) {
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ use cargo_credential::Secret;
use cargo_util::paths;
use cargo_util_schemas::manifest::RegistryName;
use curl::easy::Easy;
use jiff::Timestamp;
use lazycell::LazyCell;
use serde::de::IntoDeserializer as _;
use serde::Deserialize;
use serde_untagged::UntaggedEnumVisitor;
use time::OffsetDateTime;
use toml_edit::Item;
use url::Url;

Expand Down Expand Up @@ -155,7 +155,7 @@ enum WhyLoad {
#[derive(Debug)]
pub struct CredentialCacheValue {
pub token_value: Secret<String>,
pub expiration: Option<OffsetDateTime>,
pub expiration: Option<Timestamp>,
pub operation_independent: bool,
}

Expand Down
5 changes: 2 additions & 3 deletions src/cargo/util/credential/paseto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use pasetors::{
keys::{AsymmetricKeyPair, AsymmetricPublicKey, AsymmetricSecretKey, Generate},
paserk::FormatAsPaserk,
};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use url::Url;

use crate::{
Expand Down Expand Up @@ -103,10 +102,10 @@ impl<'a> Credential for PasetoCredential<'a> {
.expose();
let kip: pasetors::paserk::Id = (&public).into();

let iat = OffsetDateTime::now_utc();
let iat = jiff::Timestamp::now();

let message = Message {
iat: &iat.format(&Rfc3339).unwrap(),
iat: &iat.to_string(),
sub: secret_key_subject.as_deref(),
mutation: match operation {
Operation::Publish { .. } => Some("publish"),
Expand Down

0 comments on commit 28d9fc3

Please sign in to comment.