The :credo_binary_patterns
library contains Credo checks for
writing consistent binary patterns. Elixir lets you do whatever you want.
The checks in this library do not.
Add the :credo_binary_patterns
package to your mix.exs
dependencies:
def deps do
[
{:credo_binary_patterns, "~> 0.2.3", only: [:dev, :test], runtime: false}
]
end
Add the check to your .credo.exs
configuration file.
This check will raise issues if any binary patterns in your code do not follow the expected ordering or formatting.
{CredoBinaryPatterns.Check.Consistency.Pattern}
For float
, integer
, and binary
patterns should follow this ordering of options:
<<x::[endian]-[sign]-[type]-[size]>>
When using bytes
, the size comes before the type option:
<<x::16-bytes>>
Avoid providing default sizes, endians, or sign options with their respective types.
For example:
# Incorrect - integers default to 8 bits
<<x::integer-8>>
# Incorrect - integers default to big-endian
<<x::big-integer>>
See the Elixir Documentation for more information on defaults.)
If you need to use size()
, use the binary
type instead.
# Incorrect
<<x::size(16)-bytes>>
# Re-write as `binary` if you want to use `size`
<<x::binary-size(16)>>
# This is also a valid re-write
<<x::16-bytes>>
If you use bare numeric values as a size in a binary
pattern, consider using bytes
instead. Or, if consistent with the rest of the pattern, use size()
.
# Incorrect
<<x::binary-16>>
# Re-write as `bytes`
<<x::16-bytes>>
# This is also a valid re-write
<<x::binary-size(16)>>
Suppose you write the following code:
defmodule Test do
def test do
# This pattern is out of order (size comes before type)
<<x::32-integer>>
# This pattern does not need to specify the size (integers default to `8`)
<<x::integer-8>>
# This pattens does not need to specify the endian (integers default to `big`)
<<x::big-integer-16>>
end
end
Credo will warn you about the formatting issues:
$ mix credo
Consistency
┃
┃ [C] ↗ [Unneeded endian specified] Please re-write this binary pattern as <<x::integer-16>>
┃ test.ex:10:8 #(Test.test)
┃ [C] ↗ [Unneeded size specified] Please re-write this binary pattern as <<x::integer>>
┃ test.ex:7:8 #(Test.test)
┃ [C] ↗ [Options out of order] Should follow: [endian]-[sign]-[type]-[size]. Please re-write this binary pattern as <<x::integer-32>>
┃ test.ex:4:8 #(Test.test)
This library is licensed under the MIT License like Credo.
Copyright (C) 2023 SmartRent
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.