From b8aa6c4bdab2cd7f338619c15b39b3ee40cc34cd Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Sat, 3 Nov 2018 14:42:06 +0100 Subject: [PATCH] fix: compilation under elixir 1.6 by hiding since docs behind macro --- lib/structs.ex | 8 +++-- lib/structs/attachment.ex | 9 +++-- lib/structs/audit_log.ex | 10 ++++-- lib/structs/audit_log_change.ex | 13 ++++--- lib/structs/audit_log_entry.ex | 17 +++++---- lib/structs/channel.ex | 12 ++++--- lib/structs/embed.ex | 9 +++-- lib/structs/emoji.ex | 12 ++++--- lib/structs/guild.ex | 10 ++++-- lib/structs/invite.ex | 10 ++++-- lib/structs/member.ex | 12 ++++--- lib/structs/message.ex | 10 ++++-- lib/structs/overwrite.ex | 10 ++++-- lib/structs/permissions.ex | 40 +++++++++++++-------- lib/structs/presence.ex | 10 ++++-- lib/structs/reaction.ex | 10 ++++-- lib/structs/role.ex | 12 ++++--- lib/structs/user.ex | 12 ++++--- lib/structs/util.ex | 63 ++++++++++++++++++++++++++++++--- lib/structs/voice_state.ex | 10 ++++-- lib/structs/webhook.ex | 12 ++++--- 21 files changed, 226 insertions(+), 85 deletions(-) diff --git a/lib/structs.ex b/lib/structs.ex index 658b521..a7e44bf 100644 --- a/lib/structs.ex +++ b/lib/structs.ex @@ -1,10 +1,12 @@ defmodule Crux.Structs do - @moduledoc since: "0.1.0" @moduledoc """ Provides a unified function to create one or a list of structs, invoking their `create/1` function if available. """ alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") @doc """ Can be implemented by structs to transform the inital data. @@ -69,7 +71,7 @@ defmodule Crux.Structs do """ @spec create(data :: map(), target :: module()) :: struct() @spec create(data :: list(), target :: module()) :: list(struct()) - @doc since: "0.1.0" + Util.since("0.1.0") def create(data, target) def create(nil, _target), do: nil @@ -80,7 +82,7 @@ defmodule Crux.Structs do def create(%{__struct__: target} = data, target), do: data def create(data, target) do - if Keyword.has_key?(target.__info__(:functions), :create) do + if :erlang.function_exported(target, :create, 1) do target.create(data) else data = Util.atomify(data) diff --git a/lib/structs/attachment.ex b/lib/structs/attachment.ex index 5b87655..4e22d6e 100644 --- a/lib/structs/attachment.ex +++ b/lib/structs/attachment.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Attachment do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Attachment Object](https://discordapp.com/developers/docs/resources/channel#attachment-object-attachment-structure) @@ -9,6 +8,9 @@ defmodule Crux.Structs.Attachment do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( id: nil, @@ -20,7 +22,8 @@ defmodule Crux.Structs.Attachment do width: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ id: Crux.Rest.snowflake(), filename: String.t(), @@ -36,6 +39,8 @@ defmodule Crux.Structs.Attachment do > Automatically invoked by `Crux.Structs.create/2`. """ + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/audit_log.ex b/lib/structs/audit_log.ex index bdcbcd2..c11a37b 100644 --- a/lib/structs/audit_log.ex +++ b/lib/structs/audit_log.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.AuditLog do - @moduledoc since: "0.1.6" @moduledoc """ Represents a Discord [Audit Log Object](https://discordapp.com/developers/docs/resources/audit-log#audit-log-object) """ @@ -7,6 +6,9 @@ defmodule Crux.Structs.AuditLog do @behaviour Crux.Structs alias Crux.Structs.{AuditLogEntry, User, Util, Webhook} + require Util + + Util.modulesince("0.1.6") defstruct( webhooks: %{}, @@ -14,7 +16,8 @@ defmodule Crux.Structs.AuditLog do audit_log_entries: %{} ) - @typedoc since: "0.1.6" + Util.typesince("0.1.6") + @type t :: %__MODULE__{ webhooks: %{Crux.Rest.snowflake() => Webhook.t()}, users: %{Crux.Rest.snowflake() => User.t()}, @@ -26,7 +29,8 @@ defmodule Crux.Structs.AuditLog do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.6" + Util.since("0.1.6") + def create(data) do data = data diff --git a/lib/structs/audit_log_change.ex b/lib/structs/audit_log_change.ex index e8dbde3..a87d8e6 100644 --- a/lib/structs/audit_log_change.ex +++ b/lib/structs/audit_log_change.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.AuditLogChange do - @moduledoc since: "0.1.6" @moduledoc """ Represents a Discord [Audit Log Change Object](https://discordapp.com/developers/docs/resources/audit-log#audit-log-change) """ @@ -8,6 +7,9 @@ defmodule Crux.Structs.AuditLogChange do alias Crux.Structs alias Crux.Structs.{Overwrite, Role, Util} + require Util + + Util.modulesince("0.1.6") defstruct( new_value: nil, @@ -20,7 +22,8 @@ defmodule Crux.Structs.AuditLogChange do > Note that the Role object returned by Discord in audit logs is a partial role that only contains id and name. """ - @typedoc since: "0.1.6" + Util.typesince("0.1.6") + @type audit_log_change_value :: String.t() | Crux.Rest.snowflake() @@ -29,7 +32,8 @@ defmodule Crux.Structs.AuditLogChange do | [Role.t()] | [Overwrite.t()] - @typedoc since: "0.1.6" + Util.typesince("0.1.6") + @type t :: %__MODULE__{ new_value: audit_log_change_value() | nil, old_value: audit_log_change_value() | nil, @@ -41,7 +45,8 @@ defmodule Crux.Structs.AuditLogChange do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.6" + Util.since("0.1.6") + def create(data) do data = data diff --git a/lib/structs/audit_log_entry.ex b/lib/structs/audit_log_entry.ex index a4e0646..9ecb8f5 100644 --- a/lib/structs/audit_log_entry.ex +++ b/lib/structs/audit_log_entry.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.AuditLogEntry do - @moduledoc since: "0.1.6" @moduledoc """ Represents a Discord [Audit Log Object](https://discordapp.com/developers/docs/resources/audit-log) """ @@ -7,6 +6,9 @@ defmodule Crux.Structs.AuditLogEntry do @behaviour Crux.Structs alias Crux.Structs.{AuditLogChange, Util} + require Util + + Util.modulesince("0.1.6") @audit_log_events %{ guild_update: 1, @@ -40,7 +42,8 @@ defmodule Crux.Structs.AuditLogEntry do @typedoc """ Union type of audit log event name atoms. """ - @typedoc since: "0.1.6" + Util.typesince("0.1.6") + @type event_name :: :guild_update | :channel_create @@ -75,14 +78,14 @@ defmodule Crux.Structs.AuditLogEntry do Returns a map of all audit log event names with their id """ @spec events() :: %{event_name => non_neg_integer()} - @doc since: "0.1.6" + Util.since("0.1.6") def events, do: @audit_log_events @doc """ Gets the event name from the action type id """ @spec event_name(action_type :: non_neg_integer()) :: atom() - @doc since: "0.1.6" + Util.since("0.1.6") def event_name(action_type), do: Map.get(@audit_log_events_key, action_type) defstruct( @@ -95,7 +98,8 @@ defmodule Crux.Structs.AuditLogEntry do reason: nil ) - @typedoc since: "0.1.6" + Util.typesince("0.1.6") + @type t :: %__MODULE__{ id: Crux.Rest.snowflake(), target_id: Crux.Rest.snowflake(), @@ -111,7 +115,8 @@ defmodule Crux.Structs.AuditLogEntry do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.6" + Util.since("0.1.6") + def create(data) do data = data diff --git a/lib/structs/channel.ex b/lib/structs/channel.ex index 57087f1..5cb37ef 100644 --- a/lib/structs/channel.ex +++ b/lib/structs/channel.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Channel do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Channel Object](https://discordapp.com/developers/docs/resources/channel#channel-object-channel-structure). @@ -33,6 +32,9 @@ defmodule Crux.Structs.Channel do @behaviour Crux.Structs alias Crux.Structs.{Overwrite, Util} + require Util + + Util.modulesince("0.1.0") defstruct( application_id: nil, @@ -55,7 +57,8 @@ defmodule Crux.Structs.Channel do user_limit: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ application_id: Crux.Rest.snowflake(), bitrate: integer(), @@ -82,7 +85,8 @@ defmodule Crux.Structs.Channel do > Automatically invoked by `Crux.Structs.create/2` """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data @@ -116,7 +120,7 @@ defmodule Crux.Structs.Channel do ``` """ @spec to_mention(user :: Crux.Structs.Channel.t()) :: String.t() - @doc since: "0.1.1" + Util.since("0.1.1") def to_mention(%__MODULE__{id: id}), do: "<##{id}>" defimpl String.Chars, for: Crux.Structs.Channel do diff --git a/lib/structs/embed.ex b/lib/structs/embed.ex index 6f4956b..e02579e 100644 --- a/lib/structs/embed.ex +++ b/lib/structs/embed.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Embed do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Embed Object](https://discordapp.com/developers/docs/resources/channel#embed-object-embed-structure). @@ -8,6 +7,11 @@ defmodule Crux.Structs.Embed do @behaviour Crux.Structs + alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") + defstruct( title: nil, type: "rich", @@ -24,7 +28,8 @@ defmodule Crux.Structs.Embed do fields: [] ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ title: String.t() | nil, type: String.t() | nil, diff --git a/lib/structs/emoji.ex b/lib/structs/emoji.ex index 22b7f34..9461640 100644 --- a/lib/structs/emoji.ex +++ b/lib/structs/emoji.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Emoji do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Emoji Object](https://discordapp.com/developers/docs/resources/emoji#emoji-object-emoji-structure). @@ -10,6 +9,9 @@ defmodule Crux.Structs.Emoji do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( animated: nil, @@ -21,7 +23,8 @@ defmodule Crux.Structs.Emoji do managed: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ animated: boolean() | nil, id: Crux.Rest.snowflake() | nil, @@ -37,7 +40,8 @@ defmodule Crux.Structs.Emoji do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data @@ -93,7 +97,7 @@ defmodule Crux.Structs.Emoji do """ @spec to_identifier(emoji :: Crux.Structs.Emoji.t() | Crux.Structs.Reaction.t() | String.t()) :: String.t() - @doc since: "0.1.1" + Util.since("0.1.1") def to_identifier(%Crux.Structs.Reaction{emoji: emoji}), do: to_identifier(emoji) def to_identifier(%__MODULE__{id: nil, name: name}), do: name |> URI.encode_www_form() def to_identifier(%__MODULE__{id: id, name: name, animated: true}), do: "a:#{name}:#{id}" diff --git a/lib/structs/guild.ex b/lib/structs/guild.ex index e47cb77..2d983a8 100644 --- a/lib/structs/guild.ex +++ b/lib/structs/guild.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Guild do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Guild Object](https://discordapp.com/developers/docs/resources/guild#guild-object-guild-structure). @@ -12,6 +11,9 @@ defmodule Crux.Structs.Guild do @behaviour Crux.Structs alias Crux.Structs.{Member, Role, Util, VoiceState} + require Util + + Util.modulesince("0.1.0") defstruct( id: nil, @@ -42,7 +44,8 @@ defmodule Crux.Structs.Guild do # presences: %{} ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ id: Crux.Rest.snowflake(), name: String.t(), @@ -77,7 +80,8 @@ defmodule Crux.Structs.Guild do > Automatically invoked by `Crux.Structs.create/2`. """ # TODO: Write a test - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/invite.ex b/lib/structs/invite.ex index 0971a7f..e7959cf 100644 --- a/lib/structs/invite.ex +++ b/lib/structs/invite.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Invite do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Invite Object](https://discordapp.com/developers/docs/resources/invite#invite-object) @@ -31,6 +30,9 @@ defmodule Crux.Structs.Invite do alias Crux.Structs alias Crux.Structs.{Channel, Guild, User, Util} + require Util + + Util.modulesince("0.1.0") defstruct( # always @@ -49,7 +51,8 @@ defmodule Crux.Structs.Invite do approximate_member_count: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ code: String.t(), guild: Guild.t(), @@ -70,7 +73,8 @@ defmodule Crux.Structs.Invite do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/member.ex b/lib/structs/member.ex index b15bf24..3187c9c 100644 --- a/lib/structs/member.ex +++ b/lib/structs/member.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Member do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Guild Member Object](https://discordapp.com/developers/docs/resources/guild#guild-member-object-guild-member-structure). @@ -10,6 +9,9 @@ defmodule Crux.Structs.Member do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( user: nil, @@ -21,7 +23,8 @@ defmodule Crux.Structs.Member do guild_id: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ user: Crux.Rest.snowflake(), nick: String.t() | nil, @@ -37,7 +40,8 @@ defmodule Crux.Structs.Member do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data @@ -68,7 +72,7 @@ defmodule Crux.Structs.Member do ``` """ @spec to_mention(user :: Crux.Structs.Member.t()) :: String.t() - @doc since: "0.1.1" + Util.since("0.1.1") def to_mention(%__MODULE__{user: id, nick: nil}), do: "<@#{id}>" def to_mention(%__MODULE__{user: id}), do: "<@!#{id}>" diff --git a/lib/structs/message.ex b/lib/structs/message.ex index 0f21c4c..eddead7 100644 --- a/lib/structs/message.ex +++ b/lib/structs/message.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Message do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Message Object](https://discordapp.com/developers/docs/resources/channel#message-object-message-structure). @@ -11,6 +10,9 @@ defmodule Crux.Structs.Message do alias Crux.Structs alias Crux.Structs.{Attachment, Embed, Member, Reaction, User, Util} + require Util + + Util.modulesince("0.1.0") defstruct( attachments: [], @@ -34,7 +36,8 @@ defmodule Crux.Structs.Message do webhook_id: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ attachments: [Attachment.t()], # Might be webhook @@ -63,7 +66,8 @@ defmodule Crux.Structs.Message do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/overwrite.ex b/lib/structs/overwrite.ex index e67dc9e..c694804 100644 --- a/lib/structs/overwrite.ex +++ b/lib/structs/overwrite.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Overwrite do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Overwrite Object](https://discordapp.com/developers/docs/resources/channel#overwrite-object-overwrite-structure). """ @@ -7,6 +6,9 @@ defmodule Crux.Structs.Overwrite do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( id: nil, @@ -15,7 +17,8 @@ defmodule Crux.Structs.Overwrite do deny: 0 ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ id: Crux.Rest.snowflake(), type: String.t(), @@ -28,7 +31,8 @@ defmodule Crux.Structs.Overwrite do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/permissions.ex b/lib/structs/permissions.ex index 8e30c86..b225f9b 100644 --- a/lib/structs/permissions.ex +++ b/lib/structs/permissions.ex @@ -1,11 +1,14 @@ defmodule Crux.Structs.Permissions do - @moduledoc since: "0.1.3" @moduledoc """ Custom non discord api struct to help with working with permissions. """ use Bitwise alias Crux.Structs + alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.3") @permissions %{ create_instant_invite: 1 <<< 0, @@ -44,7 +47,7 @@ defmodule Crux.Structs.Permissions do Returns a map of permissions, keyed under their permission name with the bit value. """ @spec permissions() :: %{permission_name() => non_neg_integer()} - @doc since: "0.1.3" + Util.since("0.1.3") def permissions(), do: @permissions @permission_names @permissions |> Map.keys() @@ -52,7 +55,7 @@ defmodule Crux.Structs.Permissions do Returns a list of permission keys. """ @spec permission_names() :: [permission_name()] - @doc since: "0.1.3" + Util.since("0.1.3") def permission_names(), do: @permission_names @permission_all @permissions |> Map.values() |> Enum.reduce(&|||/2) @@ -60,13 +63,14 @@ defmodule Crux.Structs.Permissions do Returns the integer value of all permissions together. """ @spec permission_all :: pos_integer() - @doc since: "0.1.3" + Util.since("0.1.3") def permission_all(), do: @permission_all @typedoc """ Union type of all valid permission name atoms """ - @typedoc since: "0.1.3" + Util.typesince("0.1.3") + @type permission_name :: :create_instant_invite | :kick_members @@ -103,7 +107,8 @@ defmodule Crux.Structs.Permissions do @typedoc """ Represents a `Crux.Struct.Permissions`. """ - @typedoc since: "0.1.3" + Util.typesince("0.1.3") + @type t :: %__MODULE__{ bitfield: non_neg_integer() } @@ -111,7 +116,7 @@ defmodule Crux.Structs.Permissions do @typedoc """ All valid types which can be directly resolved into a permissions bitfield. """ - @typedoc since: "0.1.3" + Util.typesince("0.1.3") @type permissions :: t() | [permission_name()] | non_neg_integer() | permission_name() @doc ~S""" @@ -146,7 +151,7 @@ defmodule Crux.Structs.Permissions do ``` """ @spec resolve(permissions :: permissions()) :: non_neg_integer() - @doc since: "0.1.3" + Util.since("0.1.3") def resolve(permissions) def resolve(%__MODULE__{bitfield: bitfield}), do: bitfield @@ -179,7 +184,7 @@ defmodule Crux.Structs.Permissions do Creates a new `Crux.Structs.Permissions` struct from a valid `t:permissions/0`. """ @spec new(permissions :: permissions()) :: t() - @doc since: "0.1.3" + Util.since("0.1.3") def new(permissions), do: %__MODULE__{bitfield: resolve(permissions)} @doc """ @@ -188,7 +193,8 @@ defmodule Crux.Structs.Permissions do > The administrator flag implicitly grants all permissions. """ @spec to_map(permissions :: permissions()) :: %{permission_name() => boolean()} - @doc since: "0.1.3" + Util.since("0.1.3") + def to_map(permissions) do permissions = resolve(permissions) @@ -211,7 +217,8 @@ defmodule Crux.Structs.Permissions do ``` """ @spec to_list(permissions :: permissions()) :: [permission_name()] - @doc since: "0.1.3" + Util.since("0.1.3") + def to_list(permissions) do permissions = resolve(permissions) @@ -232,7 +239,8 @@ defmodule Crux.Structs.Permissions do ``` """ @spec add(base :: permissions(), to_add :: permissions()) :: t() - @doc since: "0.1.3" + Util.since("0.1.3") + def add(base, to_add) do to_add = to_add |> resolve() @@ -254,7 +262,8 @@ defmodule Crux.Structs.Permissions do ``` """ @spec remove(base :: permissions(), to_remove :: permissions()) :: t() - @doc since: "0.1.3" + Util.since("0.1.3") + def remove(base, to_remove) do to_remove = to_remove |> resolve() |> bnot() @@ -285,7 +294,8 @@ defmodule Crux.Structs.Permissions do ``` """ @spec has(permissions(), permissions()) :: boolean() - @doc since: "0.1.3" + Util.since("0.1.3") + def has(current, want) do current = resolve(current) @@ -310,7 +320,7 @@ defmodule Crux.Structs.Permissions do guild :: Structs.Guild.t(), channel :: Structs.Channel.t() | nil ) :: t() - @doc since: "0.1.3" + Util.since("0.1.3") def from(member, guild, channel \\ nil) def from(%Structs.Member{user: user_id}, guild, channel), do: from(user_id, guild, channel) def from(%Structs.User{id: user_id}, guild, channel), do: from(user_id, guild, channel) diff --git a/lib/structs/presence.ex b/lib/structs/presence.ex index ae7d498..58a8c5b 100644 --- a/lib/structs/presence.ex +++ b/lib/structs/presence.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Presence do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Presence Object](https://discordapp.com/developers/docs/topics/gateway#presence-update-presence-update-event-fields). @@ -10,6 +9,9 @@ defmodule Crux.Structs.Presence do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( user: nil, @@ -19,7 +21,8 @@ defmodule Crux.Structs.Presence do status: "offline" ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ user: Crux.Rest.snowflake(), # roles: [Crux.Rest.snowflake()], @@ -33,7 +36,8 @@ defmodule Crux.Structs.Presence do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/reaction.ex b/lib/structs/reaction.ex index 8f33d9a..cf4725e 100644 --- a/lib/structs/reaction.ex +++ b/lib/structs/reaction.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Reaction do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Reaction Object](https://discordapp.com/developers/docs/resources/channel#reaction-object). """ @@ -8,6 +7,9 @@ defmodule Crux.Structs.Reaction do alias Crux.Structs alias Crux.Structs.{Util, Emoji} + require Util + + Util.modulesince("0.1.0") defstruct( count: nil, @@ -15,7 +17,8 @@ defmodule Crux.Structs.Reaction do emoji: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ count: integer(), me: boolean, @@ -27,7 +30,8 @@ defmodule Crux.Structs.Reaction do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/role.ex b/lib/structs/role.ex index 824a145..9cfd991 100644 --- a/lib/structs/role.ex +++ b/lib/structs/role.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Role do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Role Object](https://discordapp.com/developers/docs/topics/permissions#role-object-role-structure). """ @@ -7,6 +6,9 @@ defmodule Crux.Structs.Role do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( id: nil, @@ -20,7 +22,8 @@ defmodule Crux.Structs.Role do guild_id: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ id: Crux.Rest.snowflake(), name: String.t(), @@ -38,7 +41,8 @@ defmodule Crux.Structs.Role do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data @@ -61,7 +65,7 @@ defmodule Crux.Structs.Role do ``` """ @spec to_mention(user :: Crux.Structs.Role.t()) :: String.t() - @doc since: "0.1.1" + Util.since("0.1.1") def to_mention(%__MODULE__{id: id}), do: "<@&#{id}>" defimpl String.Chars, for: Crux.Structs.Role do diff --git a/lib/structs/user.ex b/lib/structs/user.ex index c146bf7..cdf5874 100644 --- a/lib/structs/user.ex +++ b/lib/structs/user.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.User do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [User Object](https://discordapp.com/developers/docs/resources/user#user-object-user-structure) """ @@ -7,6 +6,9 @@ defmodule Crux.Structs.User do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( avatar: nil, @@ -16,7 +18,8 @@ defmodule Crux.Structs.User do username: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ avatar: String.t() | nil, bot: boolean(), @@ -30,7 +33,8 @@ defmodule Crux.Structs.User do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data @@ -51,7 +55,7 @@ defmodule Crux.Structs.User do ``` """ @spec to_mention(user :: Crux.Structs.User.t()) :: String.t() - @doc since: "0.1.1" + Util.since("0.1.1") def to_mention(%__MODULE__{id: id}), do: "<@#{id}>" defimpl String.Chars, for: Crux.Structs.User do diff --git a/lib/structs/util.ex b/lib/structs/util.ex index 7c4b19f..8bf52ff 100644 --- a/lib/structs/util.ex +++ b/lib/structs/util.ex @@ -1,11 +1,14 @@ defmodule Crux.Structs.Util do - @moduledoc since: "0.1.0" @moduledoc """ Collection of util functions. """ alias Crux.Structs + if Version.compare(System.version(), "1.7.0") != :lt do + @moduledoc since: "0.1.0" + end + @doc ~s""" Converts a string, likely Discord snowflake, to an integer @@ -27,7 +30,12 @@ defmodule Crux.Structs.Util do ``` """ @spec id_to_int(id :: String.t() | integer() | nil) :: integer() | nil | no_return() - @doc since: "0.1.0" + if Version.compare(System.version(), "1.7.0") != :lt do + @doc since: "0.1.0" + else + @since "0.1.0" + end + def id_to_int(str) when is_bitstring(str), do: String.to_integer(str) def id_to_int(already) when is_integer(already), do: already def id_to_int(nil), do: nil @@ -82,7 +90,12 @@ defmodule Crux.Structs.Util do """ @spec raw_data_to_map(data :: list, target :: module(), key :: atom()) :: map() - @doc since: "0.1.0" + if Version.compare(System.version(), "1.7.0") != :lt do + @doc since: "0.1.0" + else + @since "0.1.0" + end + def raw_data_to_map(data, target, key \\ :id) do Structs.create(data, target) |> Map.new(fn struct -> {Map.fetch!(struct, key), struct} end) @@ -107,7 +120,12 @@ defmodule Crux.Structs.Util do ``` """ @spec string_to_atom(input :: String.t() | atom()) :: atom() - @doc since: "0.1.0" + if Version.compare(System.version(), "1.7.0") != :lt do + @doc since: "0.1.0" + else + @since "0.1.0" + end + def string_to_atom(string) when is_bitstring(string), do: String.to_atom(string) def string_to_atom(atom) when is_atom(atom), do: atom @@ -151,7 +169,12 @@ defmodule Crux.Structs.Util do ``` """ @spec atomify(input :: map() | list()) :: map() | list() - @doc since: "0.1.0" + if Version.compare(System.version(), "1.7.0") != :lt do + @doc since: "0.1.0" + else + @since "0.1.0" + end + def atomify(input) def atomify(%{__struct__: _struct} = struct), do: struct |> Map.from_struct() |> atomify() def atomify(%{} = map), do: Map.new(map, &atomify_kv/1) @@ -159,4 +182,34 @@ defmodule Crux.Structs.Util do def atomify(other), do: other defp atomify_kv({k, v}), do: {string_to_atom(k), atomify(v)} + + # TODO: Remove this as soon as 1.7.0 is required + if Version.compare(System.version(), "1.7.0") != :lt do + defmacro since(version) when is_binary(version) do + quote do + @doc since: unquote(version) + end + end + + defmacro modulesince(version) when is_binary(version) do + quote do + @moduledoc since: unquote(version) + end + end + + defmacro typesince(version) when is_binary(version) do + quote do + @typedoc since: unquote(version) + end + end + else + defmacro since(version) when is_binary(version) do + quote do + @since unquote(version) + end + end + + defmacro modulesince(version) when is_binary(version), do: nil + defmacro typesince(version) when is_binary(version), do: nil + end end diff --git a/lib/structs/voice_state.ex b/lib/structs/voice_state.ex index 47eedd2..0eb73bc 100644 --- a/lib/structs/voice_state.ex +++ b/lib/structs/voice_state.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.VoiceState do - @moduledoc since: "0.1.0" @moduledoc """ Represents a Discord [Voice State Object](https://discordapp.com/developers/docs/resources/voice#voice-state-object-voice-state-structure) """ @@ -7,6 +6,9 @@ defmodule Crux.Structs.VoiceState do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.0") defstruct( guild_id: nil, @@ -21,7 +23,8 @@ defmodule Crux.Structs.VoiceState do suppress: nil ) - @typedoc since: "0.1.0" + Util.typesince("0.1.0") + @type t :: %__MODULE__{ guild_id: Crux.Rest.snowflake(), channel_id: Crux.Rest.snowflake() | nil, @@ -39,7 +42,8 @@ defmodule Crux.Structs.VoiceState do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.0" + Util.since("0.1.0") + def create(data) do data = data diff --git a/lib/structs/webhook.ex b/lib/structs/webhook.ex index a44c19e..971fd8c 100644 --- a/lib/structs/webhook.ex +++ b/lib/structs/webhook.ex @@ -1,5 +1,4 @@ defmodule Crux.Structs.Webhook do - @moduledoc since: "0.1.6" @moduledoc """ Represents a Discord [Webhook Object](https://discordapp.com/developers/docs/resources/webhook#webhook-object) @@ -10,6 +9,9 @@ defmodule Crux.Structs.Webhook do @behaviour Crux.Structs alias Crux.Structs.Util + require Util + + Util.modulesince("0.1.6") defstruct( avatar: nil, @@ -21,7 +23,8 @@ defmodule Crux.Structs.Webhook do user: nil ) - @typedoc since: "0.1.6" + Util.typesince("0.1.6") + @type t :: %__MODULE__{ avatar: String.t() | nil, id: Crux.Rest.snowflake(), @@ -37,7 +40,8 @@ defmodule Crux.Structs.Webhook do > Automatically invoked by `Crux.Structs.create/2`. """ - @doc since: "0.1.6" + Util.since("0.1.6") + def create(data) do data = data @@ -63,7 +67,7 @@ defmodule Crux.Structs.Webhook do ``` """ @spec to_mention(webhook :: Crux.Structs.Webhook.t()) :: String.t() - @doc since: "0.1.6" + Util.since("0.1.6") def to_mention(%__MODULE__{id: id}), do: "<@#{id}>" defimpl String.Chars, for: Crux.Structs.Webhook do