Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZSTD error code on ZSTD.Error where possible #10

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Sources/ZSTD/API/ZSTD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ extension ZSTD: CompressionCapable {
while !finished {
var output: ZSTD_outBuffer_s = ZSTD_outBuffer(dst: writeBuffer, size: outputBufferSize, pos: 0)
let remaining: Int = ZSTD_compressStream2(context.pointer, &output, &input, mode)
if Error.isError(remaining) { throw Error.compress }
if Error.isError(remaining) { throw Error.compress(code: remaining) }
let written: Int = writer.write(writeBuffer, length: output.pos)
guard written == output.pos else {
throw Error.write(expect: output.pos, written: written)
}
finished = lastChunk ? (remaining == 0) : (input.pos == input.size)
}

if input.pos != input.size { throw Error.compress }
if input.pos != input.size { throw Error.compress() }

if lastChunk { break }
}
Expand Down Expand Up @@ -87,7 +87,7 @@ extension ZSTD: CompressionCapable {
while input.pos < input.size {
var output: ZSTD_outBuffer_s = ZSTD_outBuffer(dst: writeBuffer, size: outputBufferSize, pos: 0)
decompressResult = ZSTD_decompressStream(context.pointer, &output, &input)
if Error.isError(decompressResult) { throw Error.decompress }
if Error.isError(decompressResult) { throw Error.decompress(code: decompressResult) }

let written: Int = writer.write(writeBuffer, length: output.pos)
guard written == output.pos else {
Expand Down Expand Up @@ -116,7 +116,7 @@ extension ZSTD: CompressionCapable {
}

let compressResult: Int = ZSTD_compress2(context.pointer, outputBuffer, outputBufferSize, inputBuffer, inputBufferSize)
guard ZSTD_isError(compressResult) == 0 else { throw Error.compress }
guard ZSTD_isError(compressResult) == 0 else { throw Error.compress(code: compressResult) }

let written: Int = writer.write(outputBuffer, length: compressResult)
guard written == compressResult else { throw Error.write(expect: compressResult, written: written) }
Expand Down Expand Up @@ -145,8 +145,8 @@ extension ZSTD: CompressionCapable {
}

let decompressResult: Int = ZSTD_decompressDCtx(context.pointer, outputBuffer, outputBufferSize, inputBuffer, inputBufferSize)
guard ZSTD_isError(decompressResult) == 0 else { throw Error.decompress }
guard decompressResult == outputBufferSize else { throw Error.decompress }
guard ZSTD_isError(decompressResult) == 0 else { throw Error.decompress(code: decompressResult) }
guard decompressResult == outputBufferSize else { throw Error.decompress() }

let written: Int = writer.write(outputBuffer, length: outputBufferSize)
guard written == outputBufferSize else {
Expand Down
4 changes: 2 additions & 2 deletions Sources/ZSTD/InternalTypes/CompressContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ final class CompressContext {
extension CompressContext {
func set(level: ZSTD.Level) throws {
let result: Int = ZSTD_CCtx_setParameter(pointer, ZSTD_c_compressionLevel, level.rawValue)
if ZSTD.Error.isError(result) { throw ZSTD.Error.setParameter(description: ZSTD.Error.errorName(code: result)) }
if ZSTD.Error.isError(result) { throw ZSTD.Error.setParameter(code: result) }
}

func load(dictionary: ZSTD.Dictionary) throws {
let result: Int = try ZSTD_CCtx_loadDictionary(pointer, dictionary.pointer, dictionary.size)
if ZSTD.Error.isError(result) { throw ZSTD.Error.loadDictionary(description: ZSTD.Error.errorName(code: result)) }
if ZSTD.Error.isError(result) { throw ZSTD.Error.loadDictionary(code: result) }
}
}
4 changes: 2 additions & 2 deletions Sources/ZSTD/InternalTypes/DecompressContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ extension DecompressContext {
switch parameter {
case .windowLogMax(let value):
let result: Int = ZSTD_DCtx_setParameter(pointer, ZSTD_d_windowLogMax, value)
if ZSTD.Error.isError(result) { throw ZSTD.Error.setParameter(description: ZSTD.Error.errorName(code: result)) }
if ZSTD.Error.isError(result) { throw ZSTD.Error.setParameter(code: result) }
}
}
}

func load(dictionary: ZSTD.Dictionary) throws {
let result = try ZSTD_DCtx_loadDictionary(pointer, dictionary.pointer, dictionary.size)
if ZSTD.Error.isError(result) { throw ZSTD.Error.loadDictionary(description: ZSTD.Error.errorName(code: result)) }
if ZSTD.Error.isError(result) { throw ZSTD.Error.loadDictionary(code: result) }
}
}
10 changes: 5 additions & 5 deletions Sources/ZSTD/Types/ZSTD.Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public extension ZSTD {
enum Error: Swift.Error {
case encoderCreate
case decoderCreate
case setParameter(description: String?)
case compress
case decompress
case setParameter(code: Int)
case compress(code: Int = 1)
case decompress(code: Int = 1)
case invalidData
case write(expect: Int, written: Int)
case dictionaryData
case loadDictionary(description: String?)
case loadDictionary(code: Int)
}
}

extension ZSTD.Error {
public extension ZSTD.Error {
static func isError(_ code: Int) -> Bool {
ZSTD_isError(code) != 0
}
Expand Down
Loading