Skip to content

libtime: strftime now returns Result<String, String> instead of String. #17467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

hatahet
Copy link
Contributor

@hatahet hatahet commented Sep 23, 2014

[breaking-change]

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

@hatahet hatahet force-pushed the mybranch branch 5 times, most recently from 83c2077 to 79ec54e Compare September 23, 2014 05:15
@@ -872,7 +871,7 @@ pub fn strptime(s: &str, format: &str) -> Result<Tm, String> {
}

/// Formats the time according to the format string.
pub fn strftime(format: &str, tm: &Tm) -> String {
pub fn strftime(format: &str, tm: &Tm) -> Result<String, String> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the pattern for errors such as this has been Result<T, CustomErrorEnum> to provide more contextual information sometimes and also to be able to programmatically inspect an error result to see what happened. Could you add a custom enum for this method to return? Also be sure to implement Show for the enum to basically return the same String that's already being returned.

Also, can you update the documentation for this function to explain why the return value is a Result, and exactly what cases will cause it to return an Err?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Could you refer me to other implementations that do what you describe regarding implementing custom error enums?

Also, if I am going to do this, then strptime should change as well to reflect the same I suppose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsers in semver have some examples of this. It's ok to not update strptime at this time, it can always be done as part of a later PR as well!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I went ahead an fixed strptime anyway :)

@hatahet
Copy link
Contributor Author

hatahet commented Sep 28, 2014

@alexcrichton I addressed the review comments. Should I submit a separate commit, or should I merge both commits into one, then update the PR?

@sfackler
Copy link
Member

A separate commit makes it a bit easier to review.

@hatahet hatahet force-pushed the mybranch branch 2 times, most recently from cc8fa93 to c909bbd Compare September 28, 2014 07:28
@alexcrichton
Copy link
Member

Looks great to me, thanks @hatahet! Could you squash the two commits together and expand the commit message a little more with respect to our breaking changes policy? Just need to add some rationale as to why the API is changing and how to migrate code from the old to the new (should be pretty simple in this case).

@hatahet
Copy link
Contributor Author

hatahet commented Oct 6, 2014

@alexcrichton Done! How does the comment look?

@alexcrichton
Copy link
Member

Looks great, thanks @hatahet!

@alexcrichton
Copy link
Member

It looks like travis found a few tidy errors though, could you fix the lines larger than 100 chars?

…ing, ParseError>`.

`strftime` currently returns a `String`. This does not indicate that
this function may return an error due to to a malformed format string.
This change introduces a `ParseError` enum which indicates the type of
error that occurred. The return type of `strptime` was also changed to
use this new enum instead of returning `Result<String, String>`. Now,
all instances where `strftime` was used need to have their return value
checked to see if it were valid or not.

[breaking-change]
@hatahet
Copy link
Contributor Author

hatahet commented Oct 12, 2014

@alexcrichton Done!

bors added a commit that referenced this pull request Oct 13, 2014
@bors bors closed this Oct 13, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 11, 2024
feat: add bool_to_enum assist for parameters

## Summary

This PR adds parameter support for `bool_to_enum` assists. Essentially, the assist can now transform this:

```rs
fn function($0foo: bool) {
    if foo {
        println!("foo");
    }
}
```

To this,

```rs
#[derive(PartialEq, Eq)]
enum Bool { True, False }

fn function(foo: Bool) {
    if foo == Bool::True {
        println!("foo");
    }
}
```

Thanks to `@/davidbarsky`  for the test skeleton (:

Closes rust-lang#17400
lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 11, 2024
- unrelated to the PR but I wanted to change this in rust-lang#17467
10takla pushed a commit to 10takla/rust that referenced this pull request Aug 3, 2024
- unrelated to the PR but I wanted to change this in rust-lang#17467
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants