-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
753431a
commit 29b8720
Showing
2 changed files
with
50 additions
and
0 deletions.
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,45 @@ | ||
/// Parses a request's scheme. | ||
/// | ||
/// Used to require a particular scheme at a particular endpoint. | ||
/// | ||
/// ```swift | ||
/// Route(.case(SiteRoute.custom)) { | ||
/// Scheme("custom") // Only route custom:// requests | ||
/// ... | ||
/// } | ||
/// ``` | ||
public struct Scheme: ParserPrinter { | ||
@usableFromInline | ||
let name: String | ||
|
||
/// A parser of the `http` scheme. | ||
public static let http = Self("http") | ||
|
||
/// A parser of the `https` scheme. | ||
public static let https = Self("https") | ||
|
||
/// A parser of custom schemes. | ||
public static func custom(_ scheme: String) -> Self { | ||
Self(scheme) | ||
} | ||
|
||
/// Initializes a scheme parser with a scheme name. | ||
/// | ||
/// - Parameter name: A method name. | ||
@inlinable | ||
public init(_ name: String) { | ||
self.name = name | ||
} | ||
|
||
@inlinable | ||
public func parse(_ input: inout URLRequestData) throws { | ||
guard let scheme = input.scheme else { throw RoutingError() } | ||
try self.name.parse(scheme) | ||
input.scheme = nil | ||
} | ||
|
||
@inlinable | ||
public func print(_ output: (), into input: inout URLRequestData) { | ||
input.scheme = self.name | ||
} | ||
} |
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