deleted rogue file

This commit is contained in:
Tom Sydney Kerckhove 2021-10-29 20:31:57 +02:00
parent 86d9a7b742
commit 6071b88b0c

69
-
View File

@ -1,69 +0,0 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Autodocodec.Yaml.Document where
import Autodocodec
import Autodocodec.Aeson
import Control.Monad.State
import Data.List.NonEmpty (NonEmpty (..))
import Data.Set (Set)
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Encoding.Error as TE
import Data.Yaml as Yaml
import Text.Colour
schemaChunksViaCodec :: forall a. HasCodec a => [Chunk]
schemaChunksViaCodec = schemaChunksVia (codec @a)
schemaChunksVia :: Codec input output -> [Chunk]
schemaChunksVia = jsonSchemaChunks . jsonSchemaVia
jsonSchemaChunks :: JSONSchema -> [Chunk]
jsonSchemaChunks = concatMap (\l -> l ++ ["\n"]) . (`evalState` S.empty) . go
where
indent :: [[Chunk]] -> [[Chunk]]
indent = map (" " :)
addInFrontOfFirstInList :: [Chunk] -> [[Chunk]] -> [[Chunk]]
addInFrontOfFirstInList cs = \case
[] -> [cs] -- Shouldn't happen, but fine if it doesn't
(l : ls) -> (cs ++ l) : indent ls
go :: JSONSchema -> State (Set Text) [[Chunk]]
go = \case
AnySchema -> pure [[fore yellow "<any>"]]
NullSchema -> pure [[fore yellow "null"]]
BoolSchema -> pure [[fore yellow "<boolean>"]]
StringSchema -> pure [[fore yellow "<string>"]]
NumberSchema -> pure [[fore yellow "<number>"]]
ArraySchema s ->
let addListMarker = addInFrontOfFirstInList ["- "]
in addListMarker <$> go s
ObjectSchema s ->
let requirementComment = \case
Required -> fore red "required"
Optional -> fore blue "optional"
keySchemaFor k (kr, ks) = do
keySchemaChunks <- go ks
pure $ addInFrontOfFirstInList [fore white $ chunk k, ":", " "] (["# ", requirementComment kr] : keySchemaChunks)
in if null s
then pure [["<object>"]]
else concatMap (uncurry keySchemaFor) s
ValueSchema v -> [[chunk $ TE.decodeUtf8With TE.lenientDecode (Yaml.encode v)]]
ChoiceSchema s ->
let addListAround = \case
s_ :| [] -> addInFrontOfFirstInList ["[ "] (go s_) ++ [["]"]]
(s_ :| rest) ->
concat $
addInFrontOfFirstInList ["[ "] (go s_) :
map (addInFrontOfFirstInList [", "] . go) rest
++ [[["]"]]]
in addListAround s
CommentSchema comment s -> [chunk $ "# " <> comment] : go s
ReferenceSchema name s -> [fore cyan $ chunk $ "def: " <> name] : go s