package octez-libs
A package that contains multiple base libraries used by the Octez suite
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/octez-libs.tree-encoding/decoding.ml.html
Source file decoding.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2022 TriliTech <contact@trili.tech> *) (* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) type key = string list exception Key_not_found of key exception No_tag_matched_on_decoding exception Decode_error of {key : key; error : Data_encoding.Binary.read_error} (** Given the tail key, construct a full key. *) type prefix_key = key -> key (** [of_key key] constructs a [prefix_key] where [key] is the prefix. *) let of_key key tail = let rec go = function [] -> tail | x :: xs -> x :: go xs in go key (** [append_key prefix key] append [key] to [prefix] in order to create a new [prefix_key]. *) let append_key prefix key tail = prefix (of_key key tail) type 'a t = { decode : 'tree. 'tree Tree.backend -> 'tree -> prefix_key -> 'a Lwt.t; } [@@unboxed] type ('tag, 'a) case = | Case : { tag : 'tag; extract : 'b -> 'a Lwt.t; decode : 'b t; } -> ('tag, 'a) case let delayed f = {decode = (fun backend -> (f ()).decode backend)} let of_lwt lwt = {decode = (fun _backend _tree _prefix -> lwt)} let map f {decode} = {decode = (fun backend tree prefix -> Lwt.map f (decode backend tree prefix))} let map_lwt f {decode} = { decode = (fun backend tree prefix -> Lwt.bind (decode backend tree prefix) f); } module Syntax = struct let return value = {decode = (fun _backend _tree _prefix -> Lwt.return value)} let bind {decode} f = { decode = (fun backend tree prefix -> Lwt.bind (decode backend tree prefix) (fun x -> (f x).decode backend tree prefix)); } let both lhs rhs = { decode = (fun backend tree prefix -> Lwt.both (lhs.decode backend tree prefix) (rhs.decode backend tree prefix)); } let ( let+ ) m f = map f m let ( and+ ) = both let ( let* ) = bind let ( and* ) = ( and+ ) end let run backend {decode} tree = decode backend tree Fun.id let raw key = { decode = (fun backend tree prefix -> let open Lwt_syntax in let key = prefix key in let+ value = Tree.find backend tree key in match value with | Some value -> value | None -> raise (Key_not_found key)); } let value_option key decoder = { decode = (fun backend tree prefix -> let open Lwt_syntax in let key = prefix key in let* value = Tree.find backend tree key in match value with | Some value -> ( match Data_encoding.Binary.of_bytes decoder value with | Ok value -> return_some value | Error error -> raise (Decode_error {key; error})) | None -> return_none); } let value ?default key decoder = { decode = (fun backend tree prefix -> let open Lwt_syntax in let* value = (value_option key decoder).decode backend tree prefix in match (value, default) with | Some value, _ -> return value | None, Some default -> return default | None, None -> raise (Key_not_found (prefix key))); } let subtree backend tree prefix = let open Lwt_syntax in let tmp_directory = "tmp" in let* subtree = Tree.find_tree backend tree (prefix []) in let+ subtree = match subtree with | Some subtree -> return subtree | None -> ( let* tree = Tree.add backend tree (prefix [tmp_directory]) (Bytes.of_string "") in let* subtree = Tree.find_tree backend tree (prefix []) in match subtree with | Some subtree -> Tree.remove backend subtree [tmp_directory] | None -> (* This case is impossible, because we have added something in the tree to avoid it. *) assert false) in subtree let scope key {decode} = { decode = (fun backend tree prefix -> decode backend tree (append_key prefix key)); } let lazy_mapping to_key field_enc = { decode = (fun backend input_tree input_prefix -> let open Lwt_syntax in let produce_value index = (scope (to_key index) field_enc).decode backend input_tree input_prefix in let+ tree = subtree backend input_tree input_prefix in (Some (Tree.Wrapped_tree (tree, backend)), produce_value)); } let case_lwt tag decode extract = Case {tag; decode; extract} let case tag decode extract = case_lwt tag decode (fun x -> Lwt.return @@ extract x) let tagged_union ?default decode_tag cases = { decode = (fun backend input_tree prefix -> let open Lwt_syntax in Lwt.try_bind (fun () -> (scope ["tag"] decode_tag).decode backend input_tree prefix) (fun target_tag -> (* Search through the cases to find a matching branch. *) let candidate = List.find_map (fun (Case {tag; decode; extract}) -> if tag = target_tag then Some ((map_lwt extract (scope ["value"] decode)).decode backend input_tree prefix) else None) cases in match candidate with | Some case -> case | None -> raise No_tag_matched_on_decoding) (function | Key_not_found _ as exn -> ( match default with | Some default -> return (default ()) | None -> Lwt.reraise exn) | exn -> Lwt.reraise exn)); } let wrapped_tree = { decode = (fun backend tree prefix -> let open Lwt.Syntax in let+ tree = subtree backend tree prefix in Tree.Wrapped_tree (tree, backend)); }
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>