Skip to content

Commit

Permalink
Make GzipError struct
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jun 10, 2017
1 parent 51f666f commit c1e83a4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 82 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Change Log
### Changes

- Make `CompressionLevel` struct.
- Make `GzipError` struct.



Expand Down
134 changes: 57 additions & 77 deletions Sources/Data+Gzip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,111 +61,91 @@ public struct CompressionLevel: RawRepresentable, Equatable {
/**
Errors on gzipping/gunzipping based on the zlib error codes.
*/
public enum GzipError: Error {
public struct GzipError: Swift.Error {
// cf. http://www.zlib.net/manual.html

/**
The stream structure was inconsistent.

- underlying zlib error: `Z_STREAM_ERROR` (-2)
- parameter message: returned message by zlib
*/
case stream(message: String)

/**
The input data was corrupted (input stream not conforming to the zlib format or incorrect check value).

- underlying zlib error: `Z_DATA_ERROR` (-3)
- parameter message: returned message by zlib
*/
case data(message: String)

/**
There was not enough memory.

- underlying zlib error: `Z_MEM_ERROR` (-4)
- parameter message: returned message by zlib
*/
case memory(message: String)

/**
No progress is possible or there was not enough room in the output buffer.

- underlying zlib error: `Z_BUF_ERROR` (-5)
- parameter message: returned message by zlib
*/
case buffer(message: String)

/**
The zlib library version is incompatible with the version assumed by the caller.

- underlying zlib error: `Z_VERSION_ERROR` (-6)
- parameter message: returned message by zlib
*/
case version(message: String)
public enum Kind {
/**
The stream structure was inconsistent.

- underlying zlib error: `Z_STREAM_ERROR` (-2)
*/
case stream

/**
The input data was corrupted (input stream not conforming to the zlib format or incorrect check value).

- underlying zlib error: `Z_DATA_ERROR` (-3)
*/
case data

/**
There was not enough memory.

- underlying zlib error: `Z_MEM_ERROR` (-4)
*/
case memory

/**
No progress is possible or there was not enough room in the output buffer.

- underlying zlib error: `Z_BUF_ERROR` (-5)
*/
case buffer

/**
The zlib library version is incompatible with the version assumed by the caller.

- underlying zlib error: `Z_VERSION_ERROR` (-6)
*/
case version

/**
An unknown error occurred.

- parameter code: return error by zlib
*/
case unknown(code: Int)
}

/**
An unknown error occurred.
/// Error kind.
public let kind: Kind

- parameter message: returned message by zlib
- parameter code: return error by zlib
*/
case unknown(message: String, code: Int)
/// Returned message by zlib.
public let message: String


internal init(code: Int32, msg: UnsafePointer<CChar>?) {

let message: String = {
self.message = {
guard let msg = msg, let message = String(validatingUTF8: msg) else {
return "Unknown gzip error"
}
return message
}()

self = {
self.kind = {
switch code {
case Z_STREAM_ERROR:
return .stream(message: message)

return .stream
case Z_DATA_ERROR:
return .data(message: message)

return .data
case Z_MEM_ERROR:
return .memory(message: message)

return .memory
case Z_BUF_ERROR:
return .buffer(message: message)

return .buffer
case Z_VERSION_ERROR:
return .version(message: message)

return .version
default:
return .unknown(message: message, code: Int(code))
return .unknown(code: Int(code))
}
}()
}


public var localizedDescription: String {

let description: String = {
switch self {
case .stream(let message):
return message
case .data(let message):
return message
case .memory(let message):
return message
case .buffer(let message):
return message
case .version(let message):
return message
case .unknown(let message, _):
return message
}
}()

return description
return self.message
}

}
Expand Down
10 changes: 5 additions & 5 deletions Tests/GzipTests/GzipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/*
The MIT License (MIT)

© 2015-2016 1024jp
© 2015-2017 1024jp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -70,10 +70,10 @@ class GzipTests: XCTestCase {
do {
uncompressed = try data.gunzipped()
} catch let error as GzipError {
switch error {
case .data(let message):
XCTAssertEqual(message, "incorrect header check")
XCTAssertEqual(message, error.localizedDescription)
switch error.kind {
case .data:
XCTAssertEqual(error.message, "incorrect header check")
XCTAssertEqual(error.message, error.localizedDescription)
default:
XCTFail("Caught incorrect error.")
}
Expand Down

0 comments on commit c1e83a4

Please sign in to comment.