Skip to content

Commit ac628d0

Browse files
ElijahAhianyom4txpre-commit-ci[bot]
authored
feat: AsFormField for Date, Time, DateTime (#342)
* AsFormField for Date, Time, DateTime * add some tests and basic docs * more fixes * remove wrapped types * add more docs and attributes * add form support for DateTime<FixedOffset> * fix error message * minor fixes * turn on alloc feature for chrono * some more minor improvs * more improvs * move steps into attrs * refactor * rebase * use chrono_tz for Timezone field * make clippy happy * remove unnecessary test name demarcation * add basic form example * fix CI * more unnecessary code removal * Update examples/forms/templates/index.html Co-authored-by: Mateusz Maćkowski <mateusz@mackowski.org> * address comments * rebase * fix tests * remove lingering code as a result of the rebase * chore(pre-commit.ci): auto fixes from pre-commit hooks * add more variants * minor formatting --------- Co-authored-by: Mateusz Maćkowski <mateusz@mackowski.org> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2366503 commit ac628d0

File tree

13 files changed

+1809
-142
lines changed

13 files changed

+1809
-142
lines changed

Cargo.lock

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ members = [
1313
"examples/json",
1414
"examples/sessions",
1515
"examples/todo-list",
16+
"examples/forms"
1617
]
1718
resolver = "2"
1819

@@ -63,6 +64,7 @@ backtrace = "0.3"
6364
bytes = "1.10"
6465
cargo_toml = "0.22"
6566
chrono = { version = "0.4.41", default-features = false }
67+
chrono-tz = { version = "0.10.3", default-features = false }
6668
clap = { version = "4", features = ["deprecated"] }
6769
clap-verbosity-flag = { version = "3", default-features = false }
6870
clap_complete = "4"

cot/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ async-trait.workspace = true
2222
axum = { workspace = true, features = ["http1", "tokio"] }
2323
backtrace.workspace = true
2424
bytes.workspace = true
25-
chrono = { workspace = true, features = ["serde"] }
25+
chrono = { workspace = true, features = ["alloc", "serde"] }
26+
chrono-tz.workspace = true
2627
clap.workspace = true
2728
cot_macros.workspace = true
2829
deadpool-redis = { workspace = true, features = ["tokio-comp", "rt_tokio_1"], optional = true }

cot/src/form.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use std::fmt::Display;
2929

3030
use async_trait::async_trait;
3131
use bytes::Bytes;
32+
use chrono::NaiveDateTime;
33+
use chrono_tz::Tz;
3234
use cot::error::ErrorRepr;
3335
/// Derive the [`Form`] trait for a struct and create a [`FormContext`] for it.
3436
///
@@ -152,7 +154,20 @@ pub enum FormFieldValidationError {
152154
/// The maximum permitted value.
153155
max_value: String,
154156
},
155-
157+
/// The field value is an ambiguous datetime.
158+
#[error("This is an ambiguous datetime: {datetime}.")]
159+
AmbiguousDateTime {
160+
/// The ambiguous datetime value.
161+
datetime: NaiveDateTime,
162+
},
163+
/// The field value is a non-existent local datetime.
164+
#[error("Local datetime {datetime} does not exist for the given timezone {timezone}.")]
165+
NonExistentLocalDateTime {
166+
/// The non-existent local datetime value.
167+
datetime: NaiveDateTime,
168+
/// The timezone in which the datetime was specified.
169+
timezone: Tz,
170+
},
156171
/// The field value is required to be true.
157172
#[error("This field must be checked.")]
158173
BooleanRequiredToBeTrue,
@@ -207,6 +222,19 @@ impl FormFieldValidationError {
207222
}
208223
}
209224

225+
/// Creates a new `FormFieldValidationError` for an ambiguous datetime.
226+
#[must_use]
227+
pub fn ambiguous_datetime(datetime: NaiveDateTime) -> Self {
228+
FormFieldValidationError::AmbiguousDateTime { datetime }
229+
}
230+
231+
/// Creates a new `FormFieldValidationError` for a non-existent local
232+
/// datetime.
233+
#[must_use]
234+
pub fn non_existent_local_datetime(datetime: NaiveDateTime, timezone: Tz) -> Self {
235+
FormFieldValidationError::NonExistentLocalDateTime { datetime, timezone }
236+
}
237+
210238
/// Creates a new `FormFieldValidationError` from a `String`.
211239
#[must_use]
212240
pub const fn from_string(message: String) -> Self {

0 commit comments

Comments
 (0)