Struct DateStr

Source
pub struct DateStr { /* private fields */ }
Expand description

The date struct

Months and years are 1-indexed, meaning they start at ONE (1). So January would be 1, as written normally, and December is 12.

Called DateStr because it comes from a String

Implementations§

Source§

impl DateStr

Source

pub fn new(year: Year, month: Month, day: Day) -> Result<Self, DateErrors>

Creates a new DateStr from the given parts

Source§

impl DateStr

Source

pub fn from_iso_str<T: ToString>(string: T) -> DateStr

Parse a string to a DateStr struct

Parses a string (or any type implementing the ToString trait) to a DateStr struct.

The given date must be in ISO-8601 format, that is: YYYY-MM-DD.

I’d recommend using crate::DateStr::try_from_iso_str when unsure what the input string will be, since it returns a Result with understandable errors.

§Examples
let date_string: String = String::from("2022-12-31");
let new_date_from_string: DateStr = DateStr::from_iso_str(date_string);
let new_date_from_str: DateStr = DateStr::from_iso_str("2022-12-31");
assert_eq!(new_date_from_str, new_date_from_string);
Source

pub fn try_from_iso_str<T: ToString>(string: T) -> Result<DateStr, DateErrors>

Parse a string to a DateStr struct

Parses a string (or any type implementing the ToString trait) to a DateStr struct. This function returns a Result enum.

The given date must be in ISO-8601 format, that is: YYYY-MM-DD.

§Examples
let date_string: String = String::from("2022-12-31");
let date_from_string: Result<DateStr, errors::DateErrors> = DateStr::try_from_iso_str(date_string);
assert!(date_from_string.is_ok());;
§Errors

Since it checks for month first, it will return a DateErrors::InvalidMonth even if the day is wrong too, in wich it would return a DateErrors::InvalidDay.

Source§

impl DateStr

Source

pub fn format(&self, fmt: DateFormat) -> String

Format the date with a DateFormat

Pass a DateFormat. Will output a String with the date formatted how you wanted.

Use crate::DateStr::try_format for easy error handling

§Example
let a_date: DateStr = DateStr::from_iso_str("2022-12-29");
let a_fmtr: DateFormat = DateFormat::from_string("dd_mm_yyyy", Some('_')).unwrap();
let formatted_date: String = a_date.format(a_fmtr);
println!("{}", formatted_date);

Above code will output 29-12-2022.

§Panics

This function will panic when an invalid DateFormat is passed.

To use errors see crate::DateStr::try_format()

Source

pub fn try_format(&self, fmt: DateFormat) -> Result<String, DateErrors>

Try to format the date with a custom formatter

Safe function using the Result enum. Receives a DateFormat struct.

§Example:
let a_date: DateStr = DateStr::from_iso_str("2022-12-29");
let some_formatter: DateFormat = DateFormat::from_string("dd-mm-yyyy", None).unwrap();
let formatted_date: String = a_date.try_format(some_formatter).unwrap();
println!("{}", formatted_date);

Will output 29-12-2022

Trait Implementations§

Source§

impl Add for DateStr

Source§

type Output = DateStr

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Debug for DateStr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for DateStr

Display trait implementation for DateStr

Prints the date in ISO-8601 format (YYYY-MM-DD)

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<DateStr> for String

Source§

fn from(value: DateStr) -> Self

Converts to this type from the input type.
Source§

impl Into<DateStr> for String

Implementation of ToDateStr for String

Source§

fn to_datestr(&self) -> DateStr

This function creates a crate::DateStr in a to_string() fashion
Source§

fn try_to_datestr(&self) -> Result<DateStr, DateErrors>

Try to convert to DateStr using crate::DateStr::try_from_iso_str function, which returns a Result enum.
Source§

impl Into<DateStr> for str

Implementation of ToDateStr for &str

Source§

fn to_datestr(&self) -> DateStr

This function creates a crate::DateStr in a to_string() fashion
Source§

fn try_to_datestr(&self) -> Result<DateStr, DateErrors>

Try to convert to DateStr using crate::DateStr::try_from_iso_str function, which returns a Result enum.
Source§

impl PartialEq for DateStr

Source§

fn eq(&self, other: &DateStr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for DateStr

Source§

type Output = DateStr

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl TryFrom<String> for DateStr

Source§

type Error = DateErrors

The type returned in the event of a conversion error.
Source§

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for DateStr

Source§

impl StructuralPartialEq for DateStr

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.