2020-04-20 14:57:20 +02:00
|
|
|
# Reads a CBOR input into a diagnostic string.
|
|
|
|
# This consumes the IO and is mostly usedful to tests again the example
|
|
|
|
# provided in the RFC and ensuring a correct functioning of the `CBOR::Lexer`.
|
|
|
|
class CBOR::Diagnostic
|
|
|
|
@lexer : Lexer
|
|
|
|
|
|
|
|
def initialize(input)
|
|
|
|
@lexer = Lexer.new(input)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reads the content of the IO and prints out a diagnostic string
|
|
|
|
# represation of the input.
|
|
|
|
def to_s : String
|
|
|
|
result = ""
|
2020-04-21 15:02:31 +02:00
|
|
|
while value = next_value
|
2020-04-21 22:48:27 +02:00
|
|
|
result += value
|
2020-04-20 14:57:20 +02:00
|
|
|
end
|
|
|
|
result
|
2020-04-19 20:40:10 +02:00
|
|
|
end
|
|
|
|
|
2020-04-20 14:57:20 +02:00
|
|
|
private def next_value : String?
|
2020-04-21 15:02:31 +02:00
|
|
|
token = @lexer.read_next
|
2020-04-20 14:57:20 +02:00
|
|
|
return nil unless token
|
|
|
|
|
2020-04-22 11:17:06 +02:00
|
|
|
case token.kind
|
2020-04-21 22:48:27 +02:00
|
|
|
when Kind::Int
|
2020-04-22 11:17:06 +02:00
|
|
|
token.value.to_s
|
2020-04-21 23:18:56 +02:00
|
|
|
when Kind::String
|
2020-04-22 13:28:03 +02:00
|
|
|
if token.chunks
|
|
|
|
chunks = chunks(token.value.as(String), token.chunks.as(Array(Int32)))
|
|
|
|
"(_ #{chunks.map { |s| string(s) }.join(", ")})"
|
|
|
|
else
|
|
|
|
string(token.value.as(String))
|
|
|
|
end
|
2020-04-21 22:48:27 +02:00
|
|
|
when Kind::Bytes
|
2020-04-22 13:28:03 +02:00
|
|
|
if token.chunks
|
|
|
|
chunks = chunks(token.value.as(Bytes), token.chunks.as(Array(Int32)))
|
|
|
|
"(_ #{chunks.map { |b| bytes(b) }.join(", ")})"
|
|
|
|
else
|
|
|
|
bytes(token.value.as(Bytes))
|
|
|
|
end
|
2020-04-21 00:01:35 +02:00
|
|
|
else
|
2020-04-22 11:17:06 +02:00
|
|
|
token.kind.to_s
|
2020-04-19 20:40:10 +02:00
|
|
|
end
|
2020-04-20 14:57:20 +02:00
|
|
|
end
|
2020-04-22 13:28:03 +02:00
|
|
|
|
|
|
|
private def chunks(value : Bytes, chunks : Array(Int32)) : Array(Bytes)
|
|
|
|
res = Array(Bytes).new
|
|
|
|
bytes = value.to_a
|
|
|
|
chunks.each do |size|
|
|
|
|
bytes_chunk = bytes.shift(size)
|
|
|
|
res << Bytes.new(bytes_chunk.to_unsafe, bytes_chunk.size)
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
private def chunks(value : String, chunks : Array(Int32)) : Array(String)
|
|
|
|
res = Array(String).new
|
|
|
|
arr = value.split("")
|
|
|
|
chunks.each do |size|
|
|
|
|
res << arr.shift(size).join
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
private def bytes(b : Bytes) : String
|
|
|
|
"h'#{b.hexstring}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
private def string(s : String) : String
|
|
|
|
%("#{s}")
|
|
|
|
end
|
2020-04-19 20:40:10 +02:00
|
|
|
end
|