Skip to content

Commit 6b09b8c

Browse files
committed
Created project
1 parent bdd5a21 commit 6b09b8c

File tree

13 files changed

+1615
-0
lines changed

13 files changed

+1615
-0
lines changed

Configs/TLVCoding.plist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>FMWK</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>$(CURRENT_PROJECT_VERSION)</string>
23+
<key>NSHumanReadableCopyright</key>
24+
<string>Copyright © 2018 Alsey Coleman Miller. All rights reserved.</string>
25+
<key>NSPrincipalClass</key>
26+
<string></string>
27+
</dict>
28+
</plist>

Configs/TLVCodingTests.plist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
</dict>
24+
</plist>

Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:4.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "TLVCoding",
8+
products: [
9+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10+
.library(
11+
name: "TLVCoding",
12+
targets: ["TLVCoding"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
21+
.target(
22+
name: "TLVCoding",
23+
dependencies: []),
24+
.testTarget(
25+
name: "TLVCodingTests",
26+
dependencies: ["TLVCoding"]),
27+
]
28+
)

Sources/TLVCodable.swift

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// TLVCoding.swift
3+
// PureSwift
4+
//
5+
// Created by Alsey Coleman Miller on 3/8/18.
6+
// Copyright © 2018 PureSwift. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// Type-Length-Value Codable
12+
public typealias TLVCodable = TLVEncodable & TLVDecodable
13+
14+
/// TLV Decodable type
15+
public protocol TLVDecodable {
16+
17+
static var typeCode: TLVTypeCode { get }
18+
19+
init?(valueData: Foundation.Data)
20+
}
21+
22+
public protocol TLVEncodable {
23+
24+
static var typeCode: TLVTypeCode { get }
25+
26+
var valueData: Foundation.Data { get }
27+
}
28+
29+
/// TLV Type Code header
30+
public protocol TLVTypeCode {
31+
32+
init?(rawValue: UInt8)
33+
34+
var rawValue: UInt8 { get }
35+
}
36+
37+
// MARK: - Codable Implementations
38+
39+
public extension TLVDecodable where Self: RawRepresentable, Self.RawValue: RawRepresentable, Self.RawValue.RawValue: Integer {
40+
41+
public init?(valueData: Foundation.Data) {
42+
43+
typealias IntegerType = Self.RawValue.RawValue
44+
45+
assert(MemoryLayout<IntegerType>.size == 1)
46+
47+
guard valueData.count == 1
48+
else { return nil }
49+
50+
let valueByte = valueData[0]
51+
52+
guard let rawValue = RawValue.init(rawValue: numericCast(valueByte) as IntegerType)
53+
else { return nil }
54+
55+
self.init(rawValue: rawValue)
56+
}
57+
}
58+
59+
public extension TLVEncodable where Self: RawRepresentable, Self.RawValue: RawRepresentable, Self.RawValue.RawValue: Integer {
60+
61+
public var valueData: Foundation.Data {
62+
63+
typealias IntegerType = Self.RawValue.RawValue
64+
65+
assert(MemoryLayout<IntegerType>.size == 1)
66+
67+
let byte = numericCast(rawValue.rawValue) as UInt8
68+
69+
return Data([byte])
70+
}
71+
}
72+
73+
public extension TLVDecodable where Self: RawRepresentable, Self.RawValue: StringProtocol {
74+
75+
public init?(valueData: Foundation.Data) {
76+
77+
guard let string = String(data: valueData, encoding: .utf8) as? Self.RawValue
78+
else { return nil }
79+
80+
self.init(rawValue: string)
81+
}
82+
}
83+
84+
public extension TLVEncodable where Self: RawRepresentable, Self.RawValue: StringProtocol {
85+
86+
public var valueData: Foundation.Data {
87+
88+
guard let data = (self.rawValue as? String)?.data(using: .utf8)
89+
else { fatalError("Could not encode string") }
90+
91+
return data
92+
}
93+
}

TLVCoding.podspec

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Pod::Spec.new do |s|
2+
s.name = "TLVCoding"
3+
s.version = "0.1"
4+
s.summary = ""
5+
s.description = <<-DESC
6+
Your description here.
7+
DESC
8+
s.homepage = "https://github.com/PureSwift/TLVCoding"
9+
s.license = { :type => "MIT", :file => "LICENSE" }
10+
s.author = { "Alsey Coleman Miller" => "[email protected]" }
11+
s.social_media_url = ""
12+
s.ios.deployment_target = "8.0"
13+
s.osx.deployment_target = "10.9"
14+
s.watchos.deployment_target = "2.0"
15+
s.tvos.deployment_target = "9.0"
16+
s.source = { :git => "https://github.com/PureSwift/TLVCoding.git", :tag => s.version.to_s }
17+
s.source_files = "Sources/**/*"
18+
s.frameworks = "Foundation"
19+
end

0 commit comments

Comments
 (0)