From d88091ea5a5d730ff5f556b967a65d6ea0aa0951 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Wed, 8 Jan 2025 16:21:27 +0000 Subject: [PATCH] feat: rename extraction option field `isString`->`type` --- README.md | 2 +- examples/tour.luau | 4 ++-- lib/init.luau | 13 ++++++------- tests/edge_cases.luau | 4 ++-- tests/extract.luau | 4 ++-- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 40d03b9..f5b8053 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ print("Directory structure:") reader:walk(function(entry, depth) local prefix = string.rep(" ", depth) local suffix = if not entry.isDirectory - then string.format(" (%d bytes), content: %s", entry.size, reader:extract(entry, { isString = true }) :: string) + then string.format(" (%d bytes), content: %s", entry.size, reader:extract(entry, { type = "text" }) :: string) else "" print(prefix .. entry.name .. suffix) end) diff --git a/examples/tour.luau b/examples/tour.luau index 8f682aa..117aa4c 100644 --- a/examples/tour.luau +++ b/examples/tour.luau @@ -18,7 +18,7 @@ local function formatTree(tree: Tree): Tree -- File, format the value local fileEntry = value :: EntryData - local content = reader:extract(assert(reader:findEntry(fileEntry.path)), { isString = true }) :: string + local content = reader:extract(assert(reader:findEntry(fileEntry.path)), { type = "text" }) :: string local truncContents = content:gsub("\n", ""):sub(1, 100) result[key] = string.format( @@ -72,7 +72,7 @@ reader:walk(function(entry, depth) path = entry:getPath(), size = entry.size, attributes = entry.attributes, - content = reader:extract(entry, { isString = true }) :: string, + content = reader:extract(entry, { type = "text" }) :: string, } end end diff --git a/lib/init.luau b/lib/init.luau index 47cc587..78e247e 100644 --- a/lib/init.luau +++ b/lib/init.luau @@ -59,8 +59,7 @@ type ZipEntryInner = { method: CompressionMethod, -- Method used to compress the file crc: number, -- CRC32 checksum of uncompressed data isDirectory: boolean, -- Whether the entry is a directory or not - -- TODO: Rename to isText or similar in breaking change - isAscii: boolean, -- Whether the entry is plain ASCII text or binary + isText: boolean, -- Whether the entry is plain ASCII text or binary attributes: number, -- File attributes parent: ZipEntry?, -- The parent of the current entry, nil for root children: { ZipEntry }, -- The children of the entry @@ -322,7 +321,7 @@ end type ExtractionOptions = { followSymlinks: boolean?, decompress: boolean?, - isString: boolean?, -- TODO: Rename to isText or similar in breaking change + type: ("binary" | "text")?, skipCrcValidation: boolean?, skipSizeValidation: boolean?, } @@ -348,7 +347,7 @@ function ZipReader.extract(self: ZipReader, entry: ZipEntry, options: Extraction local defaultOptions: ExtractionOptions = { followSymlinks = false, decompress = true, - isString = entry.isAscii, + type = if entry.isText then "text" else "binary", skipValidation = false, } @@ -356,7 +355,7 @@ function ZipReader.extract(self: ZipReader, entry: ZipEntry, options: Extraction local optionsOrDefault: { followSymlinks: boolean, decompress: boolean, - isString: boolean, + type: "binary" | "text", skipCrcValidation: boolean, skipSizeValidation: boolean, } = if options @@ -444,7 +443,7 @@ function ZipReader.extract(self: ZipReader, entry: ZipEntry, options: Extraction end optionsOrDefault.followSymlinks = false - optionsOrDefault.isString = false + optionsOrDefault.type = "binary" optionsOrDefault.skipCrcValidation = true optionsOrDefault.skipSizeValidation = true content = @@ -463,7 +462,7 @@ function ZipReader.extract(self: ZipReader, entry: ZipEntry, options: Extraction ) end - return if optionsOrDefault.isString then buffer.tostring(content) else content + return if optionsOrDefault.type == "text" then buffer.tostring(content) else content end function ZipReader.extractDirectory( diff --git a/tests/edge_cases.luau b/tests/edge_cases.luau index 681d1bf..8dfecac 100644 --- a/tests/edge_cases.luau +++ b/tests/edge_cases.luau @@ -32,10 +32,10 @@ return function(test: typeof(frktest.test)) local entry = assert(zip:findEntry("/pandoc-3.2-arm64/bin/pandoc-lua")) assert(entry:isSymlink(), "Entry type must be a symlink") - local targetPath = zip:extract(entry, { isString = true }) :: string + local targetPath = zip:extract(entry, { type = "text" }) :: string check.equal(targetPath, "pandoc") - local bin = zip:extract(entry, { isString = false, followSymlinks = true }) :: buffer + local bin = zip:extract(entry, { type = "text", followSymlinks = true }) :: buffer local expectedBin = process.spawn("unzip", { "-p", "tests/data/pandoc_soft_links.zip", "pandoc-3.2-arm64/bin/pandoc" }) check.is_true(expectedBin.ok) diff --git a/tests/extract.luau b/tests/extract.luau index 49f4850..48b4a6b 100644 --- a/tests/extract.luau +++ b/tests/extract.luau @@ -37,8 +37,8 @@ return function(test: typeof(frktest.test)) local zip = ZipReader.load(buffer.fromstring(data)) -- Test both string and buffer extraction - local stringOptions = { isString = true, decompress = true } - local bufferOptions = { isString = false, decompress = true } + local stringOptions = { type = "text" :: "text", decompress = true } + local bufferOptions = { type = "binary" :: "binary", decompress = true } -- Extract and verify each file zip:walk(function(entry)