purebred-mua / purebred-email

A fast email parsing library implemented in Haskell
https://hackage.haskell.org/package/purebred-email
GNU Affero General Public License v3.0
23 stars 4 forks source link

Add useful MIME optics? #71

Closed tomjaguarpaw closed 1 year ago

tomjaguarpaw commented 2 years ago

It seems like these optics would be useful to add. The parseErrors one was particularly useful for tracking down the error in https://github.com/purebred-mua/purebred-email/issues/70. I don't see any way of getting hold of the errors otherwise!

If you think they would be useful addition then I can make a PR. Suggestions for the best names would be welcome.

bodyOfMIMEMessage :: Lens' MIMEMessage MIME
bodyOfMIMEMessage f = \case
  Message h msg -> Message h <$> f msg

-- MIMEParseError is not exported
parseErrors :: Traversal' MIME _
parseErrors f = \case
  Part b -> pure (Part b)
  Encapsulated msg -> Encapsulated <$> traverseOf bodyOfMIMEMessage (parseErrors f) msg
  Multipart sub b bs -> Multipart sub b <$> traverseOf (traversed . bodyOfMIMEMessage) (parseErrors f) bs
  FailedParse e bs -> flip FailedParse bs <$> f e
frasertweedale commented 2 years ago

Thanks @tomjaguarpaw for the suggestion. bodyOfMIMEMessage exists (with a more general type) as Data.IMF.body. I'll contemplate the name and whether it's better to have as the big type MIME, MIMEMessage or perhaps both. But I'm happy to adopt this idea.

tomjaguarpaw commented 2 years ago

bodyOfMIMEMessage exists (with a more general type) as Data.IMF.body

Ah, I didn't see it because I only looked in MIME (although now I've seen it I realise I've used it before).

tomjaguarpaw commented 2 years ago

Ah, and I see it is exported from MIME but it is not in the docs because it is part of a module re-export.

frasertweedale commented 1 year ago

Closing as the ask seems to be already satisfied.