open Stdune

type t =
  | Rpc
  | Gc
  | Fd
  | Sandbox
  | Persistent
  | Process
  | Rules
  | Pkg
  | Scheduler
  | Promote
  | Build
  | Debug
  | Config
  | File_watcher
  | Diagnostics
  | Log
  | Cram
  | Action
  | Cache
  | Digest
  | Artifact_substitution
  | Thread

let all =
  [ Rpc
  ; Gc
  ; Fd
  ; Sandbox
  ; Persistent
  ; Process
  ; Rules
  ; Pkg
  ; Scheduler
  ; Promote
  ; Build
  ; Debug
  ; Config
  ; File_watcher
  ; Diagnostics
  ; Log
  ; Cram
  ; Action
  ; Cache
  ; Digest
  ; Artifact_substitution
  ; Thread
  ]
;;

let to_string = function
  | Rpc -> "rpc"
  | Gc -> "gc"
  | Fd -> "fd"
  | Sandbox -> "sandbox"
  | Persistent -> "persistent"
  | Process -> "process"
  | Rules -> "rules"
  | Pkg -> "pkg"
  | Scheduler -> "scheduler"
  | Promote -> "promote"
  | Build -> "build"
  | Debug -> "debug"
  | Config -> "config"
  | File_watcher -> "file_watcher"
  | Diagnostics -> "diagnostics"
  | Log -> "log"
  | Cram -> "cram"
  | Action -> "action"
  | Cache -> "cache"
  | Digest -> "digest"
  | Artifact_substitution -> "artifact_subtitution"
  | Thread -> "thread"
;;

let of_string =
  let all = List.map all ~f:(fun a -> to_string a, a) in
  fun x -> List.assoc_opt x all
;;

let to_dyn t = Dyn.variant (String.uppercase_ascii (to_string t)) []

module Set = Bit_set.Make (struct
    type nonrec t = t

    let to_dyn = to_dyn
    let all = all

    let to_int = function
      | Rpc -> 0
      | Gc -> 1
      | Fd -> 2
      | Sandbox -> 3
      | Persistent -> 4
      | Process -> 5
      | Rules -> 6
      | Pkg -> 7
      | Scheduler -> 8
      | Promote -> 9
      | Build -> 10
      | Debug -> 11
      | Config -> 12
      | File_watcher -> 13
      | Diagnostics -> 14
      | Log -> 15
      | Cram -> 16
      | Action -> 17
      | Cache -> 18
      | Digest -> 19
      | Artifact_substitution -> 20
      | Thread -> 21
    ;;
  end)

let default =
  [ Config
  ; Sandbox
  ; Persistent
  ; Process
  ; Rules
  ; Pkg
  ; Promote
  ; Build
  ; Log
  ; File_watcher
  ; Diagnostics
  ; Cram
  ; Action
  ; Artifact_substitution
  ; Thread
  ]
;;
