Lines
95.83 %
Functions
50 %
Branches
100 %
//! Free-text date entry widget.
//!
//! Wraps an [`Editor`] for free-text date entry (`YYYY-MM-DDTHH:MM` / RFC3339).
//! New widgets are pre-seeded with the current instant (RFC3339). Up/Down day-stepping is
//! available via [`DateWidget::step`].
use forms::{now_instant, step_date};
use super::editor::Editor;
/// A text field for date entry.
#[derive(Debug, Clone)]
pub struct DateWidget {
editor: Editor,
}
/// A date widget pre-seeded with the current instant (RFC3339).
impl Default for DateWidget {
fn default() -> Self {
Self {
editor: Editor::with_buffer(now_instant()),
impl DateWidget {
#[must_use]
pub fn with_value(value: impl Into<String>) -> Self {
editor: Editor::with_buffer(value),
/// Buffer text (the current date string).
pub fn value(&self) -> &str {
self.editor.buffer()
pub fn editor(&self) -> &Editor {
&self.editor
/// Direct access to the inner editor.
pub fn as_editor_mut(&mut self) -> &mut Editor {
&mut self.editor
/// Advance the date by `delta_days` days, preserving the time-of-day component.
///
/// On parse failure the buffer is replaced with a now-relative step. On
/// date-range overflow the buffer is left unchanged.
pub fn step(&mut self, delta_days: i64) {
if let Some(new_val) = step_date(self.value(), delta_days) {
self.editor.replace_buffer(new_val);
#[cfg(test)]
#[path = "date_tests.rs"]
mod tests;