Skip to content

Instantly share code, notes, and snippets.

@eMdOS
Last active May 2, 2020 02:37
Show Gist options
  • Save eMdOS/538393d3ef320b5ae28bed1a50a8c207 to your computer and use it in GitHub Desktop.
Save eMdOS/538393d3ef320b5ae28bed1a50a8c207 to your computer and use it in GitHub Desktop.
public protocol DecodableDataDecoder {
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable
}
extension JSONDecoder: DecodableDataDecoder {}
extension PropertyListDecoder: DecodableDataDecoder {}
public extension Result where Success == Data {
func decode<T: Decodable>(_ type: T.Type, using decoder: DecodableDataDecoder) -> Result<T, Error> {
do {
return .success(try decoder.decode(type, from: try get()))
} catch {
return .failure(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment