Skip to content
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

Replace thiserror with displaydoc #863

Merged
merged 15 commits into from
Jul 16, 2021
Merged

Conversation

Manishearth
Copy link
Member

Fixes #860

I added a displaydoc feature that makes it more suited to our use case.

I've tried to include code transformation commands (fastmod, awk) in the commit message where I used them.

We have a couple downsides of this change:

  • From impls need to be manually written when required (not a huge deal)
  • An explicit impl std::error::Error for FooError {} is needed everywhere. We could follow this up with a custom derive but that seems like overkill to me

However, this change is required to sensibly support no_std.

fastmod "thiserror = .*$" "displaydoc = { version = \"0.2.3\", default-features = false }"
fastmod thiserror::Error displaydoc::Display
fastmod "derive\(Error" "derive(Display"
Run `find . -name *.rs | xargs -I{} gawk -f replace.awk -i inplace {}`

with

```awk
BEGIN { a = 0; enum = "" }

/#\[derive\(Display/ {
    a = 1
}

match($0, /enum ([a-zA-Z]+)/, arr) {
    enum = arr[1]
}

{ print }

/^}/ && a == 1 {
    print "\nimpl std::error::Error for " enum " {}\n"
    a = 0
}
```
@Manishearth Manishearth requested review from echeran, nciric, sffc, zbraniecki and a team as code owners July 16, 2021 21:19
@codecov-commenter
Copy link

codecov-commenter commented Jul 16, 2021

Codecov Report

Merging #863 (5fdfb66) into main (cce8f22) will decrease coverage by 0.02%.
The diff coverage is 13.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #863      +/-   ##
==========================================
- Coverage   74.38%   74.35%   -0.03%     
==========================================
  Files         206      206              
  Lines       13033    13042       +9     
==========================================
+ Hits         9694     9697       +3     
- Misses       3339     3345       +6     
Impacted Files Coverage Δ
components/datetime/src/date.rs 50.00% <0.00%> (ø)
components/datetime/src/fields/length.rs 87.50% <0.00%> (ø)
components/datetime/src/fields/mod.rs 45.16% <0.00%> (+1.41%) ⬆️
components/datetime/src/fields/symbols.rs 68.58% <0.00%> (+0.52%) ⬆️
components/decimal/src/error.rs 0.00% <0.00%> (ø)
components/plurals/src/operands.rs 90.90% <0.00%> (-1.05%) ⬇️
components/plurals/src/rules/lexer.rs 90.10% <0.00%> (+1.93%) ⬆️
experimental/provider_ppucd/src/error.rs 0.00% <0.00%> (ø)
provider/cldr/src/download/error.rs 0.00% <0.00%> (ø)
provider/cldr/src/error.rs 0.00% <0.00%> (ø)
... and 17 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cce8f22...5fdfb66. Read the comment docs.

@coveralls
Copy link

coveralls commented Jul 16, 2021

Pull Request Test Coverage Report for Build 39e3f732880cd2d5c3ddf510387e506152edf23f-PR-863

  • 15 of 123 (12.2%) changed or added relevant lines in 27 files are covered.
  • 7 unchanged lines in 7 files lost coverage.
  • Overall coverage decreased (-0.05%) to 74.415%

Changes Missing Coverage Covered Lines Changed/Added Lines %
components/datetime/src/fields/length.rs 0 1 0.0%
components/datetime/src/fields/mod.rs 0 1 0.0%
components/datetime/src/fields/symbols.rs 0 1 0.0%
components/plurals/src/operands.rs 0 1 0.0%
components/plurals/src/rules/lexer.rs 0 1 0.0%
utils/fixed_decimal/src/lib.rs 0 1 0.0%
utils/pattern/src/pattern/error.rs 0 1 0.0%
components/datetime/src/skeleton.rs 1 5 20.0%
components/uniset/src/lib.rs 1 5 20.0%
provider/cldr/src/error.rs 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
components/plurals/src/operands.rs 1 90.91%
components/plurals/src/rules/parser.rs 1 96.25%
experimental/provider_ppucd/src/parse_ppucd.rs 1 93.0%
provider/cldr/src/download/error.rs 1 0%
provider/cldr/src/transform/numbers/decimal_pattern.rs 1 83.18%
provider/fs/src/error.rs 1 10.34%
utils/fixed_decimal/src/lib.rs 1 33.33%
Totals Coverage Status
Change from base Build cce8f224c263fb579d3f5ff02e6589e3aa9d9717: -0.05%
Covered Lines: 9828
Relevant Lines: 13207

💛 - Coveralls

InvalidTimeZoneOffset,
}

impl std::error::Error for DateTimeError {}
Copy link
Member

Choose a reason for hiding this comment

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

Issue: when #[error(transparent)] or #[from] is used, I think thiserror properly implements the source function of std::error::Error. Should we do that here, too?

Copy link
Member Author

Choose a reason for hiding this comment

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

We're not using .source() and it's not super important (this is, like I said, some of the "old error handling" stuff that will eventually be replaced)

UnknownSyntax(SyntaxOption),
}

impl std::error::Error for Error {}

impl From<serde_json::error::Error> for Error {
Copy link
Member

Choose a reason for hiding this comment

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

Question: do we have a path forward for getting rid of these From impls again in the future?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's easy to write another custom derive for From for stuff like this.

But also, manually writing From impls like this is super common.

sffc
sffc previously approved these changes Jul 16, 2021
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

LGTM but make CI happy first :)

@Manishearth
Copy link
Member Author

Yep, on it, ci-job-features takes a while to run locally 😄

@Manishearth Manishearth merged commit 56b5cf8 into unicode-org:main Jul 16, 2021
@Manishearth Manishearth deleted the thiserror branch July 16, 2021 23:49
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.

Switch from thiserror to displaydoc
4 participants