First pass at uniformed types, specs and docs

- spec public functions (skipping controllers, views, live views and
components)
- use types instead of explanations in docs
- remove redundant docs
- fix typos
This commit is contained in:
Claudio Ortolina
2026-03-06 08:33:11 +00:00
parent 99e30d5fdf
commit 7cf9b4e7f8
81 changed files with 652 additions and 300 deletions
+5
View File
@@ -14,6 +14,9 @@ defmodule MusicLibrary.Assets.Asset do
timestamps(type: :utc_datetime)
end
@type t :: %__MODULE__{}
@spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
def changeset(asset, attrs) do
asset
|> cast(attrs, [:content, :format, :properties])
@@ -22,6 +25,7 @@ defmodule MusicLibrary.Assets.Asset do
|> unique_constraint(:hash)
end
@spec image_changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
def image_changeset(asset, attrs) do
asset
|> cast(attrs, [:content, :format])
@@ -60,6 +64,7 @@ defmodule MusicLibrary.Assets.Asset do
}
end
@spec hash(binary()) :: String.t()
def hash(content) do
:crypto.hash(:sha256, content) |> Base.encode16()
end
+5
View File
@@ -1,13 +1,16 @@
defmodule MusicLibrary.Assets.Cache do
@spec new() :: :ets.table()
def new do
:ets.new(__MODULE__, [:named_table, :public, :compressed, read_concurrency: true])
end
@spec set(String.t(), String.t(), binary()) :: true
def set(payload, format, content) do
inserted_at = DateTime.utc_now() |> DateTime.to_unix()
:ets.insert(__MODULE__, {{payload, format}, inserted_at, content})
end
@spec get(String.t(), String.t()) :: {:found, binary()} | :not_found
def get(payload, format) do
case :ets.lookup(__MODULE__, {payload, format}) do
[{{^payload, ^format}, _inserted_at, content}] -> {:found, content}
@@ -15,6 +18,7 @@ defmodule MusicLibrary.Assets.Cache do
end
end
@spec total_content_size() :: non_neg_integer()
def total_content_size do
:ets.foldl(
fn {_key, _inserted_at, content}, acc -> acc + byte_size(content) end,
@@ -23,6 +27,7 @@ defmodule MusicLibrary.Assets.Cache do
)
end
@spec prune(non_neg_integer()) :: non_neg_integer()
def prune(older_than_seconds) do
threshold =
DateTime.utc_now()
+3
View File
@@ -9,13 +9,16 @@ defmodule MusicLibrary.Assets.Image do
@default_size 2000
@default_format "image/jpeg"
@spec fallback_data() :: binary()
def fallback_data, do: unquote(fallback_data)
@spec resize(binary(), pos_integer(), String.t()) :: {:ok, binary()} | {:error, term()}
def resize(cover_data, size \\ @default_size, format \\ @default_format) do
{:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
Image.write_to_buffer(thumb, extension(format))
end
@spec convert(binary(), String.t(), String.t()) :: {:ok, binary()}
def convert(cover_data, data_format, target_format) do
if data_format == target_format do
{:ok, cover_data}
+1
View File
@@ -5,6 +5,7 @@ defmodule MusicLibrary.Assets.Transform do
@type t :: %__MODULE__{}
@type payload :: String.t()
@spec new(map()) :: t()
def new(attrs \\ %{}), do: struct!(__MODULE__, attrs)
@doc """