crystal-cbor/src/cbor/diagnostic.cr

36 lines
800 B
Crystal
Raw Normal View History

require "./lexer"
require "./token"
# 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
result << value
end
result
end
private def next_value : String?
2020-04-21 15:02:31 +02:00
token = @lexer.read_next
return nil unless token
2020-04-21 15:02:31 +02:00
case token[:kind]
when Kind::BytesArray
BytesArray.new(token[:value]).to_diagnostics
2020-04-21 00:01:35 +02:00
else
2020-04-21 15:02:31 +02:00
Token.do_diagnostics(token)
end
end
end