Skip to content

Commit 9225dc3

Browse files
Joannisfabianfett
andauthored
Introduces a RESP3Token wrapper struct around ByteBuffer (#71)
Co-authored-by: Fabian Fett <[email protected]>
1 parent 89a29d4 commit 9225dc3

File tree

5 files changed

+1077
-0
lines changed

5 files changed

+1077
-0
lines changed

Package.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ let package = Package(
5050
"RediStack"
5151
]
5252
),
53+
.target(
54+
name: "RESP3",
55+
dependencies: [
56+
.product(name: "NIOCore", package: "swift-nio"),
57+
]
58+
),
5359
.testTarget(
5460
name: "RediStackTests",
5561
dependencies: [
@@ -67,6 +73,15 @@ let package = Package(
6773
.product(name: "NIO", package: "swift-nio")
6874
]
6975
),
76+
.testTarget(
77+
name: "RESP3Tests",
78+
dependencies: [
79+
"RESP3",
80+
.product(name: "NIOCore", package: "swift-nio"),
81+
.product(name: "NIOEmbedded", package: "swift-nio"),
82+
.product(name: "NIOTestUtils", package: "swift-nio"),
83+
]
84+
),
7085
.testTarget(
7186
name: "RediStackIntegrationTests",
7287
dependencies: [

Sources/RESP3/RESP3Error.swift

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the RediStack open source project
4+
//
5+
// Copyright (c) 2023 RediStack project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of RediStack project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIOCore
16+
17+
/// This error is thrown if a RESP3 package could not be decoded.
18+
///
19+
/// If you see this error, there a two potential reasons this might happen:
20+
///
21+
/// 1. The Swift RESP3 implementation is wrong
22+
/// 2. You are contacting an untrusted backend
23+
///
24+
struct RESP3ParsingError: Error {
25+
struct Code: Hashable, Sendable, CustomStringConvertible {
26+
private enum Base {
27+
case invalidLeadingByte
28+
case invalidData
29+
case tooDeeplyNestedAggregatedTypes
30+
case missingColonInVerbatimString
31+
case canNotParseInteger
32+
case canNotParseDouble
33+
case canNotParseBigNumber
34+
}
35+
36+
private let base: Base
37+
38+
private init(_ base: Base) {
39+
self.base = base
40+
}
41+
42+
static let invalidLeadingByte = Self.init(.invalidLeadingByte)
43+
static let invalidData = Self.init(.invalidData)
44+
static let tooDeeplyNestedAggregatedTypes = Self.init(.tooDeeplyNestedAggregatedTypes)
45+
static let missingColonInVerbatimString = Self.init(.missingColonInVerbatimString)
46+
static let canNotParseInteger = Self.init(.canNotParseInteger)
47+
static let canNotParseDouble = Self.init(.canNotParseDouble)
48+
static let canNotParseBigNumber = Self.init(.canNotParseBigNumber)
49+
50+
var description: String {
51+
switch self.base {
52+
case .invalidLeadingByte:
53+
return "invalidLeadingByte"
54+
case .invalidData:
55+
return "invalidData"
56+
case .tooDeeplyNestedAggregatedTypes:
57+
return "tooDeeplyNestedAggregatedTypes"
58+
case .missingColonInVerbatimString:
59+
return "missingColonInVerbatimString"
60+
case .canNotParseInteger:
61+
return "canNotParseInteger"
62+
case .canNotParseDouble:
63+
return "canNotParseDouble"
64+
case .canNotParseBigNumber:
65+
return "canNotParseBigNumber"
66+
}
67+
}
68+
}
69+
70+
var code: Code
71+
72+
var buffer: ByteBuffer
73+
}
74+
75+
enum RESP3Error: Error, Equatable {
76+
case dataMalformed
77+
case invalidType(UInt8)
78+
case tooDepplyNestedAggregatedTypes
79+
}

0 commit comments

Comments
 (0)