pydantic / speedate

Fast and simple datetime, date, time and duration parsing for rust.
https://docs.rs/speedate/latest/speedate/
MIT License
199 stars 17 forks source link

Add a TimeConfigBuilder to avoid further breaking changes #46

Closed adriangb closed 1 year ago

adriangb commented 1 year ago

I noticed while updating pydantic-core that despite efforts to avoid breaking changes adding a field to TimeConfig is indeed a breaking change. I'm adding this TimeConfigBuilder so that we can add more options down the road without a breaking change.

@samuelcolvin I realize we probably think we won't need to add any more options, if you're 100% sure of this please just close this, but I figured since we had 0 a week ago and now have 2...

adriangb commented 1 year ago

please review

codecov[bot] commented 1 year ago

Codecov Report

Merging #46 (17a3987) into main (7058870) will increase coverage by 0.02%. The diff coverage is 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #46 +/- ## ========================================== + Coverage 99.07% 99.10% +0.02% ========================================== Files 6 6 Lines 862 890 +28 ========================================== + Hits 854 882 +28 Misses 8 8 ``` | [Impacted Files](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic) | Coverage Δ | | |---|---|---| | [src/lib.rs](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic#diff-c3JjL2xpYi5ycw==) | `91.66% <ø> (ø)` | | | [src/datetime.rs](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic#diff-c3JjL2RhdGV0aW1lLnJz) | `100.00% <100.00%> (ø)` | | | [src/duration.rs](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic#diff-c3JjL2R1cmF0aW9uLnJz) | `100.00% <100.00%> (ø)` | | | [src/time.rs](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic#diff-c3JjL3RpbWUucnM=) | `97.76% <100.00%> (+0.25%)` | :arrow_up: | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic). Last update [7058870...17a3987](https://app.codecov.io/gh/pydantic/speedate/pull/46?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydantic).
samuelcolvin commented 1 year ago

I'm a bit confused by why this is necessary and what it does.

I thought you could just do

TimeConfig { unix_timestamp_offset: Some(123), ..Default::default() };

And extra settings added to TimeConfig wouldn't break your code?

adriangb commented 1 year ago

Yes but then you have to force users to do that, and it’s not immediately obvious, eg in pydantic-core that’s not what we did. The builder pattern is meant precisely to address this situation.

adriangb commented 1 year ago

No real performance impact:

 name                             main ns/iter  variable ns/iter  diff ns/iter   diff %  speedup 
 compare_datetime_error_chrono    40            40                           0    0.00%   x 1.00 
 compare_datetime_error_iso8601   141           144                          3    2.13%   x 0.98 
 compare_datetime_error_speedate  10            10                           0    0.00%   x 1.00 
 compare_datetime_ok_chrono       166           166                          0    0.00%   x 1.00 
 compare_datetime_ok_iso8601      82            82                           0    0.00%   x 1.00 
 compare_datetime_ok_speedate     11            11                           0    0.00%   x 1.00 
 compare_duration_ok_iso8601      46            46                           0    0.00%   x 1.00 
 compare_duration_ok_speedate     31            32                           1    3.23%   x 0.97 
 compare_timestamp_ok_chrono      9             9                            0    0.00%   x 1.00 
 compare_timestamp_ok_speedate    8             7                           -1  -12.50%   x 1.14 
 date                             3             3                            0    0.00%   x 1.00 
 dt_custom_tz                     13            13                           0    0.00%   x 1.00 
 dt_naive                         12            13                           1    8.33%   x 0.92 
 format_date                      38            38                           0    0.00%   x 1.00 
 format_date_time                 180           221                         41   22.78%   x 0.81 
 format_time                      50            49                          -1   -2.00%   x 1.02 
 time                             6             8                            2   33.33%   x 0.75 
 x_combined                       92            96                           4    4.35%   x 0.96