Skip to content

Commit 3616f25

Browse files
committed
Update 'time' to 0.3, 'cookie' to 0.16.
Also reexport 'time' from the crate root.
1 parent 4b272f1 commit 3616f25

File tree

10 files changed

+53
-31
lines changed

10 files changed

+53
-31
lines changed

core/codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ glob = "0.3"
2626

2727
[dev-dependencies]
2828
rocket = { version = "0.5.0-rc.1", path = "../lib", features = ["json", "msgpack"] }
29+
time = { version = "0.3", features = ["macros"] }
2930
pretty_assertions = "0.7"
3031
version_check = "0.9"
3132
trybuild = "1.0"
32-
time = "0.2.11"

core/codegen/tests/from_form.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::net::{IpAddr, SocketAddr};
22
use std::collections::{BTreeMap, HashMap};
3+
34
use pretty_assertions::assert_eq;
45

56
use rocket::UriDisplayQuery;
@@ -732,13 +733,13 @@ fn test_defaults() {
732733
ip: IpAddr,
733734
#[field(default = ([192u8, 168, 1, 0], 20))]
734735
addr: SocketAddr,
735-
#[field(default = time::date!(2021-05-27))]
736+
#[field(default = time::macros::date!(2021-05-27))]
736737
date: time::Date,
737-
#[field(default = time::time!(01:15:00))]
738+
#[field(default = time::macros::time!(01:15:00))]
738739
time: time::Time,
739740
#[field(default = time::PrimitiveDateTime::new(
740-
time::date!(2021-05-27),
741-
time::time!(01:15:00),
741+
time::macros::date!(2021-05-27),
742+
time::macros::time!(01:15:00),
742743
))]
743744
datetime: time::PrimitiveDateTime,
744745
}
@@ -773,9 +774,12 @@ fn test_defaults() {
773774
string: "wowie".to_string(),
774775
ip: [192u8, 168, 1, 0].into(),
775776
addr: ([192u8, 168, 1, 0], 20).into(),
776-
date: time::date!(2021-05-27),
777-
time: time::time!(01:15:00),
778-
datetime: time::PrimitiveDateTime::new(time::date!(2021-05-27), time::time!(01:15:00)),
777+
date: time::macros::date!(2021-05-27),
778+
time: time::macros::time!(01:15:00),
779+
datetime: time::PrimitiveDateTime::new(
780+
time::macros::date!(2021-05-27),
781+
time::macros::time!(01:15:00)
782+
),
779783
}));
780784

781785
let form2: Option<FormWithDefaults> = strict(&form_string).ok();

core/http/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ uuid = ["uuid_"]
2626
smallvec = "1.0"
2727
percent-encoding = "2"
2828
http = "0.2"
29-
time = "0.2.11"
29+
time = { version = "0.3", features = ["formatting", "macros"] }
3030
indexmap = { version = "1.5.2", features = ["std"] }
3131
rustls = { version = "0.19", optional = true }
3232
tokio-rustls = { version = "0.22.0", optional = true }
@@ -39,7 +39,7 @@ pear = "0.2.3"
3939
pin-project-lite = "0.2"
4040
memchr = "2"
4141
stable-pattern = "0.1"
42-
cookie = { version = "0.15", features = ["percent-encode"] }
42+
cookie = { version = "0.16.0-rc.1", features = ["percent-encode"] }
4343
state = "0.5.1"
4444

4545
[dependencies.x509-parser]

core/http/src/uri/fmt/uri_display.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::collections::{BTreeMap, HashMap};
22
use std::{fmt, path};
33
use std::borrow::Cow;
44

5+
use time::{macros::format_description, format_description::FormatItem};
6+
57
use crate::RawStr;
68
use crate::uri::fmt::{Part, Path, Query, Formatter};
79

@@ -354,28 +356,34 @@ impl_with_display! {
354356

355357
macro_rules! impl_with_string {
356358
($($T:ty => $f:expr),+ $(,)?) => {$(
357-
/// This implementation is identical to a percent-encoded versiono the
359+
/// This implementation is identical to a percent-encoded version of the
358360
/// `Display` implementation.
359361
impl<P: Part> UriDisplay<P> for $T {
360362
#[inline(always)]
361363
fn fmt(&self, f: &mut Formatter<'_, P>) -> fmt::Result {
362-
let func: fn(&$T) -> String = $f;
363-
func(self).as_str().fmt(f)
364+
let func: fn(&$T) -> Result<String, fmt::Error> = $f;
365+
func(self).and_then(|s| s.as_str().fmt(f))
364366
}
365367
}
366368
)+}
367369
}
368370

369371
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
370372

371-
// Keep in-sync with the 'FromUriParam' impls.
373+
// Keep formats in sync with 'FromFormField' impls.
374+
static DATE_FMT: &[FormatItem<'_>] = format_description!("[year padding:none]-[month]-[day]");
375+
static TIME_FMT: &[FormatItem<'_>] = format_description!("[hour padding:none]:[minute]:[second]");
376+
static DATE_TIME_FMT: &[FormatItem<'_>] =
377+
format_description!("[year padding:none]-[month]-[day]T[hour padding:none]:[minute]:[second]");
378+
379+
// Keep list in sync with the 'FromUriParam' impls.
372380
impl_with_string! {
373-
time::Date => |d| d.format("%F"),
374-
time::PrimitiveDateTime => |d| d.format("%FT%T"),
375-
time::Time => |d| d.format("%T"),
376-
SocketAddr => |s| s.to_string(),
377-
SocketAddrV4 => |s| s.to_string(),
378-
SocketAddrV6 => |s| s.to_string(),
381+
time::Date => |d| d.format(&DATE_FMT).map_err(|_| fmt::Error),
382+
time::Time => |d| d.format(&TIME_FMT).map_err(|_| fmt::Error),
383+
time::PrimitiveDateTime => |d| d.format(&DATE_TIME_FMT).map_err(|_| fmt::Error),
384+
SocketAddr => |a| Ok(a.to_string()),
385+
SocketAddrV4 => |a| Ok(a.to_string()),
386+
SocketAddrV6 => |a| Ok(a.to_string()),
379387
}
380388

381389
// These are second level implementations: they all defer to an existing

core/lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ futures = { version = "0.3.0", default-features = false, features = ["std"] }
3838
yansi = "0.5"
3939
log = { version = "0.4", features = ["std"] }
4040
num_cpus = "1.0"
41-
time = "0.2.11"
41+
time = { version = "0.3", features = ["macros", "parsing"] }
4242
memchr = "2" # TODO: Use pear instead.
4343
binascii = "0.1"
4444
atty = "0.2"

core/lib/src/form/from_form_field.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::num::{
66
};
77

88
use time::{Date, Time, PrimitiveDateTime};
9+
use time::{macros::format_description, format_description::FormatItem};
910

1011
use crate::data::Capped;
1112
use crate::http::uncased::AsUncased;
@@ -355,10 +356,19 @@ impl_with_parse!(
355356
NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
356357
Ipv4Addr, IpAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr
357358
);
359+
//
360+
// Keep formats in sync with 'FromFormField' impls.
361+
static DATE_FMT: &[FormatItem<'_>] = format_description!("[year padding:none]-[month]-[day]");
362+
static TIME_FMT1: &[FormatItem<'_>] = format_description!("[hour padding:none]:[minute]:[second]");
363+
static TIME_FMT2: &[FormatItem<'_>] = format_description!("[hour padding:none]:[minute]");
364+
static DATE_TIME_FMT1: &[FormatItem<'_>] =
365+
format_description!("[year padding:none]-[month]-[day]T[hour padding:none]:[minute]:[second]");
366+
static DATE_TIME_FMT2: &[FormatItem<'_>] =
367+
format_description!("[year padding:none]-[month]-[day]T[hour padding:none]:[minute]");
358368

359369
impl<'v> FromFormField<'v> for Date {
360370
fn from_value(field: ValueField<'v>) -> Result<'v, Self> {
361-
let date = Self::parse(field.value, "%F")
371+
let date = Self::parse(field.value, &DATE_FMT)
362372
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
363373

364374
Ok(date)
@@ -367,8 +377,8 @@ impl<'v> FromFormField<'v> for Date {
367377

368378
impl<'v> FromFormField<'v> for Time {
369379
fn from_value(field: ValueField<'v>) -> Result<'v, Self> {
370-
let time = Self::parse(field.value, "%T")
371-
.or_else(|_| Self::parse(field.value, "%R"))
380+
let time = Self::parse(field.value, &TIME_FMT1)
381+
.or_else(|_| Self::parse(field.value, &TIME_FMT2))
372382
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
373383

374384
Ok(time)
@@ -377,8 +387,8 @@ impl<'v> FromFormField<'v> for Time {
377387

378388
impl<'v> FromFormField<'v> for PrimitiveDateTime {
379389
fn from_value(field: ValueField<'v>) -> Result<'v, Self> {
380-
let dt = Self::parse(field.value, "%FT%T")
381-
.or_else(|_| Self::parse(field.value, "%FT%R"))
390+
let dt = Self::parse(field.value, &DATE_TIME_FMT1)
391+
.or_else(|_| Self::parse(field.value, &DATE_TIME_FMT2))
382392
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
383393

384394
Ok(dt)

core/lib/src/form/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ macro_rules! assert_parse_fails {
6464

6565
#[test]
6666
fn time() {
67-
use time::{date, time, Date, Time, PrimitiveDateTime as DateTime};
67+
use time::{macros::{date, time}, Date, Time, PrimitiveDateTime as DateTime};
6868

6969
assert_values_parse_eq! {
7070
&["=2010-10-20"] => Date = date!(2010-10-20),

core/lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
pub use futures;
107107
pub use tokio;
108108
pub use figment;
109+
pub use time;
109110

110111
#[doc(hidden)]
111112
#[macro_use] pub mod log;

examples/forms/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ publish = false
77

88
[dependencies]
99
rocket = { path = "../../core/lib" }
10-
time = "0.2"
1110

1211
[dependencies.rocket_dyn_templates]
1312
path = "../../contrib/dyn_templates"

examples/forms/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#[macro_use] extern crate rocket;
22

3+
use rocket::time::Date;
34
use rocket::http::{Status, ContentType};
45
use rocket::form::{Form, Contextual, FromForm, FromFormField, Context};
5-
use rocket::fs::TempFile;
6-
use rocket::fs::{FileServer, relative};
6+
use rocket::fs::{FileServer, TempFile, relative};
77

88
use rocket_dyn_templates::Template;
99

@@ -36,7 +36,7 @@ enum Category {
3636
struct Submission<'v> {
3737
#[field(validate = len(1..))]
3838
title: &'v str,
39-
date: time::Date,
39+
date: Date,
4040
#[field(validate = len(1..=250))]
4141
r#abstract: &'v str,
4242
#[field(validate = ext(ContentType::PDF))]

0 commit comments

Comments
 (0)