Current section
Files
Jump to
Current section
Files
src/pig@workspace.erl
-module(pig@workspace).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/pig/workspace.gleam").
-export([open/1, close/1, connection/1, write_file/3, read_file/2, read_file_lines/4, list_directory/2, mkdir/2, delete_file/2, remember/3, recall/2, list_keys/2, all_tools/1]).
-export_type([workspace/0, error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-opaque workspace() :: {workspace, sqlight:connection()}.
-type error() :: {sql_error, sqlight:error()} |
{not_found, binary()} |
{not_empty, binary()} |
{already_exists, binary()} |
{invalid_path, binary()} |
{key_error, binary()}.
-file("src/pig/workspace.gleam", 25).
?DOC(" Open a workspace database. Creates if doesn't exist.\n").
-spec open(binary()) -> {ok, workspace()} | {error, error()}.
open(Path) ->
gleam@result:'try'(
gleam@result:map_error(
sqlight:open(Path),
fun(Field@0) -> {sql_error, Field@0} end
),
fun(Conn) -> case pig@workspace@schema:init(Conn) of
{ok, nil} ->
{ok, {workspace, Conn}};
{error, E} ->
_ = sqlight:close(Conn),
{error, {sql_error, E}}
end end
).
-file("src/pig/workspace.gleam", 37).
?DOC(" Close the workspace database.\n").
-spec close(workspace()) -> {ok, nil} | {error, error()}.
close(Ws) ->
_pipe = sqlight:close(erlang:element(2, Ws)),
gleam@result:map_error(_pipe, fun(Field@0) -> {sql_error, Field@0} end).
-file("src/pig/workspace.gleam", 43).
?DOC(" Get the underlying connection (escape hatch for power users).\n").
-spec connection(workspace()) -> sqlight:connection().
connection(Ws) ->
erlang:element(2, Ws).
-file("src/pig/workspace.gleam", 132).
?DOC(" Convert VFS errors to workspace errors.\n").
-spec wrap_vfs_error(pig@workspace@vfs:error()) -> error().
wrap_vfs_error(E) ->
case E of
{sql_error, E@1} ->
{sql_error, E@1};
{not_found, P} ->
{not_found, P};
{not_empty, P@1} ->
{not_empty, P@1};
{already_exists, P@2} ->
{already_exists, P@2};
{invalid_path, P@3} ->
{invalid_path, P@3}
end.
-file("src/pig/workspace.gleam", 48).
?DOC(" Write content to a file.\n").
-spec write_file(workspace(), binary(), binary()) -> {ok, nil} |
{error, error()}.
write_file(Ws, Path, Content) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@vfs:write_file(_pipe, Path, Content),
gleam@result:map_error(_pipe@1, fun wrap_vfs_error/1).
-file("src/pig/workspace.gleam", 59).
?DOC(" Read the full content of a file.\n").
-spec read_file(workspace(), binary()) -> {ok, binary()} | {error, error()}.
read_file(Ws, Path) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@vfs:read_file(_pipe, Path),
gleam@result:map_error(_pipe@1, fun wrap_vfs_error/1).
-file("src/pig/workspace.gleam", 66).
?DOC(" Read lines with offset/limit, formatted with line numbers.\n").
-spec read_file_lines(workspace(), binary(), integer(), integer()) -> {ok,
binary()} |
{error, error()}.
read_file_lines(Ws, Path, Offset, Limit) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@vfs:read_file_lines(_pipe, Path, Offset, Limit),
gleam@result:map_error(_pipe@1, fun wrap_vfs_error/1).
-file("src/pig/workspace.gleam", 78).
?DOC(" List entries in a directory.\n").
-spec list_directory(workspace(), binary()) -> {ok, list(binary())} |
{error, error()}.
list_directory(Ws, Path) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@vfs:list_directory(_pipe, Path),
gleam@result:map_error(_pipe@1, fun wrap_vfs_error/1).
-file("src/pig/workspace.gleam", 88).
?DOC(" Create a directory.\n").
-spec mkdir(workspace(), binary()) -> {ok, nil} | {error, error()}.
mkdir(Ws, Path) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@vfs:mkdir(_pipe, Path),
gleam@result:map_error(_pipe@1, fun wrap_vfs_error/1).
-file("src/pig/workspace.gleam", 95).
?DOC(" Delete a file or empty directory.\n").
-spec delete_file(workspace(), binary()) -> {ok, nil} | {error, error()}.
delete_file(Ws, Path) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@vfs:delete_file(_pipe, Path),
gleam@result:map_error(_pipe@1, fun wrap_vfs_error/1).
-file("src/pig/workspace.gleam", 143).
?DOC(" Convert KV errors to workspace errors.\n").
-spec wrap_kv_error(pig@workspace@kv:error()) -> error().
wrap_kv_error(E) ->
case E of
{sql_error, E@1} ->
{sql_error, E@1};
{not_found, K} ->
{key_error, K}
end.
-file("src/pig/workspace.gleam", 102).
?DOC(" Store a key-value pair.\n").
-spec remember(workspace(), binary(), binary()) -> {ok, nil} | {error, error()}.
remember(Ws, Key, Value) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@kv:remember(_pipe, Key, Value),
gleam@result:map_error(_pipe@1, fun wrap_kv_error/1).
-file("src/pig/workspace.gleam", 113).
?DOC(" Retrieve a value by key.\n").
-spec recall(workspace(), binary()) -> {ok, binary()} | {error, error()}.
recall(Ws, Key) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@kv:recall(_pipe, Key),
gleam@result:map_error(_pipe@1, fun wrap_kv_error/1).
-file("src/pig/workspace.gleam", 120).
?DOC(" List keys matching a prefix.\n").
-spec list_keys(workspace(), binary()) -> {ok, list(binary())} |
{error, error()}.
list_keys(Ws, Prefix) ->
_pipe = erlang:element(2, Ws),
_pipe@1 = pig@workspace@kv:list_keys(_pipe, Prefix),
gleam@result:map_error(_pipe@1, fun wrap_kv_error/1).
-file("src/pig/workspace.gleam", 127).
?DOC(" Get all workspace tools as a list.\n").
-spec all_tools(workspace()) -> list(pig@tool:tool()).
all_tools(Ws) ->
pig@workspace@tools:all_tools(erlang:element(2, Ws)).