Skip to content

Commit 236047a

Browse files
committed
β0.2.0
perf!: Support luajit instead of lua perf: Refactor `utils.str.split_lines_to_list` to have identical behavior in lua and luajit In testing, this new algorithm is 10.5x slower in lua `( 5.511s -> 63.593s)` but 42% faster in luajit `( 7.439s -> 4.283s)` feat: Implement `fold.fold`, a streamlined API for running crazywall fix(ermsgs): Plan stream logged instead of text stream fix: Replace `math.tointeger` with `tonumber` refactor!: Move `core/` to `crazywall/core/` style: Indent `id` when logging `Section` test: Remove uninvoked error test: Reach 100% error coverage
1 parent 48e66d9 commit 236047a

36 files changed

+306
-176
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
uses: actions/checkout@v3
1616

1717
- name: Install Lua
18-
run: sudo apt-get update && sudo apt-get install -y lua5.4
18+
run: sudo apt-get update && sudo apt-get install -y luajit
1919

2020
- name: Run tests
21-
run: lua ./tests/tests.lua
21+
run: luajit ./tests/tests.lua

configs.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
local utils = require("core.utils")
2-
local Path = require("core.path")
1+
local utils = require("crazywall.core.utils")
2+
local Path = require("crazywall.core.path")
33

