-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from DrAma999/feature/DSL
DSL Style for Byte Array Creation
- Loading branch information
Showing
8 changed files
with
502 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// ByteArrayBuilder.swift | ||
// | ||
// | ||
// Created by Andrea Finollo on 30/01/22. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
/// `ByteArrayBuilder` is a `@resultBuilder` to create sequence of `Byte` in a DSL style | ||
@resultBuilder | ||
public enum ByteArrayBuilder { | ||
|
||
public static func buildBlock(_ components: [Byte]...) -> [Byte] { | ||
components.flatMap { $0 } | ||
} | ||
|
||
public static func buildExpression(_ expression: [Byte]) -> [Byte] { | ||
expression | ||
} | ||
|
||
public static func buildExpression(_ expression: Byte) -> [Byte] { | ||
[expression] | ||
} | ||
|
||
public static func buildOptional(_ component: [Byte]?) -> [Byte] { | ||
component ?? [] | ||
} | ||
|
||
public static func buildEither(first component: [Byte]) -> [Byte] { | ||
component | ||
} | ||
|
||
public static func buildEither(second component: [Byte]) -> [Byte] { | ||
component | ||
} | ||
|
||
public static func buildArray(_ components: [[Byte]]) -> [Byte] { | ||
return components.flatMap { $0 } | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// DataConvertibleBuilder.swift | ||
// | ||
// | ||
// Created by Andrea Finollo on 04/02/22. | ||
// | ||
|
||
import Foundation | ||
|
||
/// `DataConvertibleBuilder` is a `@resultBuilder` to create `Data` in a DSL style | ||
@resultBuilder | ||
public enum DataConvertibleBuilder { | ||
|
||
public static func buildBlock(_ components: DataConvertible...) -> Data { | ||
return components.reduce(Data()) { partialResult, value in | ||
var mutBuffer = partialResult | ||
mutBuffer.append(value.data) | ||
return mutBuffer | ||
} | ||
} | ||
|
||
public static func buildExpression(_ expression: [DataConvertible]) -> Data { | ||
return expression.reduce(Data()) { partialResult, value in | ||
var mutBuffer = partialResult | ||
mutBuffer.append(value.data) | ||
return mutBuffer | ||
} | ||
} | ||
|
||
public static func buildExpression(_ expression: DataConvertible) -> Data { | ||
expression.data | ||
} | ||
|
||
public static func buildOptional(_ component: DataConvertible?) -> Data { | ||
return component?.data ?? Data() | ||
} | ||
|
||
public static func buildEither(first component: DataConvertible) -> Data { | ||
return component.data | ||
} | ||
|
||
public static func buildEither(second component: DataConvertible) -> Data { | ||
return component.data | ||
} | ||
|
||
public static func buildArray(_ components: [DataConvertible]) -> Data { | ||
return components | ||
.reduce(Data()) { partialResult, value in | ||
var mutBuffer = partialResult | ||
mutBuffer.append(value.data) | ||
return mutBuffer | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
Sources/BitWiser/DataRepresentable/DataRepresentable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// DataRepresentable.swift | ||
// | ||
// | ||
// Created by Andrea Finollo on 04/02/22. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol ExpressibleByData { | ||
init?(data: Data) | ||
} | ||
|
||
public protocol DataConvertible { | ||
var data: Data { get } | ||
} | ||
|
||
public typealias DataRepresentable = ExpressibleByData & DataConvertible |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.