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

Add function to read UTF-8 files #459

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ulidtko
Copy link
Contributor

@ulidtko ulidtko commented Aug 29, 2024

Following up to #372 and #407, consider a snippet

 [] do
      mRssPages <- readMay @Int . (!! 1) . T.split (== ' ') <$> readFileText "/proc/self/statm"
      []

Produces deprecation warning:

In the use of ‘readFileText’
"'readFileText' depends on the system's locale settings and can throw unexpected exceptions.
 Use 'readFileBS' instead."

I've read the documented deprecations, and I fully understand the motivating blog post of Mr. Snoyberg.

However, those locale concerns simply don't apply here. Because I'm writing code to read a guaranteed small file that's nearly guaranteed to never exceed the 12-character alphabet 0123456789 \n.

So then, can I silence the deprecation warning on this specific use-site? No — all deprecations over the entire module only.

Sigh... 😔 Sure, writing yet another little helper once more is far from hard:

readFileUtf8 :: MonadIO m => FilePath -> m Text
readFileUtf8 = fmap decodeUtf8Lenient . readFileBS

And everybody is doing that already anyway. Yet by this same reasoning, the entire Relude.File module is redundant.


Constructively: I'm proposing a function:

readFileUtf8 :: (MonadIO m, ConvertUtf8 r ByteString) => FilePath -> m r

which:

  • reads text from files, strictly;
  • interacts nicely with Relude's String.Conversions module,
  • thus avoids the whole clunky series readFileUtf8Text, readFileUtf8LText, readFileUtf8String elegantly.

I'll add lazy version on demand.

Checklist:

HLint

  • I've changed the exposed interface (add new reexports, remove reexports, rename reexported things, etc.).
    — new API, no need for hlint changes.

General

  • I've updated the CHANGELOG with the short description of my latest changes.
  • All new and existing tests pass.
  • I keep the code style used in the files I've changed (see style-guide for more details).
  • I've used the stylish-haskell file.
  • My change requires the documentation updates.
    • I've updated the documentation accordingly.
  • I've added the [ci skip] text to the docs-only related commit's name.

@tomjaguarpaw
Copy link
Contributor

Hi @ulidtko, I'm a volunteer who helps keep Relude building. API decisions are taken by @vrom911.

@ulidtko
Copy link
Contributor Author

ulidtko commented Aug 30, 2024 via email

@ulidtko
Copy link
Contributor Author

ulidtko commented Sep 2, 2024

... BTW, it wasn't my conscious choice to set you as reviewer @tomjaguarpaw; GitHub did it itself. I believe it auto-assigns reviewers based on the CODEOWNERS file.

@tomjaguarpaw
Copy link
Contributor

I see, that's good to know, thanks.

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.

2 participants