0 | module Derive.Sqlite3.ToCell
2 | import Sqlite3.Marshall
4 | import Language.Reflection.Util
12 | cellType : Vect n Name -> ParamCon n -> Maybe TTImp
13 | cellType vs (MkParamCon _ _ args) =
14 | case mapMaybe convert $
toList args of
15 | [t] => Just `(ToCellType
~(t))
18 | convert : ConArg n -> Maybe TTImp
19 | convert (CArg _ MW _ t) = Just $
ttimp vs t
25 | toCellImplClaim : (impl : Name) -> (p : ParamTypeInfo) -> Decl
26 | toCellImplClaim impl p = implClaim impl (implType "ToCell" p)
35 | parameters (nms : List Name)
36 | encEnumClause : Con n vs -> Clause
37 | encEnumClause c = patClause (var c.name) `(Just
~(c.namePrim))
39 | encEnum : TypeInfo -> TTImp
41 | lam (lambdaArg x) $
iCase (var x) (var ti.name) (map encEnumClause ti.cons)
43 | toCellEnumDef : Name -> TypeInfo -> Decl
44 | toCellEnumDef f ti =
45 | def f [patClause (var f) `(MkToCell TEXT
~(encEnum ti))]
47 | encNT : ParamCon n -> TTImp
49 | lam (lambdaArg x) $
iCase (var x) implicitFalse
50 | [patClause `(~(var c.name) y
) `(toCell y
)]
52 | toCellNewtypeDef : Name -> TTImp -> ParamCon n -> Decl
53 | toCellNewtypeDef f ct c =
54 | def f [patClause (var f) `(MkToCell
~(ct) ~(encNT c))]
63 | ToCell : List Name -> ParamTypeInfo -> Res (List TopLevel)
65 | let impl := implName p "ToCell"
68 | Right [ TL (toCellImplClaim impl p) (toCellEnumDef nms impl p.info) ]
72 | case cellType p.paramNames c of
73 | Just ct => Right [ TL (toCellImplClaim impl p) (toCellNewtypeDef nms impl ct c) ]
74 | Nothing => Left "Interface ToCell can only be derived for enumerations and newtypes."
75 | _ => Left "Interface ToCell can only be derived for enumerations and newtypes."