datetime_string/lib.rs
1//! Datetime string types.
2//!
3//! This crate provides borrowed and owned string types for datetime.
4//!
5//! The types in this crate do:
6//!
7//! * guarantee that the content string is well-formatted in some specific
8//! datetime format, and
9//! * provide convenient API for getting and setting components of datetime,
10//! for example getting the hour and setting the day of a month.
11//!
12//! # Feature flags
13//!
14//! * `alloc`
15//! + Enabled by default.
16//! + Provides types and functions which requires `alloc` crate.
17//! Examples of stuff in `alloc` is `String`, `ToOwned`, `Vec<u8>`, etc.
18//! * `std`
19//! + Enabled by default.
20//! + Provides types and functions which requires `std` crate.
21//! Examples of stuff in `std` is `std::error::Error`.
22//! * `chrono04`
23//! + Provides some conversions between types in `chrono` crate v0.4 and this crate.
24//! * `serde`
25//! + Provides `serde::{Serilaize, Deserialize}` implementations for string types.
26//! * `serde-alloc`
27//! + Enables `alloc` feature for this crate and `alloc` feature for `serde` crate.
28//! * `serde-std`
29//! + Enables `std` feature for this crate and `std` feature for `serde` crate.
30//! * `time03`
31//! + Provides some conversions between types in `time` crate v0.3 and this crate.
32//!
33//! # Values construction
34//!
35//! Borrowed string slice types can be constructed by:
36//!
37//! * `from_str()`, `from_mut_str()`,
38//! * `from_bytes()`, `from_bytes_mut()`,
39//! * `TryFrom<&[u8]>`, `TryFrom<&mut [u8]>`,
40//! * `TryFrom<&str>`, and `TryFrom<&mut str>`.
41//!
42//! Owned string types can be constructed by:
43//!
44//! * `From<&{corresponding borrowed string type}>`,
45//! * `TryFrom<&[u8]>`, `TryFrom<Vec<u8>>`,
46//! * `TryFrom<&str>`, `TryFrom<String>`, and
47//! * `FromStr` (i.e. [`str::parse`]).
48//!
49//! # Examples
50//!
51//! ```
52//! // `Hms6ColonStr` guarantees that the string is valid `hh:mm:ss` time.
53//! use datetime_string::common::Hms6ColonStr;
54//!
55//! let time = Hms6ColonStr::from_str("12:34:56")?;
56//!
57//! assert_eq!(time.hour(), 12);
58//! assert_eq!(time.minute(), 34);
59//! assert_eq!(time.second(), 56);
60//! # Ok::<_, datetime_string::Error>(())
61//! ```
62//!
63//! ```
64//! use std::convert::TryFrom;
65//! // `Ymd8HyphenString` guarantees that the string is valid `YYYY-MM-DD` date.
66//! use datetime_string::common::Ymd8HyphenString;
67//!
68//! let mut date = Ymd8HyphenString::try_from("1999-12-31")?;
69//!
70//! assert_eq!(date.year(), 1999);
71//! // 1-based month.
72//! assert_eq!(date.month1(), 12);
73//! // 0-based month.
74//! assert_eq!(date.month0(), 11);
75//! // Day of a month.
76//! assert_eq!(date.mday(), 31);
77//!
78//! date.set_month1(1)?;
79//! assert_eq!(date.as_str(), "1999-01-31");
80//!
81//! assert!(date.set_month1(11).is_err(), "This fails because 1999-11-31 is invalid date");
82//! # Ok::<_, datetime_string::Error>(())
83//! ```
84#![warn(missing_docs)]
85#![warn(rust_2018_idioms)]
86#![warn(clippy::missing_docs_in_private_items)]
87#![cfg_attr(not(feature = "std"), no_std)]
88#![cfg_attr(docsrs, feature(doc_cfg))]
89
90#[cfg(feature = "alloc")]
91extern crate alloc;
92
93#[macro_use]
94mod macros;
95
96pub mod common;
97pub(crate) mod datetime;
98pub(crate) mod error;
99pub(crate) mod parse;
100pub mod rfc3339;
101pub(crate) mod str;
102
103pub use self::error::{ConversionError, Error};