Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mirage/mirage
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d8b83923753d
Choose a base ref
...
head repository: mirage/mirage
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3828a22e2f44
Choose a head ref
  • 3 commits
  • 5 files changed
  • 2 contributors

Commits on Jan 2, 2019

  1. Copy the full SHA
    957fa13 View commit details
  2. Copy the full SHA
    3b018c6 View commit details

Commits on Jan 4, 2019

  1. Merge pull request #956 from hannesm/not-using-sexp

    Use pp and of_string from Ipaddr modules instead of s-expressions
    avsm authored Jan 4, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3828a22 View commit details
Showing with 44 additions and 38 deletions.
  1. +27 −23 lib/mirage_key.ml
  2. +11 −10 lib_runtime/mirage_runtime.ml
  3. +4 −3 lib_runtime/mirage_runtime.mli
  4. +1 −1 mirage-runtime.opam
  5. +1 −1 mirage.opam
50 changes: 27 additions & 23 deletions lib/mirage_key.ml
Original file line number Diff line number Diff line change
@@ -23,50 +23,54 @@ open Astring
module Arg = struct
include Key.Arg

module type S = sig
include Mirage_runtime.Arg.S
val sexp_of_t : t -> Sexplib.Type.t
end
let from_run s = "Mirage_runtime.Arg." ^ s

(* Could be improved with metaocaml and/or some reflection.
[description] and [m] should not need to be provided.
*)
let of_module
(type t) runtime_conv m (module M: S with type t=t) =
let conv = Mirage_runtime.Arg.of_module (module M) in
let serialize ppf x =
Fmt.pf ppf "(%s.t_of_sexp (Sexplib.Sexp.of_string %S))"
m (Sexplib.Sexp.to_string @@ M.sexp_of_t x)
let make d m of_string to_string =
let parser s = match of_string s with
| Error (`Msg m) -> `Error ("Can't parse ip address: "^s^": "^m)
| Ok ip -> `Ok ip
and serialize ppf t =
Fmt.pf ppf "(Ipaddr.%s.of_string_exn %S)" m (to_string t)
and pp ppf t =
Fmt.string ppf (to_string t)
in
Functoria_key.Arg.conv ~conv ~serialize ~runtime_conv
Functoria_key.Arg.conv
~conv:(parser, pp)
~serialize
~runtime_conv:(from_run d)

let from_run s = "Mirage_runtime.Arg." ^ s
let builtin d mn m = of_module (from_run d) mn m
module type S = sig
type t
val of_string : string -> (t, [ `Msg of string ]) result
val to_string : t -> string
end

let ipv4_address = builtin "ipv4_address" "Ipaddr.V4" (module Ipaddr.V4)
let ipv6 = builtin "ipv6" "Ipaddr.V6" (module Ipaddr.V6)
let ipv6_prefix =
builtin "ipv6_prefix" "Ipaddr.V6.Prefix" (module Ipaddr.V6.Prefix)
let of_module (type t) d m (module M:S with type t = t) =
make d m M.of_string M.to_string

let ipv4_address = of_module "ipv4_address" "V4" (module Ipaddr.V4)
let ipv4 =
let serialize fmt (prefix, ip) =
Format.fprintf fmt "(Ipaddr.V4.Prefix.of_address_string_exn \"%s\")"
Format.fprintf fmt "(Ipaddr.V4.Prefix.of_address_string_exn %S)"
@@ Ipaddr.V4.Prefix.to_address_string prefix ip
in
let print fmt (prefix, ip) =
Format.fprintf fmt "%s" @@ Ipaddr.V4.Prefix.to_address_string prefix ip
in
let parse str =
match Ipaddr.V4.Prefix.of_address_string str with
| None -> `Error (str ^ " is not a valid IPv4 address and netmask")
| Some (network, ip) -> `Ok (network, ip)
| Error (`Msg m) -> `Error (str ^ " is not a valid IPv4 address and netmask: "^m)
| Ok n -> `Ok n
in
let runtime_conv = "Mirage_runtime.Arg.ipv4"
in
Functoria_key.Arg.conv
~conv:(parse, print)
~serialize
~runtime_conv

let ipv6 = of_module "ipv6" "V6" (module Ipaddr.V6)
let ipv6_prefix = of_module "ipv6_prefix" "V6.Prefix" (module Ipaddr.V6.Prefix)
end

(** {2 Documentation helper} *)
21 changes: 11 additions & 10 deletions lib_runtime/mirage_runtime.ml
Original file line number Diff line number Diff line change
@@ -38,33 +38,34 @@ module Arg = struct

include Functoria_runtime.Arg

let make of_string pp: _ Cmdliner.Arg.converter =
let make of_string to_string :_ Cmdliner.Arg.converter =
let parser s = match of_string s with
| Some ip -> `Ok ip
| None -> `Error ("Can't parse ip address: "^s)
| Error (`Msg m) -> `Error ("Can't parse ip address: "^s^": "^m)
| Ok ip -> `Ok ip
and pp ppf ip =
Fmt.string ppf (to_string ip)
in
parser, pp

module type S = sig
type t
val of_string : string -> t option
val pp_hum : Format.formatter -> t -> unit
val of_string : string -> (t, [ `Msg of string ]) result
val to_string : t -> string
end

let of_module (type t) (module M:S with type t = t) =
make M.of_string M.pp_hum
make M.of_string M.to_string

let ip = of_module (module Ipaddr)
let ipv4_address = of_module (module Ipaddr.V4)
let ipv4 =
let serialize fmt (prefix, ip) =
Format.fprintf fmt "(Ipaddr.V4.Prefix.of_address_string_exn \"%s\")"
@@ Ipaddr.V4.Prefix.to_address_string prefix ip
Format.fprintf fmt "%S" @@ Ipaddr.V4.Prefix.to_address_string prefix ip
in
let parse str =
match Ipaddr.V4.Prefix.of_address_string str with
| None -> `Error (str ^ " is not a valid IPv4 address and netmask")
| Some n -> `Ok n
| Error (`Msg m) -> `Error (str ^ " is not a valid IPv4 address and netmask: " ^ m)
| Ok n -> `Ok n
in
parse, serialize

7 changes: 4 additions & 3 deletions lib_runtime/mirage_runtime.mli
Original file line number Diff line number Diff line change
@@ -33,14 +33,15 @@ module Arg: sig

include module type of Functoria_runtime.Arg

val make: (string -> 'a option) -> 'a Fmt.t -> 'a Cmdliner.Arg.converter
val make: (string -> ('a, [ `Msg of string ]) result) -> ('a -> string) ->
'a Cmdliner.Arg.converter
(** [make of_string pp] is the command-line argument converter using
on [of_string] and [pp]. *)

module type S = sig
type t
val of_string: string -> t option
val pp_hum: Format.formatter -> t -> unit
val of_string: string -> (t, [ `Msg of string ]) result
val to_string: t -> string
end
(** [S] is the signature used by {!of_module} to create a
command-line argument converter. *)
2 changes: 1 addition & 1 deletion mirage-runtime.opam
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ build: [
depends: [
"ocaml" {>= "4.04.2"}
"dune" {build & >= "1.1.0"}
"ipaddr" {>= "2.6.0"}
"ipaddr" {>= "3.0.0"}
"functoria-runtime" {>= "2.2.2"}
"fmt"
"logs"
2 changes: 1 addition & 1 deletion mirage.opam
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ build: [
depends: [
"ocaml" {>= "4.04.2"}
"dune" {build & >= "1.1.0"}
"ipaddr" {>= "2.6.0"}
"ipaddr" {>= "3.0.0"}
"functoria" {>= "2.2.3"}
"bos"
"astring"