-module(dream_http_client@storage). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/dream_http_client/storage.gleam"). -export([load_recordings/1, save_recordings/2]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " File I/O for recordings\n" "\n" " Handles loading and saving recording files to/from the filesystem.\n" ). -file("src/dream_http_client/storage.gleam", 86). -spec build_file_path(binary()) -> binary(). build_file_path(Directory) -> Dir = case gleam_stdlib:string_ends_with(Directory, <<"/"/utf8>>) of true -> Directory; false -> <> end, <>. -file("src/dream_http_client/storage.gleam", 15). ?DOC( " Load recordings from a JSON file\n" "\n" " Returns an empty list if the file doesn't exist (for playback mode,\n" " this means no recordings are available).\n" ). -spec load_recordings(binary()) -> {ok, list(dream_http_client@recording:recording())} | {error, binary()}. load_recordings(Directory) -> File_path = build_file_path(Directory), case simplifile:read(File_path) of {ok, Content} -> case dream_http_client@recording:decode_recording_file(Content) of {ok, File} -> {ok, erlang:element(3, File)}; {error, Reason} -> {error, <<"Failed to decode recording file: "/utf8, Reason/binary>>} end; {error, enoent} -> {ok, []}; {error, Read_error} -> {error, <<"Failed to read recording file: "/utf8, (gleam@string:inspect(Read_error))/binary>>} end. -file("src/dream_http_client/storage.gleam", 40). ?DOC( " Save recordings to a JSON file\n" "\n" " Creates the directory if it doesn't exist and writes all recordings\n" " to a single JSON file.\n" ). -spec save_recordings(binary(), list(dream_http_client@recording:recording())) -> {ok, nil} | {error, binary()}. save_recordings(Directory, Recordings) -> File_path = build_file_path(Directory), _pipe = case simplifile:create_directory_all(Directory) of {ok, nil} -> {ok, nil}; {error, eexist} -> {ok, nil}; {error, Directory_error} -> {error, <<<<<<"Failed to create directory "/utf8, Directory/binary>>/binary, ": "/utf8>>/binary, (gleam@string:inspect(Directory_error))/binary>>} end, gleam@result:'try'( _pipe, fun(_) -> Recording_file = {recording_file, <<"1.0"/utf8>>, Recordings}, Json_value = dream_http_client@recording:encode_recording_file( Recording_file ), Json_string = gleam@json:to_string(Json_value), case simplifile:write(File_path, Json_string) of {ok, nil} -> {ok, nil}; {error, Write_error} -> {error, <<<<<<"Failed to write recording file "/utf8, File_path/binary>>/binary, ": "/utf8>>/binary, (gleam@string:inspect(Write_error))/binary>>} end end ).