{-# OPTIONS_GHC -fno-warn-orphans #-}

module Data.Time.Zones.All.OrphanInstances () where

import Control.Lens
import Data.Aeson qualified as A
import Data.Aeson.Types qualified as A
import Data.OpenApi
import Data.Proxy
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Time.Zones.All
import Database.Selda qualified as Selda

instance A.FromJSON TZLabel where
  parseJSON :: Value -> Parser TZLabel
parseJSON Value
json = do
    Text
text <- String -> Parser Text -> Parser Text
forall a. String -> Parser a -> Parser a
A.prependFailure String
"parsing time zone database identifier failed, " (Parser Text -> Parser Text) -> Parser Text -> Parser Text
forall a b. (a -> b) -> a -> b
$ forall a. FromJSON a => Value -> Parser a
A.parseJSON @T.Text Value
json
    case ByteString -> Maybe TZLabel
fromTZName (ByteString -> Maybe TZLabel) -> ByteString -> Maybe TZLabel
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
T.encodeUtf8 Text
text of
      Maybe TZLabel
Nothing -> String -> Parser TZLabel
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"parsing time zone database identifier failed"
      Just TZLabel
timezoneLabel -> TZLabel -> Parser TZLabel
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TZLabel
timezoneLabel

instance A.ToJSON TZLabel where
  toJSON :: TZLabel -> Value
toJSON = Text -> Value
A.String (Text -> Value) -> (TZLabel -> Text) -> TZLabel -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
T.decodeUtf8 (ByteString -> Text) -> (TZLabel -> ByteString) -> TZLabel -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TZLabel -> ByteString
toTZName

instance ToParamSchema TZLabel where
  toParamSchema :: Proxy TZLabel -> Schema
toParamSchema Proxy TZLabel
Proxy =
    Schema
forall a. Monoid a => a
mempty
      Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe OpenApiType -> Identity (Maybe OpenApiType))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
Lens' Schema (Maybe OpenApiType)
type_ ((Maybe OpenApiType -> Identity (Maybe OpenApiType))
 -> Schema -> Identity Schema)
-> OpenApiType -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ OpenApiType
OpenApiString
      Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Schema -> Identity Schema
forall s a. HasDescription s a => Lens' s a
Lens' Schema (Maybe Text)
description ((Maybe Text -> Identity (Maybe Text))
 -> Schema -> Identity Schema)
-> Text -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"IANA time zone database identifier"

instance ToSchema TZLabel where
  declareNamedSchema :: Proxy TZLabel -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy TZLabel
proxy = NamedSchema -> Declare (Definitions Schema) NamedSchema
forall a. a -> DeclareT (Definitions Schema) Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NamedSchema -> Declare (Definitions Schema) NamedSchema)
-> NamedSchema -> Declare (Definitions Schema) NamedSchema
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Schema -> NamedSchema
NamedSchema (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"TZLabel") (Schema -> NamedSchema) -> Schema -> NamedSchema
forall a b. (a -> b) -> a -> b
$ Proxy TZLabel -> Schema
forall a. ToParamSchema a => Proxy a -> Schema
toParamSchema Proxy TZLabel
proxy

instance Selda.SqlEnum TZLabel where
  toText :: TZLabel -> Text
toText = ByteString -> Text
T.decodeUtf8 (ByteString -> Text) -> (TZLabel -> ByteString) -> TZLabel -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TZLabel -> ByteString
toTZName
  fromText :: Text -> TZLabel
fromText Text
text =
    case ByteString -> Maybe TZLabel
fromTZName (ByteString -> Maybe TZLabel) -> ByteString -> Maybe TZLabel
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
T.encodeUtf8 Text
text of
      Maybe TZLabel
Nothing -> String -> TZLabel
forall a. HasCallStack => String -> a
error (String -> TZLabel) -> String -> TZLabel
forall a b. (a -> b) -> a -> b
$ String
"Failed to read time zone database identifier from an SQL value: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a. Show a => a -> String
show Text
text
      Just TZLabel
timezoneLabel -> TZLabel
timezoneLabel

deriving anyclass instance Selda.SqlType TZLabel