Move format code back to format file

This commit is contained in:
Damiaan Dufaux 2017-08-02 17:27:49 +02:00
parent 264fd16a28
commit 4c1c7937f5
2 changed files with 58 additions and 59 deletions

View file

@ -21,6 +21,10 @@ public class Encoder {
}
}
enum MsgPackEncodingError: Swift.Error {
case notImplemented, stringNotConvertibleToUTF8(String), valueDidNotAskForContainer
}
class IntermediateEncoder: Swift.Encoder {
var codingPath = [CodingKey]()
@ -47,60 +51,6 @@ class IntermediateEncoder: Swift.Encoder {
}
}
extension Format {
static func from(string: String) throws -> Format {
guard let data = string.data(using: .utf8) else {throw MsgPackEncodingError.stringNotConvertibleToUTF8(string)}
switch data.count {
case 1..<32:
return .fixString(data)
case 32..<256:
return .string8(data)
case 256..<65536:
return .string16(data)
default:
return .string32(data)
}
}
static func from(keyValuePairs: [(Format, Format)]) -> Format {
switch keyValuePairs.count {
case 1..<16:
return .fixMap(keyValuePairs)
case 16..<65536:
return .map16(keyValuePairs)
default:
return .map32(keyValuePairs)
}
}
static func from(array: [Format]) -> Format {
switch array.count {
case 1..<16:
return .fixArray(array)
case 16..<65536:
return .array16(array)
default:
return .array32(array)
}
}
static func from(int: Int) -> Format {
#if arch(arm) || arch(i386)
return .int32(Int32(int))
#else
return .int64(Int64(int))
#endif
}
static func from(uInt: UInt) -> Format {
#if arch(arm) || arch(i386)
return .uInt32(UInt32(uInt))
#else
return .uInt64(UInt64(uInt))
#endif
}
}
class MessagePackEncodingContainer: Swift.Encoder {
var userInfo = [CodingUserInfoKey : Any]()
var codingPath: [CodingKey] = []
@ -131,10 +81,6 @@ class MessagePackEncodingContainer: Swift.Encoder {
}
enum MsgPackEncodingError: Swift.Error {
case notImplemented, stringNotConvertibleToUTF8(String), valueDidNotAskForContainer
}
class MsgPackSingleValueEncodingContainer: MessagePackEncodingContainer, SingleValueEncodingContainer {
var storage: Format?

View file

@ -193,7 +193,6 @@ extension Format {
}
}
extension Data {
mutating func write<T>(value: T, offset: Int) {
withUnsafeMutableBytes {(byteContainer: UnsafeMutablePointer<UInt8>) -> Void in
@ -203,3 +202,57 @@ extension Data {
}
}
}
extension Format {
static func from(string: String) throws -> Format {
guard let data = string.data(using: .utf8) else {throw MsgPackEncodingError.stringNotConvertibleToUTF8(string)}
switch data.count {
case 1..<32:
return .fixString(data)
case 32..<256:
return .string8(data)
case 256..<65536:
return .string16(data)
default:
return .string32(data)
}
}
static func from(keyValuePairs: [(Format, Format)]) -> Format {
switch keyValuePairs.count {
case 1..<16:
return .fixMap(keyValuePairs)
case 16..<65536:
return .map16(keyValuePairs)
default:
return .map32(keyValuePairs)
}
}
static func from(array: [Format]) -> Format {
switch array.count {
case 1..<16:
return .fixArray(array)
case 16..<65536:
return .array16(array)
default:
return .array32(array)
}
}
static func from(int: Int) -> Format {
#if arch(arm) || arch(i386)
return .int32(Int32(int))
#else
return .int64(Int64(int))
#endif
}
static func from(uInt: UInt) -> Format {
#if arch(arm) || arch(i386)
return .uInt32(UInt32(uInt))
#else
return .uInt64(UInt64(uInt))
#endif
}
}