-
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
7 changed files
with
108 additions
and
161 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,48 @@ | ||
//=----------------------------------------------------------------------------= | ||
// 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: `Self.predicate(value)` must be `true` to succeed. | ||
/// | ||
@inlinable public init(_ value: consuming Value) { | ||
self.init(exactly: value)! | ||
} | ||
|
||
/// Creates a new instance by returning `nil` on failure. | ||
/// | ||
/// - Requires: `Self.predicate(value)` must be `true` to succeed. | ||
/// | ||
@inlinable public init?(exactly value: consuming Value) { | ||
guard Self.predicate(value) else { return nil } | ||
self.init(unchecked: value) | ||
} | ||
|
||
/// Creates a new instance by throwing the `error()` on failure. | ||
/// | ||
/// - Requires: `Self.predicate(value)` must be `true` to succeed. | ||
/// | ||
@inlinable public init<Failure>( | ||
_ value: consuming Value, | ||
prune error: @autoclosure () -> Failure | ||
) throws where Failure: Swift.Error { | ||
guard Self.predicate(value) else { throw error() } | ||
self.init(unchecked: 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,45 @@ | ||
//=----------------------------------------------------------------------------= | ||
// 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. | ||
public protocol Guarantee<Value> { | ||
|
||
associatedtype Value | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Metadata | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Indicates whether the given `value` can be trusted. | ||
@inlinable static func predicate(_ value: borrowing Value) -> Bool | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Initializers | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Creates a new instance without validation in release mode. | ||
/// | ||
/// - Requires: `Self.predicate(value)` must be `true` to succeed. | ||
/// | ||
/// - Warning: Only use this method when you know the `value` is valid. | ||
/// | ||
@_disfavoredOverload // collection.map(Self.init) | ||
@inlinable init(unchecked value: consuming Value) | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Utilities | ||
//=------------------------------------------------------------------------= | ||
|
||
/// The value of this trusted input. | ||
@inlinable var value: Value { get } | ||
} |
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
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