44
---@type table<string, PartialConfigTable>
55
local configs = {

core/version.lua

Lines changed: 0 additions & 1 deletion
This file was deleted.
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
local validate = require("core.validate")
2-
local default_config = require("core.defaults.config")
1+
local validate = require("crazywall.core.validate")
2+
local default_config = require("crazywall.core.defaults.config")
33

44
--- @alias NoteSchema [string, string, string][]
55

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
local utils = require("core.utils")
2-
local Path = require("core.path")
3-
local validate = require("core.validate")
1+
local utils = require("crazywall.core.utils")
2+
local Path = require("crazywall.core.path")
3+
local validate = require("crazywall.core.validate")
44
local str = utils.str
5-
local streams = require("core.streams")
6-
local Config = require("core.config")
5+
local streams = require("crazywall.core.streams")
6+
local Config = require("crazywall.core.config")
77

88
--- Represents the current state during execution, configuration options, and
99
--- command-line arguments.
@@ -149,7 +149,7 @@ function Context:new(
149149
and text_stream ~= streams.STDOUT
150150
and text_stream ~= streams.STDERR
151151
then
152-
return nil, Context.errors.invalid_value_for_text_stream(plan_stream)
152+
return nil, Context.errors.invalid_value_for_text_stream(text_stream)
153153
end
154154

155155
self.lines = {}
@@ -161,10 +161,7 @@ function Context:new(
161161
local lines = {}
162162
if type(inp) == type("") then
163163
---@cast inp string
164-
local data = str.split_lines(inp, true)
165-
for line in data do
166-
table.insert(lines, line)
167-
end
164+
lines = str.split_lines_to_list(inp, true)
168165
else
169166
---@cast inp string[]
170167
for i, line in ipairs(inp) do
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
local utils = require("core.utils")
2-
local Path = require("core.path")
1+
local utils = require("crazywall.core.utils")
2+
local Path = require("crazywall.core.path")
33

44
---@type Config
55
local default_config = {

core/fold.lua renamed to crazywall/core/fold.lua

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
local utils = require("core.utils")
1+
local utils = require("crazywall.core.utils")
22
local str = utils.str
3-
local Section = require("core.section")
4-
local traverse = require("core.traverse")
5-
local validate = require("core.validate")
6-
local Plan = require("core.plan.plan")
7-
local Action = require("core.plan.action")
8-
local Path = require("core.path")
3+
local Section = require("crazywall.core.section")
4+
local traverse = require("crazywall.core.traverse")
5+
local validate = require("crazywall.core.validate")
6+
local Plan = require("crazywall.core.plan.plan")
7+
local Action = require("crazywall.core.plan.action")
8+
local Path = require("crazywall.core.path")
99

1010
--- Functions for the primary crazywall operation.
1111
local M = {}
@@ -62,12 +62,6 @@ M.errors = {
6262
.. "\nError: "
6363
.. (err or "")
6464
end,
65-
66-
---@param path string
67-
---@return string errmsg
68-
expected_file_to_be_writable = function(path)
69-
return "Expected file " .. tostring(path) .. " to be writable"
70-
end,
7165
}
7266

7367
---@param ctx Context
@@ -161,7 +155,7 @@ end
161155

162156
---@param section_root Section
163157
---@param ctx Context
164-
---@return nil
158+
---@return boolean?
165159
---@return string? errmsg
166160
M.prepare = function(section_root, ctx)
167161
---@type { [string]: Section }
@@ -262,6 +256,8 @@ M.prepare = function(section_root, ctx)
262256
if err then
263257
return nil, err
264258
end
259+
260+
return true
265261
end
266262

267263
---@param section_root Section
@@ -432,4 +428,53 @@ M.execute = function(section_root, ctx, is_dry_run)
432428
return plan
433429
end
434430

431+
---@param ctx Context
432+
---@param display_plan_func (fun(ctx: Context, plan: Plan, is_dry_run: boolean): nil)?
433+
---@param display_lines_func (fun(ctx: Context, lines: string[], is_dry_run: boolean): nil)?
434+
---@param get_confirmation (fun(): boolean)?
435+
---@return Plan? plan
436+
---@return string? errmsg
437+
---@return Section? root
438+
function M.fold(ctx, display_plan_func, display_lines_func, get_confirmation)
439+
display_plan_func = display_plan_func or function() end
440+
display_lines_func = display_lines_func or function() end
441+
get_confirmation = get_confirmation or function()
442+
return true
443+
end
444+
local root, err = M.parse(ctx)
445+
if not root then
446+
return nil, err
447+
end
448+
449+
_, err = M.prepare(root, ctx)
450+
if err then
451+
return nil, err, root
452+
end
453+
454+
local plan
455+
plan, err = M.execute(root, ctx, true)
456+
if not plan then
457+
return nil, err, root
458+
end
459+
460+
display_plan_func(ctx, plan, ctx.is_dry_run)
461+
if ctx.is_dry_run then
462+
display_lines_func(ctx, root:get_lines(), true)
463+
return plan, nil, root
464+
end
465+
466+
if not ctx.auto_confirm and not get_confirmation() then
467+
return plan, nil, root
468+
end
469+
470+
plan, err = M.execute(root, ctx, false)
471+
if not plan then
472+
return nil, err, root
473+
end
474+
475+
display_lines_func(ctx, root:get_lines(), false)
476+
return plan, nil, root
477+
end
478+
479+
435480
return M
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
local mock_fs_utils = require("core.mock_filesystem.utils")
2-
local Handle = require("core.mock_filesystem.handle")
3-
local ProcessHandle = require("core.mock_filesystem.process_handle")
4-
local validate = require("core.validate")
5-
local Path = require("core.path")
1+
local mock_fs_utils = require("crazywall.core.mock_filesystem.utils")
2+
local Handle = require("crazywall.core.mock_filesystem.handle")
3+
local ProcessHandle = require("crazywall.core.mock_filesystem.process_handle")
4+
local validate = require("crazywall.core.validate")
5+
local Path = require("crazywall.core.path")
66

77
---@class MockFS_IO
88
---@field mock_filesystem MockFilesystem
@@ -53,6 +53,11 @@ MockFS_IO.errors = {
5353
cannot_create_directory_file_exists = function(path)
5454
return "mkdir: cannot create directory `" .. path .. "`: File exists"
5555
end,
56+
57+
--- @return string
58+
manually_forced_error = function()
59+
return "This error was manually forced to be raised."
60+
end,
5661
}
5762

5863
local function make_function_open(self)
@@ -103,6 +108,9 @@ local function make_function_popen(self)
103108
--- @param command string
104109
--- @return MockFS_ProcHandle?, string?
105110
return function(command)
111+
if self.mock_filesystem._debug_error_on_all_commands then
112+
return nil, MockFS_IO.errors.manually_forced_error()
113+
end
106114
local match = string.gmatch(command, "mkdir %-p '(.*)' 2>%&1")()
107115
if not match then
108116
return nil,

0 commit comments

Comments
 (0)