Skip to content

fix: add missing build() method and align docs with actual API (TryFrom<&str> / FromStr) #59

Description

@ZialeHub

Summary

The README and multiple inline doc comments reference a build() constructor method on Date, Time, and DateTime:

// From the README:
let mut time = Time::build("T23:17:12Z.000")?;
let mut datetime = DateTime::build("2024-10-31 06:32:28")?;

// From date.rs doc comments:
let date = Date::build("2023-10-09")?;

// From time.rs doc comments:
let time = Time::build("12:21:46")?;

// From datetime.rs doc comments:
let datetime = DateTime::build("2023-05-17 09:05:12")?;

None of these compile. No build method exists on any of the three types. The nearest equivalent is TryFrom<&str>:

let date = Date::try_from("2023-10-09")?;

This is a documentation correctness bug that directly misleads new users, causes the README code examples to fail if copied, and degrades trust in the library.


Root Cause

The build naming was used in early design sketches or a previous API version and was referenced in documentation without being implemented. The Span trait's new(y, m, d) constructor is for component-based construction; string-based construction was left only as TryFrom<&str> with no named method alias.


Proposed Fix

Option A — Add build() as a named constructor (recommended)

Add a build inherent method to each type that delegates to TryFrom<&str>:

impl Date {
    /// Parse a `Date` from a string using the active `BASE_DATE_FORMAT`.
    ///
    /// # Example
    /// ```rust
    /// let date = Date::build("2024-10-31")?;
    /// assert_eq!(date.to_string(), "2024-10-31");
    /// ```
    pub fn build(s: &str) -> Result<Self, SpanError> {
        Self::try_from(s)
    }
}

Identical additions for Time::build(s: &str) and DateTime::build(s: &str).

This approach:

Option B — Update all docs to use TryFrom

Replace every Type::build("...") reference in the README and doc comments with Type::try_from("...") or "...".parse::<Type>(). Lower effort, but makes the README less readable and requires updating every example.

Option A is strongly preferred — the method name build is already established in the library's mental model and documentation.


Affected Locations

File Reference
README.md Lines referencing Time::build, DateTime::build
date.rs Doc comment on clear_unit
time.rs Doc comment on clear_unit
datetime.rs Doc comment on clear_unit

Acceptance Criteria

  • Date::build(s: &str) -> Result<Self, SpanError> added
  • Time::build(s: &str) -> Result<Self, SpanError> added
  • DateTime::build(s: &str) -> Result<Self, SpanError> added
  • All three methods delegate to TryFrom<&str> and use BASE_*_FORMAT
  • All README code examples compile as written (cargo test --doc passes)
  • All inline doc comment examples compile (cargo test --doc passes)
  • build methods are exported from prelude via the type re-exports (inherent methods, so this is automatic)
  • Span trait updated or doc-commented to reference build as the string-parsing entry point

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions