diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:57:38 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:57:38 +0100 |
| commit | 172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (patch) | |
| tree | cb8613d53ce8051e39937ff05f8dc9b0fe80320e /lib/model/field.ml | |
| parent | fa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 (diff) | |
Refactoring
Diffstat (limited to 'lib/model/field.ml')
| -rw-r--r-- | lib/model/field.ml | 77 |
1 files changed, 46 insertions, 31 deletions
diff --git a/lib/model/field.ml b/lib/model/field.ml index 8242c97..feb712d 100644 --- a/lib/model/field.ml +++ b/lib/model/field.ml @@ -2,28 +2,33 @@ type textI18n = (string * string) list (** A label is just an association list*) type validation = { rule : string list Dsl.t; message : textI18n } -type 't field_ex = T : ('a, 't) Typ.t -> 't field_ex +type 't field_ex = T : ('db, 'ocaml, 't) Typ.t -> 't field_ex -type 't t = { - name : Identifier.t; (** The internal name *) - label : textI18n; - placeholder : textI18n option; - icon : unit option; - type' : 't field_ex; - required : bool; - validation : validation list; - criteria : string list Dsl.t option; - (** Display the field when the criteria is valid *) -} +type 't t = + | F : { + named : ('db, 'ocaml, 't) Typ.t Typ.named; + (** Name and type of the field, tied together *) + label : textI18n; + placeholder : textI18n option; + icon : unit option; + required : bool; + validation : validation list; + criteria : string list Dsl.t option; + (** Display the field when the criteria is valid *) + } + -> 't t + (** The record is a GADT so that [named] keeps its existential data type + ['a] — this is what lets {!named_of} re-stamp it with a known type. + The type stays abstract in the interface. *) -let name : 'ref t -> Identifier.t = fun t -> t.name -let label : 'ref t -> textI18n = fun t -> t.label -let placeholder : 'ref t -> textI18n option = fun t -> t.placeholder -let icon : 'ref t -> unit option = fun t -> t.icon -let type_ : 'ref t -> 'ref field_ex = fun t -> t.type' -let required : 'ref t -> bool = fun t -> t.required -let validation : 'ref t -> validation list = fun t -> t.validation -let criteria : 'ref t -> string list Dsl.t option = fun t -> t.criteria +let name : 'ref t -> Identifier.t = fun (F f) -> Typ.name f.named +let label : 'ref t -> textI18n = fun (F f) -> f.label +let placeholder : 'ref t -> textI18n option = fun (F f) -> f.placeholder +let icon : 'ref t -> unit option = fun (F f) -> f.icon +let type_ : 'ref t -> 'ref field_ex = fun (F f) -> T (Typ.typ f.named) +let required : 'ref t -> bool = fun (F f) -> f.required +let validation : 'ref t -> validation list = fun (F f) -> f.validation +let criteria : 'ref t -> string list Dsl.t option = fun (F f) -> f.criteria let create : 't field_ex -> @@ -35,15 +40,25 @@ let create : ?criteria:string list Dsl.t -> Identifier.t -> 't t = - fun type_ label ?placeholder ?icon ?(required = false) ?(validation = []) + fun (T typ) label ?placeholder ?icon ?(required = false) ?(validation = []) ?criteria name -> - { - name; - label; - placeholder; - icon; - type' = type_; - required; - validation; - criteria; - } + F + { + named = Typ.named ~name ~typ; + label; + placeholder; + icon; + required; + validation; + criteria; + } + +let named_of : type db ocaml ref. + ref t -> (db, ocaml, ref) Typ.t -> ((db, ocaml, ref) Typ.t) Typ.named option = + fun (F f) typ -> + match Typ.eq typ f.named.typ with + | None -> None + | Some Type.Equal -> ( + match Typ.eq_ocaml typ f.named.typ with + | None -> None + | Some Type.Equal -> Some f.named) |
