diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7d06679..2a370125 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: @@ -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 diff --git a/cstruct-sexp.opam b/cstruct-sexp.opam index 3cfbbcd9..2dc8a07e 100644 --- a/cstruct-sexp.opam +++ b/cstruct-sexp.opam @@ -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: """ diff --git a/lib/cstruct_sexp.ml b/lib/cstruct_sexp.ml index d3c8def2..7e1f9174 100644 --- a/lib/cstruct_sexp.ml +++ b/lib/cstruct_sexp.ml @@ -14,21 +14,18 @@ * 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 @@ -36,3 +33,9 @@ let sexp_of_t t = 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) diff --git a/lib/cstruct_sexp.mli b/lib/cstruct_sexp.mli index 62f2842a..cb816b88 100644 --- a/lib/cstruct_sexp.mli +++ b/lib/cstruct_sexp.mli @@ -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}. *) diff --git a/lib/dune b/lib/dune index c1fa1de3..cddb67f5 100644 --- a/lib/dune +++ b/lib/dune @@ -13,4 +13,4 @@ (name cstruct_sexp) (public_name cstruct-sexp) (modules cstruct_sexp) - (libraries cstruct sexplib)) + (libraries cstruct sexplib0)) diff --git a/lib_test/dune b/lib_test/dune index 16fb7d97..aab31566 100644 --- a/lib_test/dune +++ b/lib_test/dune @@ -1,5 +1,5 @@ (executable - (libraries cstruct alcotest cstruct-sexp) + (libraries cstruct alcotest cstruct-sexp sexplib) (modules bounds tests) (name tests)) diff --git a/ppx/dune b/ppx/dune index c16ee848..98500c52 100644 --- a/ppx/dune +++ b/ppx/dune @@ -6,4 +6,4 @@ (ppx_runtime_libraries cstruct) (preprocess (pps ppxlib.metaquot)) - (libraries sexplib ppxlib)) + (libraries sexplib0 ppxlib)) diff --git a/ppx/ppx_cstruct.ml b/ppx/ppx_cstruct.ml index 3525db88..defd9c72 100644 --- a/ppx/ppx_cstruct.ml +++ b/ppx/ppx_cstruct.ml @@ -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 = @@ -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 -> @@ -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 @@ -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) = @@ -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 diff --git a/ppx_cstruct.opam b/ppx_cstruct.opam index 264ad415..74a7c64e 100644 --- a/ppx_cstruct.opam +++ b/ppx_cstruct.opam @@ -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: """ diff --git a/ppx_test/basic.ml b/ppx_test/basic.ml index 02131dac..e49624f6 100644 --- a/ppx_test/basic.ml +++ b/ppx_test/basic.ml @@ -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 () diff --git a/ppx_test/dune b/ppx_test/dune index 3f85e8c6..d4045d39 100644 --- a/ppx_test/dune +++ b/ppx_test/dune @@ -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)) diff --git a/ppx_test/errors/dune b/ppx_test/errors/dune index ed51ce0c..c9f58312 100644 --- a/ppx_test/errors/dune +++ b/ppx_test/errors/dune @@ -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) diff --git a/ppx_test/with-sexp-of/dune b/ppx_test/with-sexp-of/dune new file mode 100644 index 00000000..a096fd3f --- /dev/null +++ b/ppx_test/with-sexp-of/dune @@ -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))) diff --git a/ppx_test/with-sexp-of/ppx_cstruct_and_sexp_of.ml b/ppx_test/with-sexp-of/ppx_cstruct_and_sexp_of.ml new file mode 100644 index 00000000..6539c845 --- /dev/null +++ b/ppx_test/with-sexp-of/ppx_cstruct_and_sexp_of.ml @@ -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]] diff --git a/ppx_test/with-sexp/dune b/ppx_test/with-sexp/dune index b247296b..b252fc4e 100644 --- a/ppx_test/with-sexp/dune +++ b/ppx_test/with-sexp/dune @@ -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) diff --git a/ppx_test/with-sexp/ppx_cstruct_and_sexp.ml b/ppx_test/with-sexp/ppx_cstruct_and_sexp.ml index fb88625e..ba71d7c0 100644 --- a/ppx_test/with-sexp/ppx_cstruct_and_sexp.ml +++ b/ppx_test/with-sexp/ppx_cstruct_and_sexp.ml @@ -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 = {