mirror of
https://code.sup39.dev/repos/Wqawg
synced 2025-03-20 04:46:22 +09:00
New datatype FedURI
for @id URIs
Using a dedicated type allows to record in the type the guarantees that we provide, such as scheme being HTTPS and authority being present. Allows to replace ugly `fromJust` and such with direct field access.
This commit is contained in:
parent
e325175a9c
commit
8ac559d064
12 changed files with 176 additions and 128 deletions
src/Data/Aeson
|
@ -18,9 +18,6 @@ module Data.Aeson.Local
|
|||
, toEither
|
||||
, fromEither
|
||||
, frg
|
||||
, parseHttpsURI'
|
||||
, parseHttpsURI
|
||||
, renderURI
|
||||
, (.=?)
|
||||
)
|
||||
where
|
||||
|
@ -56,26 +53,6 @@ fromEither (Right y) = Right' y
|
|||
frg :: Text
|
||||
frg = "https://forgefed.angeley.es/ns#"
|
||||
|
||||
parseHttpsURI' :: Text -> Either String URI
|
||||
parseHttpsURI' t =
|
||||
case parseURI $ T.unpack t of
|
||||
Nothing -> Left "Invalid absolute URI"
|
||||
Just u ->
|
||||
if uriScheme u == "https:"
|
||||
then case uriAuthority u of
|
||||
Just a ->
|
||||
if uriUserInfo a == "" && uriPort a == ""
|
||||
then Right u
|
||||
else Left "URI has userinfo or port"
|
||||
Nothing -> Left "URI has empty authority"
|
||||
else Left "URI scheme isn't https"
|
||||
|
||||
parseHttpsURI :: Text -> Parser URI
|
||||
parseHttpsURI = either fail return . parseHttpsURI'
|
||||
|
||||
renderURI :: URI -> String
|
||||
renderURI u = uriToString id u ""
|
||||
|
||||
infixr 8 .=?
|
||||
(.=?) :: ToJSON v => Text -> Maybe v -> Series
|
||||
_ .=? Nothing = mempty
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue