text
stringlengths
0
601k
let warn_if_unable_to_pay_account_creation_fee ~ get_account ( ~ constraint_constants : Genesis_constants . Constraint_constants . t ) t ~ logger user_command_payload = let receiver_pk = Signed_command_payload . receiver_pk user_command_payload in let token = Signed_command_payload . token user_command_payload in let r...
let to_user_command ( ? nonce_map = Account_id . Map . empty ) empty ~ get_current_nonce ~ get_account ~ constraint_constants ~ logger ( client_input : t ) t = Deferred . map ~ f : ( Result . map_error ~ f ( : fun str -> Error . createf " Error creating user command : % s Error : % s " ( Yojson . Safe . to_string ( to_...
let to_user_commands ( ? nonce_map = Account_id . Map . empty ) empty ~ get_current_nonce ~ get_account ~ constraint_constants ~ logger uc_inputs : Signed_command . t list Deferred . Or_error . t = let open Deferred . Or_error . Let_syntax in let % map user_commands , _ = Deferred . Or_error . List . fold ~ init ( [ ] ...
type t = { author_name : string option ; email : string option ; github_username : string option ; create_switch : bool option }
let of_dec ( dec : Dec_user_config . t ) t = { author_name = dec . author_name ; email = dec . email ; github_username = dec . github_username ; create_switch = dec . create_switch }
let path_of_opt value = value |> Option . map Result . ok |> Option . value ~ default : ( Config . spin_config_dir |> Result . map ( fun p -> Filename . concat p " default ) ) "
let read ? path ( ) = let decode_if_exists path = if Sys . file_exists path then Decoder . decode_sexps_file path Dec_user_config . decode |> Result . map of_dec |> Result . map Option . some |> Result . map_error ( Spin_error . of_decoder_error ~ file : path ) path else Ok None in Result . bind ( path_of_opt path ) pa...
let save ? path ( t : t ) t = let open Result . Syntax in let + path = path_of_opt path in let ( ) = Sys . mkdir_p ( Filename . dirname path ) path in Encoder . encode_file path Dec_user_config . { author_name = t . author_name ; email = t . email ; github_username = t . github_username ; create_switch = t . create_swi...
let validate_strip s = match String . trim s with " " -> Error " Enter a value . " | s -> Ok s
let prompt ? default : d ( ) = let author_name = Inquire . input " Your name " ? default ( : Option . bind d ( fun d -> d . author_name ) author_name ) author_name ~ validate : validate_strip in let email = Inquire . input " Your email " ? default ( : Option . bind d ( fun d -> d . email ) email ) email ~ validate : va...
let to_context t = let context = Hashtbl . create 256 in Option . iter ( fun v -> Hashtbl . add context " author_name " v |> ignore ) ignore t . author_name ; Option . iter ( fun v -> Hashtbl . add context " email " v |> ignore ) ignore t . email ; Option . iter ( fun v -> Hashtbl . add context " github_username " v |>...
module Search = struct module Query = Empty . Query module Request = struct type t = { search_term : string ; limited : int option } [ @@ deriving accessor ] let encoding = let to_tuple t = t . search_term , t . limited in let of_tuple v = let search_term , limited = v in { search_term ; limited } in let with_tuple = o...
type links = | LINKS of ( ( int * int ) * int ) list | WHATEVER | SOME | TYPE of string * string
type cc_port = { port_links : links ; port_states : string list option ; }
type site = | Port of cc_port | Counter of int
type cc_site = { site_name : string ; site_type : site }
type cc_node = { node_type : string ; node_id : int option ; node_sites : cc_site array ; }
type connected_component = cc_node option array array
module LinkSetMap = SetMap . Make ( struct type t = ( int * int ) * int let print = Pp . pair ( Pp . pair Format . pp_print_int Format . pp_print_int ) Format . pp_print_int let compare ( x , y ) ( x ' , y ' ) = let c = Mods . int_pair_compare x x ' in if c = 0 then Mods . int_compare y y ' else c end )
let print_link ( dangling , free_id ) p f = function | WHATEVER -> Format . pp_print_string f " [ ] " # | SOME -> Format . pp_print_string f " [ _ ] " | TYPE ( si , ty ) -> Format . fprintf f " [ % s . % s ] " si ty | LINKS [ ] -> Format . pp_print_string f " [ . ] " | LINKS l -> let myself = ref ( LinkSetMap . Map . f...
let print_port with_link node p id f = Format . fprintf f " % a % a " ( fun f -> function | None -> Format . pp_print_string f " { } " # | Some [ ] -> ( ) | Some l -> Format . fprintf f " { % a } " ( Pp . list Pp . space ( fun f i -> Format . fprintf f " % s " i ) ) l ) p . port_states ( match with_link with | Some pac...
let print_intf with_link node = Pp . array Pp . space ( fun id f si -> let ( ) = Format . fprintf f " % s " si . site_name in ( match si . site_type with | Port p -> print_port with_link node p id f | Counter i -> Format . fprintf f " { =% i } " i ) )
let print_agent with_id link node f = function | None -> Format . pp_print_string f " . " | Some ag -> Format . fprintf f " % a % s ( [ @< h >% a ] ) " @ ( Pp . option ~ with_space : false ( fun f i -> Format . fprintf f " x % i " : i ) ) ( if with_id then ag . node_id else None ) ag . node_type ( print_intf link node ...
let print_cc f mix = let link = Some ( ref ( LinkSetMap . Map . empty ) , ref 0 ) in Pp . array ( fun f -> Format . fprintf f " \\@ " ) ( fun al -> Pp . array Pp . comma ( fun ar -> print_agent true link ( al , ar ) ) ) f mix
let get_color = let store = Hashtbl . create 10 in fun i -> try Hashtbl . find store i with Not_found -> let v = Format . sprintf " #% x % x % x " ( Random . int 255 ) ( Random . int 255 ) ( Random . int 255 ) in let ( ) = Hashtbl . add store i v in v
let print_dot_cc nb_cc f mix = Pp . array Pp . empty ( fun il -> Pp . array Pp . empty ( fun ir f -> function | None -> ( ) | Some ag -> Format . fprintf f " node % d_ % d_ % d [ label = " [ \@< h >% a ] " , @\ color = " \% s " , \ style = filled ] ; , " @ nb_cc il ir ( print_agent false None ( il , ir ) ) ( Some ag ) ...
let write_cc_port ob p = let ( ) = Bi_outbuf . add_char ob ' { ' in let ( ) = JsonUtil . write_field " port_links " ( fun ob -> function | WHATEVER -> Yojson . Basic . write_null ob ( ) | SOME -> Yojson . Basic . write_bool ob true | TYPE ( si , ty ) -> let ( ) = Bi_outbuf . add_string ob " { " \ site_name " " " \:\ in...
let write_site ob f = let ( ) = Bi_outbuf . add_char ob ' [ ' in let ( ) = match f . site_type with | Counter i -> let ( ) = Yojson . Basic . write_string ob " counter " in let ( ) = Bi_outbuf . add_char ob ' , ' in Yojson . Basic . write_int ob i | Port p -> let ( ) = Yojson . Basic . write_string ob " port " in let (...
let write_cc_site ob f = let ( ) = Bi_outbuf . add_char ob ' { ' in let ( ) = JsonUtil . write_field " site_name " Yojson . Basic . write_string ob f . site_name in let ( ) = JsonUtil . write_comma ob in let ( ) = JsonUtil . write_field " site_type " write_site ob f in Bi_outbuf . add_char ob ' } '
let links_of_yojson = function | ` Null -> WHATEVER | ` Bool b -> let ( ) = assert b in SOME | ` Assoc [ " site_name " , ` String si ; " agent_type " , ` String ty ] | ` Assoc [ " agent_type " , ` String ty ; " site_name " , ` String si ] -> TYPE ( si , ty ) | ` List _ as x -> let error_msg = None in LINKS ( JsonUtil ....
let read_cc_port p lb = let ( port_links , port_states ) = Yojson . Basic . read_fields ( fun ( s , i ) key p lb -> if key = " port_links " then ( links_of_yojson ( Yojson . Basic . read_json p lb ) , i ) else let ( ) = assert ( key = " port_states " ) in ( s , JsonUtil . read_option ( Yojson . Basic . read_list Yojson...
let read_site p lb = let ( ) = Yojson . Basic . read_lbr p lb in let key = JsonUtil . read_between_spaces Yojson . Basic . read_string p lb in let ( ) = Yojson . Basic . read_comma p lb in let out = JsonUtil . read_between_spaces ( fun p lb -> if key = " counter " then Counter ( Yojson . Basic . read_int p lb ) else le...
let read_cc_site p lb = let ( site_name , site_type ) = Yojson . Basic . read_fields ( fun ( n , s ) key p lb -> if key = " site_name " then ( Yojson . Basic . read_string p lb , s ) else let ( ) = assert ( key = " site_type " ) in ( n , read_site p lb ) ) ( " " , Counter ( - 1 ) ) p lb in { site_name ; site_type }
let write_cc_node ob x = JsonUtil . write_option ( fun ob f -> let ( ) = Bi_outbuf . add_char ob ' { ' in let ( ) = JsonUtil . write_field " node_type " Yojson . Basic . write_string ob f . node_type in let ( ) = JsonUtil . write_comma ob in let ( ) = match f . node_id with | None -> ( ) | Some node_id -> let ( ) = Jso...
let read_cc_node p lb = JsonUtil . read_option ( fun p lb -> let ( node_id , node_type , node_sites ) = Yojson . Basic . read_fields ( fun ( id , n , s ) key p lb -> if key = " node_id " then ( Some ( Yojson . Basic . read_int p lb ) , n , s ) else if key = " node_type " then ( id , Yojson . Basic . read_string p lb , ...
let write_connected_component ob f = JsonUtil . write_array ( JsonUtil . write_array write_cc_node ) ob f
let read_connected_component ob f = Yojson . Basic . read_array ( Yojson . Basic . read_array read_cc_node ) ob f
let string_of_connected_component ( ? len = 1024 ) x = let ob = Bi_outbuf . create len in let ( ) = write_connected_component ob x in Bi_outbuf . contents ob
let connected_component_of_string s = read_connected_component ( Yojson . Safe . init_lexer ( ) ) ( Lexing . from_string s )
module Style = struct type t = | Loc | Error | Warning | Kwd | Id | Prompt | Details | Ok | Debug | Success | Ansi_styles of Ansi_color . Style . t list end
module Print_config = struct type t = Style . t -> Ansi_color . Style . t list open Ansi_color . Style let default : Style . t -> _ = function | Loc -> [ bold ] | Error -> [ bold ; fg_red ] | Warning -> [ bold ; fg_magenta ] | Kwd -> [ bold ; fg_blue ] | Id -> [ bold ; fg_yellow ] | Prompt -> [ bold ; fg_green ] | Deta...
type t = { loc : Loc0 . t option ; paragraphs : Style . t Pp . t list ; hints : Style . t Pp . t list }
let make ? loc ? prefix ( ? hints = [ ] ) paragraphs = let paragraphs = match ( prefix , paragraphs ) with | None , l -> l | Some p , [ ] -> [ p ] | Some p , x :: l -> Pp . concat ~ sep : Pp . space [ p ; x ] :: l in { loc ; hints ; paragraphs }
let pp { loc ; paragraphs ; hints } = let open Pp . O in let paragraphs = match hints with | [ ] -> paragraphs | _ -> List . append paragraphs ( List . map hints ~ f ( : fun hint -> Pp . verbatim " Hint " : ++ Pp . space ++ hint ) ) in let paragraphs = List . map paragraphs ~ f : Pp . box in let paragraphs = match loc ...
let print ( ? config = Print_config . default ) t = Ansi_color . print ( Pp . map_tags ( pp t ) ~ f : config )
let prerr ( ? config = Print_config . default ) t = Ansi_color . prerr ( Pp . map_tags ( pp t ) ~ f : config )
let levenshtein_distance s t = let m = String . length s and n = String . length t in let d = Array . make_matrix ~ dimx ( : m + 1 ) ~ dimy ( : n + 1 ) 0 in for i = 0 to m do d . ( i ) . ( 0 ) <- i done ; for j = 0 to n do d . ( 0 ) . ( j ) <- j done ; for j = 1 to n do for i = 1 to m do if s . [ i - 1 ] = t . [ j - 1 ...
let did_you_mean s ~ candidates = let candidates = List . filter candidates ~ f ( : fun candidate -> levenshtein_distance s candidate < 3 ) in match candidates with | [ ] -> [ ] | l -> [ Pp . textf " did you mean % s " ? ( String . enumerate_or l ) ]
let to_string t = Format . asprintf " % a " Pp . render_ignore_tags ( pp { t with loc = None } ) |> String . drop_prefix ~ prefix " : Error : " |> Option . value_exn |> String . trim
let json = ` O [ ( " name " , ` String " OCaml ) " ; ( " qualities " , ` A [ ` O [ ( " name " , ` String " awesome ) " ] ; ` O [ ( " name " , ` String " simple ) " ] ; ` O [ ( " name " , ` String " fun ) " ] ] ) ]
let section heading = print_newline ( ) ; print_string " # " ; print_endline heading
let subsection heading = print_newline ( ) ; print_string " ## " ; print_endline heading
let test tmpl = subsection " parsed " ; print_endline " " ; --- print_endline ( Mustache . to_string tmpl ) tmpl ; print_endline " " ; --- subsection " rendered " ; print_endline " " ; --- print_endline ( Mustache . render tmpl json ) json ; print_endline " " ; --- ( )
let ( ) = section " Parsed template "
let parsed_template = Mustache . of_string " Hello { { name } name } name \ nMustache is :\ n { n { n # qualities } qualities } qualities \ n * { { name } name } name \ n { n { n / qualities } qualities } qualities \ n "
let ( ) = test parsed_template
let ( ) = section " Programmed template "
let programmed_template = let open Mustache in concat [ raw " Hello " ; escaped " name " ; raw " \ n " ; raw " Mustache is " : ; raw " \ n " ; section " qualities " @@ concat [ raw " * " ; escaped " name " ; raw " \ n " ] ]
let ( ) = test programmed_template
let ( ) = section " Output comparison "
let ( ) = let parsed_output = Mustache . render parsed_template json in let programmed_output = Mustache . render programmed_template json in if String . equal parsed_output programmed_output then print_endline " Outputs match as expected . " else print_endline " Outputs DO NOT match , this is suspcious . "
module type Sig = sig val register_migration : unit -> unit val register_cleaner : unit -> unit val lifecycles : Sihl . Container . lifecycle list val search : ? ctx ( : string * string ) list -> [ ` Desc | ` Asc ] -> string option -> limit : int -> offset : int -> ( Model . t list * int ) Lwt . t val get : ? ctx ( : s...
let status = let encode m = m |> Model . status_to_string |> Result . ok in let decode = Model . status_of_string in Caqti_type . ( custom ~ encode ~ decode string ) ; ;
let user = let open Sihl . Contract . User in let encode m = Ok ( m . id , ( m . email , ( m . username , ( m . name , ( m . given_name , ( m . password , ( m . status , ( m . admin , ( m . confirmed , ( m . created_at , m . updated_at ) ) ) ) ) ) ) ) ) ) in let decode ( id , ( email , ( username , ( name , ( given_nam...
module MakeMariaDb ( MigrationService : Sihl . Contract . Migration . Sig ) : Sig = struct let lifecycles = [ Database . lifecycle ; MigrationService . lifecycle ] module Migration = struct let fix_collation = Migration . create_step ~ label " : fix collation " { sql | SET collation_server = ' utf8mb4_unicode_ci ' | sq...
module MakePostgreSql ( MigrationService : Sihl . Contract . Migration . Sig ) : Sig = struct let lifecycles = [ Database . lifecycle ; MigrationService . lifecycle ] module Migration = struct let create_users_table = Migration . create_step ~ label " : create users table " { sql | CREATE TABLE IF NOT EXISTS user_users...
let test_current_user = let current_user = Lwt_main . run begin Github . ( Monad . ( run ( User . current_info ~ token ( ) >|= Response . value ) ) ) end in assert ( current_user . Github_t . user_info_ty = ` User ) ; Format . printf " Check current user : OK . . " @
let test_current_user_first_org = let org = Lwt_main . run begin Github . ( Monad . ( run begin let orgs = Organization . current_user_orgs ~ token ( ) in Stream . next orgs >>= function | None -> Printf . eprintf " No organizations for the current user . \ n " ; exit 1 | Some ( first_org , _ ) -> return first_org end ...
let test_ocaml_organization = let ocaml_user = Lwt_main . run begin Github . ( Monad . ( run begin User . info ~ token ~ user " : ocaml " ( ) >|= Response . value end ) ) end in assert ( ocaml_user . Github_t . user_info_ty = ` Org ) ; Format . printf " Check OCaml org : OK . . " @
let test_ocaml_repository = let opam_repository = Lwt_main . run begin Github . ( Monad . ( run begin Repo . info ~ token ~ user " : ocaml " ~ repo " : opam " ( ) >|= Response . value end ) ) end in assert Github_t . ( opam_repository . repository_owner . user_ty = ` Org ) ; Format . printf " Check OCaml repo : OK . . ...
let mem u s = ISet . mem ( UChar . uint_code u ) u s
let add u s = ISet . add ( UChar . uint_code u ) u s
let add_range u1 u2 s = ISet . add_range ( UChar . uint_code u1 ) u1 ( UChar . uint_code u2 ) u2 s
let singleton u = ISet . singleton ( UChar . uint_code u ) u
let remove u s = ISet . remove ( UChar . uint_code u ) u s
let remove_range u1 u2 s = ISet . remove_range ( UChar . uint_code u1 ) u1 ( UChar . uint_code u2 ) u2 s
let from u s = ISet . from ( UChar . uint_code u ) u s
let after u s = ISet . after ( UChar . uint_code u ) u s
let until u s = ISet . until ( UChar . uint_code u ) u s
let before u s = ISet . before ( UChar . uint_code u ) u s
let iter f s = ISet . iter ( fun n -> f ( UChar . chr_of_uint n ) n ) n s
let iter_range f s = let f ' n1 n2 = f ( UChar . chr_of_uint n1 ) n1 ( UChar . chr_of_uint n2 ) n2 in ISet . iter_range f ' s
let fold f s a = let f ' n a = f ( UChar . chr_of_uint n ) n a in ISet . fold f ' s a
let fold_range f s a = let f ' n1 n2 a = f ( UChar . chr_of_uint n1 ) n1 ( UChar . chr_of_uint n2 ) n2 a in ISet . fold_range f ' s a
let for_all p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . for_all p ' s
let exists p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . exists p ' s
let filter p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . filter p ' s
let partition p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . partition p ' s
let elements s = List . map UChar . chr_of_uint ( ISet . elements s ) s
let ranges s = let f ( n1 , n2 ) n2 = ( UChar . chr_of_uint n1 , UChar . chr_of_uint n2 ) n2 in List . map f ( ISet . ranges s ) s
let min_elt s = UChar . chr_of_uint ( ISet . min_elt s ) s
let max_elt s = UChar . chr_of_uint ( ISet . max_elt s ) s
let choose s = UChar . chr_of_uint ( ISet . choose s ) s
let uset_of_iset s = s
let iset_of_uset s = s
let get_files_in_directory dir = let len = String . length dir in let dir = if len > 0 && Sys . os_type = " Win32 " && ( dir . [ len - 1 ] = ' ' / || dir . [ len - 1 ] = ' ' ) \\ then String . sub dir ~ pos : 0 ~ len ( : len - 1 ) else dir in match try Some ( opendir dir ) with Unix_error _ -> None with None -> [ ] | S...
let is_directory name = try ( stat name ) . st_kind = S_DIR with _ -> false
let concat dir name = let len = String . length dir in if len = 0 then name else if dir . [ len - 1 ] = ' ' / then dir ^ name else dir ^ " " / ^ name
let get_directories_in_files ~ path = List . filter ~ f ( : fun x -> is_directory ( concat path x ) )
let subshell ~ cmd = let rc = open_process_in cmd in let rec it l = match try Some ( input_line rc ) with _ -> None with Some x -> it ( x :: l ) | None -> List . rev l in let answer = it [ ] in ignore ( close_process_in rc ) ; answer