diff options
| -rw-r--r-- | lua/mason-core/fetch.lua | 151 | ||||
| -rw-r--r-- | lua/mason-core/result.lua | 9 | ||||
| -rw-r--r-- | tests/mason-core/result_spec.lua | 25 |
3 files changed, 108 insertions, 77 deletions
diff --git a/lua/mason-core/fetch.lua b/lua/mason-core/fetch.lua index e19040cb..fbb755b8 100644 --- a/lua/mason-core/fetch.lua +++ b/lua/mason-core/fetch.lua @@ -31,7 +31,7 @@ local function fetch(url, opts) opts.headers["User-Agent"] = USER_AGENT log.fmt_debug("Fetching URL %s", url) - local platform_specific = Result.failure() + local platform_specific = Result.failure if platform.is.win then local header_entries = _.join( @@ -40,88 +40,89 @@ local function fetch(url, opts) return ("%q = %q"):format(pair[1], pair[2]) end, _.to_pairs(opts.headers)) ) - local headers = ("@{%s}"):format(header_entries) + local headers = ("-Headers @{%s}"):format(header_entries) if opts.out_file then - platform_specific = powershell.command( - ([[iwr %s -UseBasicParsing -Method %q -Uri %q %s -OutFile %q;]]):format( - headers, - opts.method, - url, - opts.data and ("-Body %s"):format(opts.data) or "", - opts.out_file + platform_specific = function() + return powershell.command( + ([[iwr %s -UseBasicParsing -Method %q -Uri %q %s -OutFile %q;]]):format( + headers, + opts.method, + url, + opts.data and ("-Body %s"):format(opts.data) or "", + opts.out_file + ) ) - ) + end else - platform_specific = powershell.command( - ([[Write-Output (iwr %s -Method %q -UseBasicParsing %s -Uri %q).Content;]]):format( - headers, - opts.method, - opts.data and ("-Body %s"):format(opts.data) or "", - url + platform_specific = function() + return powershell.command( + ([[Write-Output (iwr %s -Method %q -UseBasicParsing %s -Uri %q).Content;]]):format( + headers, + opts.method, + opts.data and ("-Body %s"):format(opts.data) or "", + url + ) ) - ) + end end end - return platform_specific - :recover_catching(function() - local headers = - _.sort_by(_.identity, _.map(_.compose(_.format "--header=%s", _.join ": "), _.to_pairs(opts.headers))) - return spawn - .wget({ - headers, - "-nv", - "-o", - "/dev/null", - "-O", - opts.out_file or "-", - ("--method=%s"):format(opts.method), - opts.data and { - ("--body-data=%s"):format(opts.data) or vim.NIL, - } or vim.NIL, - url, - }) - :get_or_throw() - end) - :recover_catching(function() - local headers = _.sort_by( - _.nth(2), - _.map( - _.compose(function(header) - return { "-H", header } - end, _.join ": "), - _.to_pairs(opts.headers) - ) + local function wget() + local headers = + _.sort_by(_.identity, _.map(_.compose(_.format "--header=%s", _.join ": "), _.to_pairs(opts.headers))) + return spawn.wget { + headers, + "-nv", + "-o", + "/dev/null", + "-O", + opts.out_file or "-", + ("--method=%s"):format(opts.method), + opts.data and { + ("--body-data=%s"):format(opts.data) or vim.NIL, + } or vim.NIL, + url, + } + end + + local function curl() + local headers = _.sort_by( + _.nth(2), + _.map( + _.compose(function(header) + return { "-H", header } + end, _.join ": "), + _.to_pairs(opts.headers) ) - return spawn - .curl({ - headers, - "-fsSL", - { - "-X", - opts.method, - }, - opts.data and { "-d", "@-" } or vim.NIL, - opts.out_file and { "-o", opts.out_file } or vim.NIL, - url, - on_spawn = function(_, stdio) - local stdin = stdio[1] - if opts.data then - log.trace("Writing stdin to curl", opts.data) - stdin:write(opts.data) - end - stdin:close() - end, - }) - :get_or_throw() - end) - :map(function(result) - if opts.out_file then - return result - else - return result.stdout - end - end) + ) + return spawn.curl { + headers, + "-fsSL", + { + "-X", + opts.method, + }, + opts.data and { "-d", "@-" } or vim.NIL, + opts.out_file and { "-o", opts.out_file } or vim.NIL, + url, + on_spawn = function(_, stdio) + local stdin = stdio[1] + if opts.data then + log.trace("Writing stdin to curl", opts.data) + stdin:write(opts.data) + end + stdin:close() + end, + } + end + + return curl():or_else(wget):or_else(platform_specific):map(function(result) + if opts.out_file then + return result + else + return result.stdout + end + end) end return fetch diff --git a/lua/mason-core/result.lua b/lua/mason-core/result.lua index 5c7af3be..c598b967 100644 --- a/lua/mason-core/result.lua +++ b/lua/mason-core/result.lua @@ -156,6 +156,15 @@ function Result:and_then(fn) end end +---@param fn fun(err: any): Result +function Result:or_else(fn) + if self:is_failure() then + return fn(self:err_or_nil()) + else + return self + end +end + ---@param fn fun(): any ---@return Result function Result.run_catching(fn) diff --git a/tests/mason-core/result_spec.lua b/tests/mason-core/result_spec.lua index ee722281..1d8f36fb 100644 --- a/tests/mason-core/result_spec.lua +++ b/tests/mason-core/result_spec.lua @@ -154,7 +154,7 @@ describe("result", function() assert.is_false(opt:is_present()) end) - it("should chain results", function() + it("should chain successful results", function() local success = Result.success("First"):and_then(function(value) return Result.success(value .. " Second") end) @@ -166,7 +166,7 @@ describe("result", function() assert.equals("Error", failure:err_or_nil()) end) - it("should not chain results upon failure", function() + it("should not chain failed results", function() local chain = spy.new() local failure = Result.failure("Error"):and_then(chain) @@ -174,4 +174,25 @@ describe("result", function() assert.equals("Error", failure:err_or_nil()) assert.spy(chain).was_not_called() end) + + it("should chain failed results", function() + local failure = Result.failure("First"):or_else(function(value) + return Result.failure(value .. " Second") + end) + local success = Result.failure("Error"):or_else(Result.success) + + assert.is_true(success:is_success()) + assert.equals("Error", success:get_or_nil()) + assert.is_true(failure:is_failure()) + assert.equals("First Second", failure:err_or_nil()) + end) + + it("should not chain successful results", function() + local chain = spy.new() + local failure = Result.success("Error"):or_else(chain) + + assert.is_true(failure:is_success()) + assert.equals("Error", failure:get_or_nil()) + assert.spy(chain).was_not_called() + end) end) |
