Remove the error return value and instead just throw error

This commit is contained in:
2025-11-15 18:10:24 +01:00
parent 11f0bbee53
commit 3bcc958dda
2 changed files with 55 additions and 104 deletions

View File

@@ -58,12 +58,10 @@ parserDefaultOptions = { delimiter = ",", hasheader = false, hascomments = false
--- Validates options against a set of valid option keys.
--- @param options ParserOptions? The options table to validate
--- @return boolean #True if options are valid
--- @return string? #Error message if invalid, nil if valid
function areOptionsValid(options)
if options == nil then return true, nil end
if options == nil then return end
if type(options) ~= "table" then return false, "options must be a table" end
if type(options) ~= "table" then error("options must be a table") end
-- Build valid options list from validOptions table
local validOptionsStr = ""
@@ -73,12 +71,11 @@ function areOptionsValid(options)
for k, _ in pairs(options) do
if parserDefaultOptions[k] == nil then
return false,
error(
"unknown option: " .. tostring(k) .. " (valid options: " .. validOptionsStr .. ")"
)
end
end
return true, nil
end
--- Parses CSV text into rows and fields using a minimal RFC 4180 state machine.
@@ -99,13 +96,11 @@ end
--- @param csv string The CSV text to parse.
--- @param options ParserOptions? Options for the parser
--- @return table #A table (array) of rows; each row is a table with numeric indices and optionally header-named keys.
--- @return string? #Error message if parsing fails.
function fromCSV(csv, options)
if options == nil then options = {} end
-- Validate options
local isValid, err = areOptionsValid(options)
if not isValid then return {}, err end
areOptionsValid(options)
local delimiter = options.delimiter or parserDefaultOptions.delimiter
local hasheader = options.hasheader or parserDefaultOptions.hasheader
@@ -237,7 +232,7 @@ function fromCSV(csv, options)
else
rawset(t, key, value)
end
end
end,
}
local rows = {}
@@ -251,10 +246,10 @@ function fromCSV(csv, options)
table.insert(rows, row)
end
rows.Headers = headers
return rows, nil
return rows
end
return allRows, nil
return allRows
end
--- Converts a table of rows back to CSV text format (RFC 4180 compliant).
@@ -269,14 +264,12 @@ end
---
--- @param rows table Array of rows, where each row is an array of field values.
--- @param options ParserOptions? Options for the parser
--- @return string? #CSV-formatted text, error string?
--- @return string? #Error message if conversion fails.
--- @return string #CSV-formatted text
function toCSV(rows, options)
if options == nil then options = {} end
-- Validate options
local isValid, err = areOptionsValid(options)
if not isValid then return nil, err end
areOptionsValid(options)
local delimiter = options.delimiter or parserDefaultOptions.delimiter
local includeHeaders = options.hasheader or parserDefaultOptions.hasheader
@@ -332,7 +325,7 @@ function toCSV(rows, options)
table.insert(rowStrings, table.concat(fieldStrings, delimiter))
end
return table.concat(rowStrings, "\n"), nil
return table.concat(rowStrings, "\n")
end
-- String to number conversion helper