use marshal as suggested by @reynir

This commit is contained in:
Hannes Mehnert 2024-11-22 12:27:44 +01:00
parent 1e5d412d2d
commit 4ae84869bb

View file

@ -723,27 +723,18 @@ stamp: %S
mutable index : string ; mutable index : string ;
} }
let to_string { commit_id ; modified ; repo ; index } = let marshal t =
String.concat ";" [ commit_id ; modified ; repo ; index ] let version = char_of_int 1 in
String.make 1 version ^ Marshal.to_string t []
let of_string str = let unmarshal s =
match String.index_opt str ';' with let version = int_of_char s.[0] in
| None -> Error (`Msg "no separator found") match version with
| Some commit_end -> | 1 -> Ok (Marshal.from_string s 1)
match String.index_from_opt str (succ commit_end) ';' with | _ -> Error ("Unsupported version " ^ string_of_int version)
| None -> Error (`Msg "no separator found")
| Some modified_end ->
match String.index_from_opt str (succ modified_end) ';' with
| None -> Error (`Msg "no separator found")
| Some repo_end ->
let last_chunk = String.length str - succ repo_end in
Ok { commit_id = String.sub str 0 commit_end ;
modified = String.sub str (succ commit_end) modified_end ;
repo = String.sub str (succ modified_end) repo_end ;
index = String.sub str (succ repo_end) last_chunk }
let dump_index index_dump t = let dump_index index_dump t =
let data = to_string t in let data = marshal t in
Cache.write index_dump data >|= function Cache.write index_dump data >|= function
| Ok () -> | Ok () ->
Logs.info (fun m -> m "dumped index %d bytes" (String.length data)) Logs.info (fun m -> m "dumped index %d bytes" (String.length data))
@ -757,8 +748,8 @@ stamp: %S
Logs.warn (fun m -> m "failed to read index state: %a" Cache.pp_error e); Logs.warn (fun m -> m "failed to read index state: %a" Cache.pp_error e);
Error () Error ()
| Ok Some data -> | Ok Some data ->
match of_string data with match unmarshal data with
| Error `Msg msg -> | Error msg ->
Logs.warn (fun m -> m "failed to decode index: %s" msg); Logs.warn (fun m -> m "failed to decode index: %s" msg);
Error () Error ()
| Ok t -> Ok t | Ok t -> Ok t