https://github.com/inekipelov/swift-result-advance
Collection of extensions for improve Result type
https://github.com/inekipelov/swift-result-advance
extensions failure result success swift
Last synced: 11 months ago
JSON representation
Collection of extensions for improve Result type
- Host: GitHub
- URL: https://github.com/inekipelov/swift-result-advance
- Owner: inekipelov
- License: mit
- Created: 2025-05-20T08:20:51.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-06T11:43:12.000Z (about 1 year ago)
- Last Synced: 2025-06-06T12:38:10.779Z (about 1 year ago)
- Topics: extensions, failure, result, success, swift
- Language: Swift
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ResultAdvance
[](https://swift.org/)
[](https://swift.org/package-manager/)
[](LICENSE)
[](https://github.com/inekipelov/swift-result-advance/actions/workflows/swift.yml)
[](https://developer.apple.com/ios/)
[](https://developer.apple.com/macos/)
[](https://developer.apple.com/tvos/)
[](https://developer.apple.com/watchos/)
A collection of Swift extensions to improve the `Result` type with additional functionality, providing a more ergonomic API for handling results in Swift applications.
## Features
### Properties
- **`success`**: Gets the success value if available, otherwise returns `nil`
- **`failure`**: Gets the failure value if available, otherwise returns `nil`
- **`isSuccess`**: Boolean flag indicating if the result is a success
- **`isFailure`**: Boolean flag indicating if the result is a failure
### Callbacks
- **`onSuccess(_:)`**: Execute a closure when the result is a success
- **`onFailure(_:)`**: Execute a closure when the result is a failure
- **Async variants**: Support for async operations with Task priorities
### Mapping
- **`map(_:)`**: Async-compatible success value mapping
- **`mapFailure(_:)`**: Async-compatible failure value mapping
- **`map(success:failure:)`**: Map both success and failure to a common type
- **Async variants**: Full support for async/await operations
## Installation
### Swift Package Manager
Add the following to your `Package.swift` file:
```swift
dependencies: [
.package(url: "https://github.com/inekipelov/swift-result-advance.git", from: "0.1.0")
]
```
And then add the dependency to your target:
```swift
targets: [
.target(
name: "YourTarget",
dependencies: ["ResultAdvance"]),
]
```
## Usage
Import the package in your Swift files:
```swift
import ResultAdvance
```
### Working with Properties
Access success or failure values directly through properties:
```swift
let result: Result = .success(42)
// Access values directly with properties
if let value = result.success {
print("Success value: \(value)")
}
if let error = result.failure {
print("Error: \(error)")
}
// Check state with boolean properties
if result.isSuccess {
print("Operation succeeded")
}
if result.isFailure {
print("Operation failed")
}
```
### Using Callbacks
Handle success and failure cases with chainable callbacks:
```swift
fetchUserData()
.onSuccess { user in
// Handle successful user data retrieval
updateUI(with: user)
}
.onFailure { error in
// Handle error case
showError(error)
}
```
### Async Support
```swift
// Async callbacks
fetchUserData()
.onSuccess { user in
await saveUserToDatabase(user)
}
.onFailure { error in
await logError(error)
}
// Async mapping
let processedResult = await result.map { value in
return await processValue(value)
}
```
### Mapping Operations
Transform result values with mapping operations:
```swift
// Map success values
let doubledResult = result.map { value in
return value * 2
}
// Map failure values
let mappedErrorResult = result.mapFailure { error in
return AppError.mapped(from: error)
}
// Map to a unified type
let finalValue = result.map(
success: { value in return "Success: \(value)" },
failure: { error in return "Error: \(error)" }
)
```
## License
This project is available under the MIT license. See the [LICENSE](LICENSE) file for more information.