From 5d3f7eb6b08400547b7f0ee2b075d6b133bc90eb Mon Sep 17 00:00:00 2001 From: Adelyn Breelove Date: Tue, 19 Feb 2019 08:44:59 -0700 Subject: Change channel.t to use more flexible variant --- lib/models/channel/channel_t.ml | 36 ++++++++++++++-------------- lib/models/channel/channel_t.mli | 14 +++++------ lib/models/event_models.ml | 51 +++++++++++++++++++--------------------- 3 files changed, 49 insertions(+), 52 deletions(-) diff --git a/lib/models/channel/channel_t.ml b/lib/models/channel/channel_t.ml index 8611d9c..0734c63 100644 --- a/lib/models/channel/channel_t.ml +++ b/lib/models/channel/channel_t.ml @@ -48,13 +48,13 @@ type category = { name: string; } [@@deriving sexp, yojson { strict = false; exn = true }] -type t = -| Group of group -| Private of dm -| GuildText of guild_text -| GuildVoice of guild_voice -| Category of category -[@@deriving sexp, yojson { strict = false; exn = true }] +type t = [ +| `Group of group +| `Private of dm +| `GuildText of guild_text +| `GuildVoice of guild_voice +| `Category of category +] [@@deriving sexp, yojson { strict = false; exn = true }] type channel_wrapper = { id: Channel_id_t.t; @@ -103,16 +103,16 @@ let unwrap_as_category {id;guild_id;position;name;_} = let wrap s = match s.kind with - | 0 -> GuildText (unwrap_as_guild_text s) - | 1 -> Private (unwrap_as_dm s) - | 2 -> GuildVoice (unwrap_as_guild_voice s) - | 3 -> Group (unwrap_as_group s) - | 4 -> Category (unwrap_as_category s) + | 0 -> `GuildText (unwrap_as_guild_text s) + | 1 -> `Private (unwrap_as_dm s) + | 2 -> `GuildVoice (unwrap_as_guild_voice s) + | 3 -> `Group (unwrap_as_group s) + | 4 -> `Category (unwrap_as_category s) | _ -> raise (Invalid_channel (channel_wrapper_to_yojson s)) -let get_id = function -| Group g -> let `Channel_id id = g.id in id -| Private p -> let `Channel_id id = p.id in id -| GuildText t -> let `Channel_id id = t.id in id -| GuildVoice v -> let `Channel_id id = v.id in id -| Category c -> let `Channel_id id = c.id in id \ No newline at end of file +let get_id (c:t) = match c with +| `Group g -> let `Channel_id id = g.id in id +| `Private p -> let `Channel_id id = p.id in id +| `GuildText t -> let `Channel_id id = t.id in id +| `GuildVoice v -> let `Channel_id id = v.id in id +| `Category c -> let `Channel_id id = c.id in id \ No newline at end of file diff --git a/lib/models/channel/channel_t.mli b/lib/models/channel/channel_t.mli index 313939f..4193c47 100644 --- a/lib/models/channel/channel_t.mli +++ b/lib/models/channel/channel_t.mli @@ -52,13 +52,13 @@ type category = { } [@@deriving sexp, yojson { exn = true }] (** Wrapper variant for all channel types. *) -type t = -| Group of group -| Private of dm -| GuildText of guild_text -| GuildVoice of guild_voice -| Category of category -[@@deriving sexp, yojson { exn = true }] +type t = [ +| `Group of group +| `Private of dm +| `GuildText of guild_text +| `GuildVoice of guild_voice +| `Category of category +] [@@deriving sexp, yojson { exn = true }] (** Intermediate used internally. *) type channel_wrapper = { diff --git a/lib/models/event_models.ml b/lib/models/event_models.ml index 542572f..03bf79c 100644 --- a/lib/models/event_models.ml +++ b/lib/models/event_models.ml @@ -6,27 +6,26 @@ module ChannelCreate = struct let deserialize ev = Channel_t.(channel_wrapper_of_yojson_exn ev |> wrap) - let update_cache (cache:Cache.t) t = - let open Channel_t in + let update_cache (cache:Cache.t) (t:t) = let module C = Cache.ChannelMap in match t with - | GuildText c -> + | `GuildText c -> let text_channels = C.update cache.text_channels c.id ~f:(function | Some _ | None -> c) in { cache with text_channels } - | GuildVoice c -> + | `GuildVoice c -> let voice_channels = C.update cache.voice_channels c.id ~f:(function | Some _ | None -> c) in { cache with voice_channels } - | Category c -> + | `Category c -> let categories = C.update cache.categories c.id ~f:(function | Some _ | None -> c) in { cache with categories } - | Group c -> + | `Group c -> let groups = C.update cache.groups c.id ~f:(function | Some _ | None -> c) in { cache with groups } - | Private c -> + | `Private c -> let private_channels = C.update cache.private_channels c.id ~f:(function | Some _ | None -> c) in { cache with private_channels } @@ -38,23 +37,22 @@ module ChannelDelete = struct let deserialize ev = Channel_t.(channel_wrapper_of_yojson_exn ev |> wrap) - let update_cache (cache:Cache.t) t = - let open Channel_t in + let update_cache (cache:Cache.t) (t:t) = let module C = Cache.ChannelMap in match t with - | GuildText c -> + | `GuildText c -> let text_channels = C.remove cache.text_channels c.id in { cache with text_channels } - | GuildVoice c -> + | `GuildVoice c -> let voice_channels = C.remove cache.voice_channels c.id in { cache with voice_channels } - | Category c -> + | `Category c -> let categories = C.remove cache.categories c.id in { cache with categories } - | Group c -> + | `Group c -> let groups = C.remove cache.groups c.id in { cache with groups } - | Private c -> + | `Private c -> let private_channels = C.remove cache.private_channels c.id in { cache with private_channels } end @@ -65,31 +63,30 @@ module ChannelUpdate = struct let deserialize ev = Channel_t.(channel_wrapper_of_yojson_exn ev |> wrap) - let update_cache (cache:Cache.t) t = - let open Channel_t in + let update_cache (cache:Cache.t) (t:t) = let module C = Cache.ChannelMap in match t with - | GuildText c -> + | `GuildText c -> let text_channels = C.update cache.text_channels c.id ~f:(function | Some _ -> c | None -> c) in { cache with text_channels } - | GuildVoice c -> + | `GuildVoice c -> let voice_channels = C.update cache.voice_channels c.id ~f:(function | Some _ -> c | None -> c) in { cache with voice_channels } - | Category c -> + | `Category c -> let categories = C.update cache.categories c.id ~f:(function | Some _ -> c | None -> c) in { cache with categories } - | Group c -> + | `Group c -> let groups = C.update cache.groups c.id ~f:(function | Some _ -> c | None -> c) in { cache with groups } - | Private c -> + | `Private c -> let private_channels = C.update cache.private_channels c.id ~f:(function | Some _ -> c | None -> c) in @@ -184,9 +181,9 @@ module GuildCreate = struct let unavailable_guilds = Cache.GuildMap.remove cache.unavailable_guilds t.id in let text, voice, cat = ref [], ref [], ref [] in List.iter t.channels ~f:(function - | GuildText c -> text := (c.id, c) :: !text - | GuildVoice c -> voice := (c.id, c) :: !voice - | Category c -> cat := (c.id, c) :: !cat + | `GuildText c -> text := (c.id, c) :: !text + | `GuildVoice c -> voice := (c.id, c) :: !voice + | `Category c -> cat := (c.id, c) :: !cat | _ -> ()); let text_channels = match C.of_alist !text with | `Ok m -> @@ -243,9 +240,9 @@ module GuildDelete = struct let voice_channels = ref cache.voice_channels in let categories = ref cache.categories in List.iter g.channels ~f:(function - | GuildText c -> text_channels := C.remove cache.text_channels c.id - | GuildVoice c -> voice_channels := C.remove cache.voice_channels c.id - | Category c -> categories := C.remove cache.categories c.id + | `GuildText c -> text_channels := C.remove cache.text_channels c.id + | `GuildVoice c -> voice_channels := C.remove cache.voice_channels c.id + | `Category c -> categories := C.remove cache.categories c.id | _ -> () ); let guilds = G.remove cache.guilds g.id in -- cgit v1.2.3