diff options
| author | Marcos Magueta <maguetamarcos@gmail.com> | 2026-10-31 03:15:28 -0300 |
|---|---|---|
| committer | Marcos Magueta <maguetamarcos@gmail.com> | 2026-10-31 03:15:28 -0300 |
| commit | 55174a28581c33c4b6d91066086a2ee7b5c663ea (patch) | |
| tree | fedb78bf66dd114eedc358ca385cf5f01a65ae0e /lib/concepts/Encoding.ml | |
| parent | a8cd08bfe473f01f245a4808e63809ed1cddc0e7 (diff) | |
| download | RNT-55174a28581c33c4b6d91066086a2ee7b5c663ea.tar.gz RNT-55174a28581c33c4b6d91066086a2ee7b5c663ea.zip | |
bencode helpers between dicts and string maps
Diffstat (limited to '')
| -rw-r--r-- | lib/concepts/Encoding.ml | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/concepts/Encoding.ml b/lib/concepts/Encoding.ml index 50eb6e6..34ca3a0 100644 --- a/lib/concepts/Encoding.ml +++ b/lib/concepts/Encoding.ml @@ -113,6 +113,14 @@ module Bencode = struct let as_int = function Int n -> Ok n | _ -> Error (Error.type_mismatch ~expected:"integer") let as_list = function List l -> Ok l | _ -> Error (Error.type_mismatch ~expected:"list") let as_dict = function Dict d -> Ok d | _ -> Error (Error.type_mismatch ~expected:"dict") + let of_map f map = Dict (List.map (fun (k, v) -> k, f v) (BatMap.String.bindings map)) + + let to_map f data = + let open Utilities.Result in + let* kvs = as_dict data in + List.map (fun (k, v) -> f v |> Result.map (fun v -> k, v)) kvs + |> Utilities.List.sequence + |> Result.map BatMap.String.of_list let of_bytes (bytes : bytes) : (t, Condition.condition) result = let open Utilities.Result in |
