Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,37 @@ jobs:
strategy:
fail-fast: false
matrix:
ocaml-version: [ '4.13.1', '4.10.0', '4.08.1' ]
ocaml-version: [ '5', '4', '4.10.0', '4.08.1' ]
operating-system: [macos-latest, ubuntu-latest, windows-latest]
exclude:
- operating-system: macos-latest
ocaml-version: ['4.10.0', '4.08.1']
steps:
- uses: actions/checkout@v2
- uses: avsm/setup-ocaml@v1
- uses: actions/checkout@v4
- uses: ocaml/setup-ocaml@v3
with:
ocaml-version: ${{ matrix.ocaml-version }}
- run: opam pin add -n .
- name: Packages
run: opam depext -yt cstruct cstruct-sexp cstruct-unix cstruct-lwt
ocaml-compiler: ${{ matrix.ocaml-version }}
- run: opam install . --deps-only --with-test
- name: Build
run: opam install -t cstruct cstruct-sexp cstruct-unix cstruct-lwt
run: opam exec -- dune build
- run: opam exec -- dune runtest
ppx:
name: PPX
runs-on: ${{ matrix.operating-system }}
strategy:
fail-fast: false
matrix:
ocaml-version: [ '4.13.1', '4.10.0', '4.08.1' ]
ocaml-version: [ '5', '4', '4.10.0', '4.08.1' ]
operating-system: [macos-latest, ubuntu-latest, windows-latest]
exclude:
- operating-system: macos-latest
ocaml-version: ['4.10.0', '4.08.1']
steps:
- uses: actions/checkout@v2
- uses: avsm/setup-ocaml@v1
- uses: actions/checkout@v4
- uses: ocaml/setup-ocaml@v3
with:
ocaml-version: ${{ matrix.ocaml-version }}
ocaml-compiler: ${{ matrix.ocaml-version }}
- run: opam pin add -n .
- name: Packages
run: opam depext -yt ppx_cstruct
- name: Build
run: opam install -t ppx_cstruct
async:
Expand All @@ -43,15 +46,16 @@ jobs:
strategy:
fail-fast: false
matrix:
ocaml-version: [ '4.13.1', '4.10.0', '4.08.1' ]
ocaml-version: [ '5', '4', '4.10.0', '4.08.1' ]
operating-system: [macos-latest, ubuntu-latest]
exclude:
- operating-system: macos-latest
ocaml-version: ['4.10.0', '4.08.1']
steps:
- uses: actions/checkout@v2
- uses: avsm/setup-ocaml@v1
- uses: actions/checkout@v4
- uses: ocaml/setup-ocaml@v3
with:
ocaml-version: ${{ matrix.ocaml-version }}
ocaml-compiler: ${{ matrix.ocaml-version }}
- run: opam pin add -n .
- name: Packages
run: opam depext -yt cstruct-async
- name: Dependencies
run: opam install -t cstruct-async
3 changes: 2 additions & 1 deletion cstruct-sexp.opam
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ build: [
depends: [
"ocaml" {>= "4.08.0"}
"dune" {>= "2.0.0"}
"sexplib"
"sexplib0"
"cstruct" {=version}
"alcotest" {with-test}
"sexplib" {with-test}
]
synopsis: "S-expression serialisers for C-like structures"
description: """
Expand Down
13 changes: 8 additions & 5 deletions lib/cstruct_sexp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Sexplib
open Sexplib0

type buffer = Cstruct.buffer
type t = Cstruct.t

let buffer_of_sexp b = Conv.bigstring_of_sexp b
let sexp_of_buffer b = Conv.sexp_of_bigstring b

let t_of_sexp = function
| Sexp.Atom str ->
let n = String.length str in
let t = Cstruct.create_unsafe n in
Cstruct.blit_from_string str 0 t 0 n ;
t
| sexp -> Conv.of_sexp_error "Cstruct.t_of_sexp: atom needed" sexp
| sexp -> Sexp_conv.of_sexp_error "Cstruct.t_of_sexp: atom needed" sexp

let sexp_of_t t =
let n = Cstruct.length t in
let str = Bytes.create n in
Cstruct.blit_to_bytes t 0 str 0 n ;
(* The following call is safe, since str is not visible elsewhere. *)
Sexp.Atom (Bytes.unsafe_to_string str)

let buffer_of_sexp b =
(t_of_sexp b).Cstruct.buffer

let sexp_of_buffer b =
sexp_of_t (Cstruct.of_bigarray b)
8 changes: 4 additions & 4 deletions lib/cstruct_sexp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
type buffer = Cstruct.buffer
(** [buffer] is an alias for the corresponding {!type:Cstruct.buffer} type *)

val sexp_of_buffer : Cstruct.buffer -> Sexplib.Sexp.t
val sexp_of_buffer : Cstruct.buffer -> Sexplib0.Sexp.t
(** [sexp_of_buffer b] returns the s-expression representation of the raw memory buffer [b] *)

val buffer_of_sexp : Sexplib.Sexp.t -> Cstruct.buffer
val buffer_of_sexp : Sexplib0.Sexp.t -> Cstruct.buffer
(** [buffer_of_sexp s] returns a fresh memory buffer from the s-expression [s].
[s] should have been constructed using {!sexp_of_buffer}. *)

type t = Cstruct.t
(** [t] is an alias for the corresponding {!Cstruct.t} type *)

val sexp_of_t : t -> Sexplib.Sexp.t
val sexp_of_t : t -> Sexplib0.Sexp.t
(** [sexp_of_t t] returns the s-expression representation of the Cstruct [t] *)

val t_of_sexp : Sexplib.Sexp.t -> t
val t_of_sexp : Sexplib0.Sexp.t -> t
(** [t_of_sexp s] returns a fresh {!Cstruct.t} that represents the
s-expression previously serialised by {!sexp_of_t}. *)
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
(name cstruct_sexp)
(public_name cstruct-sexp)
(modules cstruct_sexp)
(libraries cstruct sexplib))
(libraries cstruct sexplib0))
2 changes: 1 addition & 1 deletion lib_test/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(executable
(libraries cstruct alcotest cstruct-sexp)
(libraries cstruct alcotest cstruct-sexp sexplib)
(modules bounds tests)
(name tests))

Expand Down
2 changes: 1 addition & 1 deletion ppx/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
(ppx_runtime_libraries cstruct)
(preprocess
(pps ppxlib.metaquot))
(libraries sexplib ppxlib))
(libraries sexplib0 ppxlib))
34 changes: 20 additions & 14 deletions ppx/ppx_cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ type cenum =
{ name : string Loc.t;
fields : (string Loc.t * int64) list;
prim : prim;
sexp : bool;
sexp : [ `None | `Sexp_of | `Sexp ];
}

let enum_op_name cenum =
Expand Down Expand Up @@ -429,17 +429,17 @@ let enum_integer ~loc {prim; _} =

let declare_enum_expr ~loc ({fields; _} as cenum) = function
| Enum_to_sexp ->
[%expr fun x -> Sexplib.Sexp.Atom ([%e Ast.evar ~loc (enum_op_name cenum Enum_print)] x) ]
[%expr fun x -> Sexplib0.Sexp.Atom ([%e Ast.evar ~loc (enum_op_name cenum Enum_print)] x) ]
| Enum_of_sexp ->
[%expr
fun x ->
match x with
| Sexplib.Sexp.List _ ->
raise (Sexplib.Pre_sexp.Of_sexp_error (Failure "expected Atom, got List", x))
| Sexplib.Sexp.Atom v ->
| Sexplib0.Sexp.List _ ->
raise (Sexplib0.Sexp.Of_sexp_error (Failure "expected Atom, got List", x))
| Sexplib0.Sexp.Atom v ->
match [%e Ast.evar ~loc (enum_op_name cenum Enum_parse)] v with
| None ->
raise (Sexplib.Pre_sexp.Of_sexp_error (Failure "unable to parse enum string", x))
raise (Sexplib0.Sexp.Of_sexp_error (Failure "unable to parse enum string", x))
| Some r -> r
]
| Enum_get ->
Expand Down Expand Up @@ -474,12 +474,15 @@ let enum_ops_for {sexp; _} =
Enum_compare ::
Enum_print ::
Enum_parse ::
if sexp then
match sexp with
| `None -> []
| `Sexp_of ->
[ Enum_to_sexp ]
| `Sexp ->

[ Enum_to_sexp
; Enum_of_sexp
]
else
[]

let enum_type_decl {name; fields; _} =
let decls = List.map (fun (f,_) -> Type.constructor f) fields in
Expand Down Expand Up @@ -508,8 +511,8 @@ let enum_op_type ~loc {name; prim; _} =
| Enum_set -> [%type: [%t cty] -> [%t oty]]
| Enum_print -> [%type: [%t cty] -> string]
| Enum_parse -> [%type: string -> [%t cty] option]
| Enum_to_sexp -> [%type: [%t cty] -> Sexplib.Sexp.t]
| Enum_of_sexp -> [%type: Sexplib.Sexp.t -> [%t cty]]
| Enum_to_sexp -> [%type: [%t cty] -> Sexplib0.Sexp.t]
| Enum_of_sexp -> [%type: Sexplib0.Sexp.t -> [%t cty]]
| Enum_compare -> [%type: [%t cty] -> [%t cty] -> int]

let output_enum_sig loc (cenum:cenum) =
Expand Down Expand Up @@ -598,11 +601,14 @@ let cenum decl =
in
let width, sexp =
match attrs with
| ({attr_name = {txt = width; _};attr_payload= PStr [];_})
| ({attr_name = {txt = width; _};attr_payload= PStr [];_})
:: ({attr_name = {txt = "sexp"; _};attr_payload = PStr []; _}) :: [] ->
width, true
width, `Sexp
| ({attr_name = {txt = width; _};attr_payload= PStr [];_})
:: ({attr_name = {txt = "sexp_of"; _};attr_payload = PStr []; _}) :: [] ->
width, `Sexp_of
| ({attr_name = {txt = width; _};attr_payload= PStr [];_}) :: [] ->
width, false
width, `None
| _ ->
loc_err loc "invalid cenum attributes"
in
Expand Down
4 changes: 2 additions & 2 deletions ppx_cstruct.opam
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ depends: [
"ounit" {with-test}
"ppxlib" {>= "0.16.0"}
"ppx_sexp_conv" {with-test}
"sexplib" {>="v0.9.0"}
"sexplib0"
"cstruct-sexp" {with-test}
"cppo" {with-test}
"cstruct-unix" {with-test & =version}
"ocaml-migrate-parsetree" {>= "2.1.0" & with-test}
"lwt_ppx" {>= "2.0.2" & with-test}
"sexplib0" {>="v0.9.0" & with-test}
]
synopsis: "Access C-like structures directly from OCaml"
description: """
Expand Down
2 changes: 1 addition & 1 deletion ppx_test/basic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ let tests () =
assert(get_foo_b be = 44);
assert(get_foo_a be = 7);
hexdump_foo be;
print_endline (Sexplib.Sexp.to_string_hum (Cstruct_sexp.sexp_of_t be));
print_endline (Sexplib0.Sexp.to_string_hum (Cstruct_sexp.sexp_of_t be));
hexdump_with_ignored_field (Cstruct.of_hex "010203")

let () = tests ()
2 changes: 1 addition & 1 deletion ppx_test/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(tests
(names pcap basic enum)
(deps http.cap)
(libraries cstruct-unix sexplib cstruct-sexp)
(libraries cstruct-unix sexplib0 cstruct-sexp)
(preprocess
(pps ppx_cstruct))
(package ppx_cstruct))
2 changes: 1 addition & 1 deletion ppx_test/errors/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(preprocess
(action
(run %{bin:cppo} -V OCAML:%{ocaml_version} %{input-file})))
(libraries ppx_cstruct ocaml-migrate-parsetree))
(libraries ppx_cstruct ppxlib))

