diff --git a/src/cbor.cr b/src/cbor.cr index c3c33ef..818fa98 100644 --- a/src/cbor.cr +++ b/src/cbor.cr @@ -5,13 +5,32 @@ require "./cbor/**" module CBOR VERSION = "0.1.0" + # Represents CBOR Hash Keys: everything except Nil, hashs, arrays. + alias HashKeyType = Int8 | Int16 | Int32 | Int64 | Int128 | + UInt8 | UInt16 | UInt32 | UInt64 | + Float32 | Float64 | + String | + Bool | + Bytes + # Represents CBOR types alias Type = Nil | Bool | String | Bytes | Array(Type) | - Hash(Type, Type) | + # Hash(Int8, Type) | + # Hash(Int16, Type) | + # Hash(Int32, Type) | + # Hash(Int64, Type) | + # Hash(UInt8, Type) | + # Hash(UInt16, Type) | + # Hash(UInt32, Type) | + # Hash(UInt64, Type) | + # Hash(Float32, Type) | + # Hash(Float64, Type) | + # Hash(String, Type) | + Hash(HashKeyType, Type) | Int8 | UInt8 | Int16 | diff --git a/src/cbor/decoder.cr b/src/cbor/decoder.cr index 2aa6e8f..43a0fe1 100644 --- a/src/cbor/decoder.cr +++ b/src/cbor/decoder.cr @@ -20,6 +20,34 @@ class CBOR::Decoder @current_token = @lexer.next_token end + # This is similar to read_value but with a focus on a key for a hash. + def read_key : HashKeyType + v = case token = @current_token + when Token::StringT + finish_token! + token.value.as(String) + when Token::IntT + finish_token! + token.value + when Token::FloatT + finish_token! + token.value + when Token::BytesT + finish_token! + token.value.as(Bytes) + when Token::SimpleValueT + finish_token! + token.value.to_t + else + puts "hash key with a #{token.class.to_s} value" + unexpected_token(token) + end + if v.nil? + raise "" + end + v + end + def read_value : Type case token = @current_token when Token::TagT @@ -47,8 +75,11 @@ class CBOR::Decoder arr when Token::MapT finish_token! - map = Hash(Type, Type).new - consume_sequence(token.size) { map[read_value] = read_value } + map = Hash(HashKeyType, Type).new + consume_sequence(token.size) { + key = read_key + map[key] = read_value + } map else unexpected_token(token)