1- {-# LANGUAGE UndecidableInstances #-}
2-
31module Functora.Cfg
42 ( module X ,
53
@@ -22,11 +20,6 @@ module Functora.Cfg
2220 -- $binary
2321 decodeBinary ,
2422 encodeBinary ,
25-
26- -- * DerivingVia
27- -- $derivingVia
28- GenericEnum (.. ),
29- GenericType (.. ),
3023 )
3124where
3225
@@ -43,10 +36,8 @@ import qualified Data.Binary.Get as Binary
4336import Data.Binary.Instances as X ()
4437import qualified Data.ByteString.Lazy as BL
4538import qualified Data.List.NonEmpty as NE
46- import Functora.CfgOrphan as X ( )
39+ import Functora.CfgOrphan ( genericTomlCodec )
4740import Functora.Prelude
48- import Functora.Uri
49- import qualified GHC.Generics as Generics
5041import qualified Options.Applicative as Cli
5142import Toml as X
5243 ( HasCodec ,
@@ -160,21 +151,6 @@ encodeToml ::
160151encodeToml =
161152 Toml. encode genericTomlCodec
162153
163- genericTomlCodec ::
164- ( Generic a ,
165- Typeable a ,
166- Toml. GenericCodec (Rep a )
167- ) =>
168- TomlCodec a
169- genericTomlCodec =
170- Toml. genericCodecWithOptions
171- Toml. TomlOptions
172- { Toml. tomlOptionsFieldModifier = \ proxy ->
173- Toml. stripTypeNamePrefix proxy . \ case
174- (' _' : xs) -> xs
175- xs -> xs
176- }
177-
178154-- $binary
179155-- Binary
180156
@@ -195,136 +171,3 @@ decodeBinary raw = do
195171
196172encodeBinary :: (Binary a ) => a -> BL. ByteString
197173encodeBinary = Binary. encode
198-
199- -- $derivingVia
200- -- Newtypes to simplify deriving via.
201- -- We have to expose default constructors/accessors
202- -- to help GHC with figuring out that runtime representation does match.
203-
204- newtype GenericEnum a = GenericEnum
205- { unGenericEnum :: a
206- }
207- deriving newtype (Show , Enum , Bounded )
208-
209- instance (Show a , Enum a , Bounded a ) => HasCodec (GenericEnum a ) where
210- hasCodec = Toml. enumBounded
211-
212- instance (Show a , Enum a , Bounded a ) => HasItemCodec (GenericEnum a ) where
213- hasItemCodec = Left Toml. _EnumBounded
214-
215- newtype GenericType a = GenericType
216- { unGenericType :: a
217- }
218- deriving stock (Generic )
219-
220- instance
221- ( Generic a ,
222- Typeable a ,
223- GToQuery (Rep a )
224- ) =>
225- ToQuery (GenericType a )
226- where
227- toQuery = genericToQuery . unGenericType
228-
229- instance
230- ( Generic a ,
231- GFromQuery (Rep a )
232- ) =>
233- FromQuery (GenericType a )
234- where
235- fromQuery = fmap GenericType . genericFromQuery
236-
237- instance
238- ( Generic a ,
239- Typeable a ,
240- Toml. GenericCodec (Rep a )
241- ) =>
242- HasCodec (GenericType a )
243- where
244- hasCodec = Toml. diwrap . Toml. table (genericTomlCodec @ a )
245-
246- instance
247- ( Generic a ,
248- Typeable a ,
249- Toml. GenericCodec (Rep a )
250- ) =>
251- HasItemCodec (GenericType a )
252- where
253- hasItemCodec = Right . Toml. diwrap $ genericTomlCodec @ a
254-
255- instance
256- ( Generic a ,
257- Typeable a ,
258- A. GFromJSON A. Zero (Rep a )
259- ) =>
260- FromJSON (GenericType a )
261- where
262- parseJSON = fmap GenericType . A. genericParseJSON (optsAeson @ a )
263-
264- instance
265- ( Generic a ,
266- Typeable a ,
267- A. GFromJSON A. Zero (Rep a ),
268- A. GFromJSONKey (Rep a )
269- ) =>
270- FromJSONKey (GenericType a )
271- where
272- fromJSONKey = GenericType <$> A. genericFromJSONKey A. defaultJSONKeyOptions
273-
274- instance
275- ( Generic a ,
276- Typeable a ,
277- A. GToJSON A. Zero (Rep a ),
278- A. GToEncoding A. Zero (Rep a )
279- ) =>
280- ToJSON (GenericType a )
281- where
282- toJSON = A. genericToJSON (optsAeson @ a ) . unGenericType
283- toEncoding = A. genericToEncoding (optsAeson @ a ) . unGenericType
284-
285- instance
286- ( Generic a ,
287- Typeable a ,
288- A. GToJSON A. Zero (Rep a ),
289- A. GToEncoding A. Zero (Rep a ),
290- A. GToJSONKey (Rep a )
291- ) =>
292- ToJSONKey (GenericType a )
293- where
294- toJSONKey = contramap unGenericType $ A. genericToJSONKey A. defaultJSONKeyOptions
295-
296- instance
297- ( Generic a ,
298- Typeable a ,
299- Binary. GBinaryPut (Rep a ),
300- Binary. GBinaryGet (Rep a )
301- ) =>
302- Binary (GenericType a )
303- where
304- putList = defaultPutList
305- put = Binary. gput . Generics. from . unGenericType
306- get = GenericType . Generics. to <$> Binary. gget
307-
308- {-# INLINE defaultPutList #-}
309- defaultPutList :: (Binary a ) => [a ] -> Binary. Put
310- defaultPutList xs = Binary. put (length xs) <> mapM_ Binary. put xs
311-
312- optsAeson :: forall a . (Typeable a ) => A. Options
313- optsAeson =
314- A. defaultOptions
315- { A. fieldLabelModifier = \ case
316- raw@ (' _' : inp) ->
317- case fmt inp of
318- out | out == inp -> fmt raw
319- out -> out
320- raw ->
321- fmt raw,
322- A. constructorTagModifier = id ,
323- A. allNullaryToStringTag = True ,
324- A. omitNothingFields = True ,
325- A. sumEncoding = A. defaultTaggedObject,
326- A. unwrapUnaryRecords = False ,
327- A. tagSingleConstructors = False
328- }
329- where
330- fmt = Toml. stripTypeNamePrefix $ Proxy @ a
0 commit comments