I came up with this, just to make it work, but it looks rather complex. Now figuring out how to simplify it ...
mkDocumentFrom(Argk)(Argf)=runExceptT$do-- HACK: this looks badv::repr<-liftEither=<<(lift$fmap(firstDocumentError_MarkupError)$readDoc@repr@b!#relpathk!#pathf)h<-withExceptTDocumentError_MarkupError$liftEither$renderDocvletmetaValueM=extractMetavmetaValue<-maybeToEitherDocumentError_MetadataMissingmetaValueMmeta<-withExceptTDocumentError_MetadataBadJSON$liftEither$resultToEither$fromJSONmetaValuepure$DocumentkvhmetaValueMmetawheremaybeToEithere=\caseNothing->throwErroreJustv->purevresultToEither=\caseErrore->LefteSuccessv->Rightv
mkDocumentFrom::forallmbreprmeta.(MonadError(DocumentErrorrepr)m,MonadIOm,Markuprepr,FromJSONmeta)=>-- | File path, used only to identify (not access) the document"relpath":!PathRelFile->-- | Actual file path, for access and reading"path":!PathbFile->m(Documentreprmeta)mkDocumentFrom(Argk)(Argf)=dov<-liftEither.firstDocumentError_MarkupError=<<liftIO(readDoc!#relpathk!#pathf)html<-liftEither$firstDocumentError_MarkupError$renderDocvletmetaValueM=extractMetavmetaValue<-metaValueM&maybeToEitherDocumentError_MetadataMissingmeta<-liftEither$firstDocumentError_MetadataBadJSON$resultToEither$fromJSONmetaValuepure$DocumentkvhtmlmetaValueMmetawheremaybeToEithere=\caseNothing->throwErroreJustv->purevresultToEither=\caseErrore->LefteSuccessv->Rightv
I came up with this, just to make it work, but it looks rather complex. Now figuring out how to simplify it ...
Improved it to this. Still not sure if I'm happy:
@Sridhar Ratnakumar your
maybeToEither
exists asnote
: https://hackage.haskell.org/package/errors-2.3.0/docs/Control-Error-Util.html#v:note@Fintan Halpenny Good to know about it! I use
relude
as my prelude, and wonder why they don't already have these useful functions ...Wait, they do already have
maybeToRight
, and I can define mine asmaybeToEither e = liftEither . maybeToRight e
This looks a bit strange. Could it be simplified to?:
Not sure how I'd simplify it further. See here for full context: https://github.com/srid/rib/blob/master/src/Rib/Document.hs#L75-L115
The
extractMeta
returns aMaybe (Either e a)
. We want to apply the Missing error for Nothing, and the Malformed error for the inner LeftAh that makes more sense! I was reading the types wrong :)