-module(thrifty@fuzz_persistence). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/thrifty/fuzz_persistence.gleam"). -export([persist_failure/5]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -file("src/thrifty/fuzz_persistence.gleam", 36). ?DOC( " Persist a failing fuzz payload and associated metadata.\n" "\n" " Inputs\n" " - `dir`: output directory. Created if it does not already exist.\n" " - `seed`: deterministic seed used when generating the payload.\n" " - `iter`: iteration index within the seed loop.\n" " - `data`: the mutated payload bytes to persist.\n" " - `reason`: the decode error that triggered persistence.\n" "\n" " Outputs\n" " - `Ok(Nil)` when both payload and metadata are written successfully.\n" " - `Error(String)` when directory creation or file writes fail.\n" "\n" " The metadata is stored as a text file containing simple `key=value` lines\n" " for quick inspection.\n" ). -spec persist_failure( binary(), integer(), integer(), bitstring(), thrifty@types:decode_error() ) -> {ok, nil} | {error, binary()}. persist_failure(Dir, Seed, Iter, Data, Reason) -> case thrifty@file_io:ensure_dir(Dir) of {error, E} -> {error, E}; {ok, _} -> Basename = <<<<<<"fuzz-failure-"/utf8, (erlang:integer_to_binary(Seed))/binary>>/binary, "-"/utf8>>/binary, (erlang:integer_to_binary(Iter))/binary>>, Bin_path = <<<<<>/binary, Basename/binary>>/binary, ".bin"/utf8>>, Meta_path = <<<<<>/binary, Basename/binary>>/binary, ".meta"/utf8>>, case thrifty@file_io:write_binary_to_path(Bin_path, Data) of {error, E@1} -> {error, E@1}; {ok, _} -> Meta = <<<<<<<<<<<<"seed="/utf8, (erlang:integer_to_binary(Seed))/binary>>/binary, "\niter="/utf8>>/binary, (erlang:integer_to_binary(Iter))/binary>>/binary, "\nreason="/utf8>>/binary, (thrifty@types:decode_error_to_string(Reason))/binary>>/binary, "\n"/utf8>>, Meta_bits = gleam_stdlib:identity(Meta), case thrifty@file_io:write_binary_to_path( Meta_path, Meta_bits ) of {error, E@2} -> {error, E@2}; {ok, _} -> {ok, nil} end end end.