(executable
(name gen_tests)
Expand Down
11 changes: 11 additions & 0 deletions ppx_test/with-sexp-of/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(executable
(name ppx_cstruct_and_sexp_of)
(preprocess
(pps ppx_cstruct ppx_sexp_conv -- -no-check))
(libraries cstruct sexplib0 cstruct-sexp))

(rule
(alias runtest)
(package ppx_cstruct)
(action
(run ./ppx_cstruct_and_sexp_of.exe)))
6 changes: 6 additions & 0 deletions ppx_test/with-sexp-of/ppx_cstruct_and_sexp_of.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(* inspect output with [dune describe pp ppx_test/with-sexp-of/ppx_cstruct_and_sexp_of.ml] *)
[%%cenum
type enum =
| One [@id 1]
| Three [@id 3]
[@@uint8_t][@@sexp_of]]
2 changes: 1 addition & 1 deletion ppx_test/with-sexp/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(name ppx_cstruct_and_sexp)
(preprocess
(pps ppx_cstruct ppx_sexp_conv -- -no-check))
(libraries cstruct sexplib cstruct-sexp))
(libraries cstruct sexplib0 cstruct-sexp))

(rule
(alias runtest)
Expand Down
2 changes: 1 addition & 1 deletion ppx_test/with-sexp/ppx_cstruct_and_sexp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
magic: uint8_t [@len 16];
}[@@little_endian]]

open Sexplib.Std
open Sexplib0.Sexp_conv
type t = int [@@deriving sexp]

type bar = {
Expand Down