Skip to content

Instantly share code, notes, and snippets.

@Woody88
Last active August 16, 2020 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Woody88/9def82f64a4c8cac7ad64d4d8df74133 to your computer and use it in GitHub Desktop.
Save Woody88/9def82f64a4c8cac7ad64d4d8df74133 to your computer and use it in GitHub Desktop.
module Main where
import Prelude
import Data.Either (Either)
import Data.Symbol (class IsSymbol, SProxy(..))
import Prim.Row as Row
import Prim.RowList (kind RowList)
import Prim.RowList as RL
import Prim.Symbol as Symbol
import Record.Builder (Builder)
import Record.Builder as Builder
import Type.Data.Row (RProxy(..))
import Type.Data.RowList (RLProxy(..))
class Parse (row :: # Type) (specs :: # Type) (conn :: # Type) | row specs -> conn, row conn -> specs where
parse :: RProxy row -> RProxy specs -> String -> Either String { | conn }
instance parseNil :: Parse row () () where
parse _ _ _ = pure {}
else instance parseHasRow ::
( RL.RowToList row rl
, ParseRow rl specs () conn
) => Parse row specs conn where
parse _ _ url = (flip Builder.build {}) <$> parsedRow
where
parsedRow = parseRow (RLProxy :: _ rl) (RProxy :: _ specs) url
class ParseRow (xs :: RowList) (specs :: # Type) (from :: # Type) (to :: # Type) | xs specs -> from to, xs from to -> specs, xs specs from -> to, xs specs to -> from where
parseRow :: RLProxy xs -> RProxy specs -> String -> Either String (Builder { | from } { | to })
class ParseCapture (var :: Symbol) (specs :: # Type) (cfrom :: # Type) (cto :: # Type) | var specs -> cfrom cto where
parseCapture :: SProxy var -> RProxy specs -> String -> Either String (Builder { | cfrom } { | cto })
instance parseNoCapture ::
( IsSymbol var
, Row.Cons var vtype spectail specs
, Row.Lacks var cfrom
, Row.Cons var String cfrom cto
) => ParseCapture var specs cfrom cto where
parseCapture _ _ url = pure $ Builder.insert varP url
where
varP = SProxy :: _ var
-- not sure about this
instance parseRowNil2 :: ParseRow RL.Nil (capture :: { | ctype} | specs) from (capture :: { | ctype } | from ) where
parseRow x y z = do
rest <- parseRow x y z
pure $ Builder.modify captureP (\blr -> (Builder.build blr {})) <<< rest
where
captureP = SProxy :: _ "capture"
else instance parseRowNil :: ParseRow RL.Nil specs from from where
parseRow _ _ _ = pure identity
instance parseRowVar' ::
( IsSymbol var
, Symbol.Append "var_" var sym
, ParseRow tail specs (capture :: Builder (Record cfrom) (Record cfrom') | from) (capture :: Builder (Record cfrom') (Record cfrom') | from')
, Row.Cons "capture" { | capspecs } spectail specs
, ParseCapture var capspecs cfrom' cto
, Row.Lacks "capture" from'
) => ParseRow (RL.Cons sym vtype tail) specs (capture :: Builder (Record cfrom) (Record cfrom') | from) ( capture :: Builder (Record cfrom') (Record cto) | from') where
parseRow _ _ url = do
rest <- parseRow (RLProxy :: _ tail) (RProxy :: _ specs) url
capture <- parseCapture varP (RProxy :: _ capspecs) url
let newVar = Builder.modify captureP (\cap -> capture <<< cap)
pure $ newVar <<< rest
where
captureP = SProxy :: _ "capture"
varP = SProxy :: _ var
else instance parseRowVar ::
( IsSymbol var
, Symbol.Append "var_" var sym
, ParseRow tail specs from from'
, Row.Cons "capture" { | capspecs } spectail specs
, Row.Cons "capture" (Builder { | cfrom } { | cto}) from' to
, ParseCapture var capspecs () cto
, Row.Lacks "capture" from'
) => ParseRow (RL.Cons sym vtype tail) specs from ( capture :: Builder (Record ()) (Record cto) | from') where
parseRow _ _ url = do
rest <- parseRow (RLProxy :: _ tail) (RProxy :: _ specs) url
capture <- parseCapture varP (RProxy :: _ capspecs) url
let newVar = Builder.insert captureP capture
pure $ newVar <<< rest
where
captureP = SProxy :: _ "capture"
varP = SProxy :: _ var
else instance parseRows ::
( ParseRow tail specs from to
) => ParseRow (RL.Cons sym vtype tail) specs from to where
parseRow _ _ url = parseRow (RLProxy :: _ tail) (RProxy :: _ specs) url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment