@@ -175,6 +175,57 @@ iex> SmileyUUID.encode("550e8400-e29b-41d4-a716-446655440000")
175175
176176```
177177
178+ ## Implementing Compatible Modules
179+
180+ Starting with version ` v4.1.0 ` , ShortUUID provides a behavior that you can implement to create compatible modules.
181+ This is useful if you want to create your own ShortUUID-compatible module with custom functionality while maintaining a consistent interface.
182+
183+ ``` elixir
184+ defmodule MyCustomShortUUID do
185+ @behaviour ShortUUID .Behaviour
186+
187+ @spec encode (String .t ()) :: {:ok , String .t ()} | {:error , String .t ()}
188+ def encode (uuid) do
189+ # Your custom implementation here
190+ {:ok , " encoded_" <> uuid}
191+ end
192+
193+ @spec encode! (String .t ()) :: String .t () | no_return ()
194+ def encode! (uuid) do
195+ case encode (uuid) do
196+ {:ok , encoded} -> encoded
197+ {:error , msg} -> raise ArgumentError , message: msg
198+ end
199+ end
200+
201+ @spec decode (String .t ()) :: {:ok , String .t ()} | {:error , String .t ()}
202+ def decode (encoded) do
203+ # Your custom implementation here
204+ if String .starts_with? (encoded, " encoded_" ) do
205+ {:ok , String .replace_prefix (encoded, " encoded_" , " " )}
206+ else
207+ {:error , " Invalid format" }
208+ end
209+ end
210+
211+ @spec decode! (String .t ()) :: String .t () | no_return ()
212+ def decode! (encoded) do
213+ case decode (encoded) do
214+ {:ok , decoded} -> decoded
215+ {:error , msg} -> raise ArgumentError , message: msg
216+ end
217+ end
218+ end
219+ ```
220+
221+ The ` ShortUUID.Behaviour ` requires implementing four callbacks:
222+ - ` encode/1 ` - Encode a UUID into a shorter representation
223+ - ` encode!/1 ` - Same as encode/1 but raises on error
224+ - ` decode/1 ` - Decode a shortened UUID back to standard format
225+ - ` decode!/1 ` - Same as decode/1 but raises on error
226+
227+ Using this behavior ensures that your custom implementation will be compatible with code expecting a ShortUUID-compatible module.
228+
178229## Documentation
179230
180231Look up the full documentation at [ https://hexdocs.pm/shortuuid ] ( https://hexdocs.pm/shortuuid ) .
0 commit comments