diff --git a/Sources/BSON_OrderedCollections/OrderedDictionary (ext).swift b/Sources/BSON_OrderedCollections/OrderedDictionary (ext).swift index 8439c33..bec1a12 100644 --- a/Sources/BSON_OrderedCollections/OrderedDictionary (ext).swift +++ b/Sources/BSON_OrderedCollections/OrderedDictionary (ext).swift @@ -1,39 +1,33 @@ import BSON import OrderedCollections -extension OrderedDictionary:@retroactive BSONDecodable - where Key == BSON.Key, Value:BSONDecodable +extension OrderedDictionary:@retroactive BSONEncodable, @retroactive BSONDocumentEncodable + where Key:BSON.Keyspace, Value:BSONEncodable { @inlinable public - init(bson:BSON.AnyValue) throws + func encode(to bson:inout BSON.DocumentEncoder) { - try self.init(bson: try .init(bson: consume bson)) - } - @inlinable public - init(bson:BSON.Document) throws - { - self.init() - try bson.parse + for (key, value):(Key, Value) in self.elements { - (field:BSON.FieldDecoder) in - - if case _? = self.updateValue(try field.decode(to: Value.self), - forKey: field.key) - { - throw BSON.DocumentKeyError.duplicate(field.key) - } + value.encode(to: &bson[key]) } } } -extension OrderedDictionary:@retroactive BSONDocumentEncodable, @retroactive BSONEncodable - where Key == BSON.Key, Value:BSONEncodable +extension OrderedDictionary:@retroactive BSONDecodable, @retroactive BSONKeyspaceDecodable + where Key:BSON.Keyspace, Value:BSONDecodable { @inlinable public - func encode(to bson:inout BSON.DocumentEncoder) + init(bson:consuming BSON.KeyspaceDecoder) throws { - for (key, value):(Key, Value) in self.elements + self.init() + while let field:BSON.FieldDecoder = try bson[+] { - bson[key] = value + guard + case nil = self.updateValue(try field.decode(to: Value.self), forKey: field.key) + else + { + throw BSON.DocumentKeyError.duplicate(field.key) + } } } }