SnmpKit.SnmpLib.Types.Format (snmpkit v2.0.1)

Human-readable formatting and parsing helpers for SNMP values: uptime from TimeTicks, IP addresses, byte counts, rates, hex strings. SnmpKit.SnmpLib.Types delegates here.

Summary

Functions

Formats bytes as human-readable size.

Formats binary data as hexadecimal string.

Formats an IP address from binary format.

Formats a rate value with units.

@spec encode_value(term(), keyword()) :: {:ok, {snmp_type(), term()}} | {:error, atom()}

Parses a hexadecimal string to binary.

Parses an IP address string into a 4-tuple of integers.

Truncates a string to a maximum length with ellipsis.

Functions

format_bytes(bytes)

@spec format_bytes(non_neg_integer()) :: binary()

Formats bytes as human-readable size.

Examples

"1.5 KB" = SnmpKit.SnmpLib.Types.format_bytes(1536)
"2.3 MB" = SnmpKit.SnmpLib.Types.format_bytes(2400000)

format_hex(binary)

@spec format_hex(binary()) :: binary()

Formats binary data as hexadecimal string.

Examples

"48656C6C6F" = SnmpKit.SnmpLib.Types.format_hex(<<"Hello">>)
"DEADBEEF" = SnmpKit.SnmpLib.Types.format_hex(<<0xDE, 0xAD, 0xBE, 0xEF>>)

format_ip_address(arg1)

@spec format_ip_address(binary()) :: binary()

Formats an IP address from binary format.

Examples

"192.168.1.1" = SnmpKit.SnmpLib.Types.format_ip_address(<<192, 168, 1, 1>>)
"0.0.0.0" = SnmpKit.SnmpLib.Types.format_ip_address(<<0, 0, 0, 0>>)

format_rate(value, unit)

@spec format_rate(number(), binary()) :: binary()

Formats a rate value with units.

Examples

"100 bps" = SnmpKit.SnmpLib.Types.format_rate(100, "bps")
"1.5 Mbps" = SnmpKit.SnmpLib.Types.format_rate(1500000, "bps")

format_timeticks_uptime(centiseconds)

@spec format_timeticks_uptime(non_neg_integer()) :: binary()

@spec encode_value(term(), keyword()) :: {:ok, {snmp_type(), term()}} | {:error, atom()}

@doc """ Formats TimeTicks as human-readable uptime string.

Parameters

  • centiseconds: Time in centiseconds (hundredths of a second)

Returns

  • Human-readable uptime string

Examples

"42 centiseconds" = SnmpKit.SnmpLib.Types.format_timeticks_uptime(42)
"1 second 50 centiseconds" = SnmpKit.SnmpLib.Types.format_timeticks_uptime(150)
"1 minute 30 seconds" = SnmpKit.SnmpLib.Types.format_timeticks_uptime(9000)
"2 hours 15 minutes 30 seconds" = SnmpKit.SnmpLib.Types.format_timeticks_uptime(81300)

parse_hex_string(hex_string)

@spec parse_hex_string(binary()) :: {:ok, binary()} | {:error, atom()}

Parses a hexadecimal string to binary.

Examples

{:ok, <<"Hello">>} = SnmpKit.SnmpLib.Types.parse_hex_string("48656C6C6F")
{:error, :invalid_hex} = SnmpKit.SnmpLib.Types.parse_hex_string("XYZ")

parse_ip_address(ip_string)

@spec parse_ip_address(binary()) ::
  {:ok, {0..255, 0..255, 0..255, 0..255}} | {:error, atom()}

Parses an IP address string into a 4-tuple of integers.

Parameters

  • ip_string: IP address as a string like "192.168.1.1"

Returns

  • {:ok, {a, b, c, d}} on success
  • {:error, reason} on failure

Examples

{:ok, {192, 168, 1, 1}} = SnmpKit.SnmpLib.Types.parse_ip_address("192.168.1.1")
{:ok, {127, 0, 0, 1}} = SnmpKit.SnmpLib.Types.parse_ip_address("127.0.0.1")
{:error, :invalid_format} = SnmpKit.SnmpLib.Types.parse_ip_address("invalid")

truncate_string(string, max_length)

@spec truncate_string(binary(), pos_integer()) :: binary()

Truncates a string to a maximum length with ellipsis.

Examples

"hello" = SnmpKit.SnmpLib.Types.truncate_string("hello", 10)
"hello..." = SnmpKit.SnmpLib.Types.truncate_string("hello world", 8)