-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
12 changed files
with
282 additions
and
340 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
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 @@ | ||
//=----------------------------------------------------------------------------= | ||
// This source file is part of the Ultimathnum open source project. | ||
// | ||
// Copyright (c) 2023 Oscar Byström Ericsson | ||
// Licensed under Apache License, Version 2.0 | ||
// | ||
// See http://www.apache.org/licenses/LICENSE-2.0 for license information. | ||
//=----------------------------------------------------------------------------= | ||
|
||
//*============================================================================* | ||
// MARK: * Guarantee x Validation | ||
//*============================================================================* | ||
|
||
extension Guarantee { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Initializers | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Creates a new instance by trapping on failure. | ||
/// | ||
/// - Requires: The given `value` must satisfy the `predicate` of this type. | ||
/// | ||
@inlinable public init(_ value: consuming Value) { | ||
precondition(Self.predicate(value), String.brokenInvariant()) | ||
self.init(unsafe:/**/value) | ||
} | ||
|
||
/// Creates a new instance by trapping on failure in debug mode. | ||
/// | ||
/// - Requires: The given `value` must satisfy the `predicate` of this type. | ||
/// | ||
@inlinable public init(unchecked value: consuming Value) { | ||
Swift.assert(Self.predicate(value), String.brokenInvariant()) | ||
self.init(unsafe:/**/value) | ||
} | ||
|
||
/// Creates a new instance by returning `nil` on failure. | ||
/// | ||
/// - Requires: The given `value` must satisfy the `predicate` of this type. | ||
/// | ||
@inlinable public init?(exactly value: consuming Value) { | ||
guard Self.predicate(value) else { return nil } | ||
self.init(unsafe:/**/value) | ||
} | ||
|
||
/// Creates a new instance by throwing the `error()` on failure. | ||
/// | ||
/// - Requires: The given `value` must satisfy the `predicate` of this type. | ||
/// | ||
@inlinable public init<Error>(_ value: consuming Value, prune error: @autoclosure () -> Error) throws where Error: Swift.Error { | ||
guard Self.predicate(value) else { throw error() } | ||
self.init(unsafe:/**/value) | ||
} | ||
} |
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,51 @@ | ||
//=----------------------------------------------------------------------------= | ||
// This source file is part of the Ultimathnum open source project. | ||
// | ||
// Copyright (c) 2023 Oscar Byström Ericsson | ||
// Licensed under Apache License, Version 2.0 | ||
// | ||
// See http://www.apache.org/licenses/LICENSE-2.0 for license information. | ||
//=----------------------------------------------------------------------------= | ||
|
||
//*============================================================================* | ||
// MARK: * Guarantee | ||
//*============================================================================* | ||
|
||
/// A trusted input type. | ||
/// | ||
/// ### Trusted Input | ||
/// | ||
/// This is a trusted input type. Validate inputs with these methods: | ||
/// | ||
/// ```swift | ||
/// init(_:prune:) // error: throws | ||
/// init(exactly:) // error: nil | ||
/// init(_:) // error: precondition | ||
/// init(unchecked:) // error: assert | ||
/// init(unsafe:) // error: %%%%%% | ||
/// ``` | ||
/// | ||
public protocol Guarantee<Value> { | ||
|
||
associatedtype Value | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Metadata | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Indicates whether the given `value` satisfies its `predicate`. | ||
@inlinable static func predicate(_ value: borrowing Value) -> Bool | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Initializers | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Creates a new instance without validation. | ||
/// | ||
/// - Requires: The given `value` must satisfy the `predicate` of this type. | ||
/// | ||
@inlinable init(unsafe value: consuming Value) | ||
|
||
/// Consumes `self` and returns its `value`. | ||
@inlinable consuming func payload() -> Value | ||
} |
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.