Skip to content

Commit

Permalink
move 'decoder' and 'encoder' into a sub-library
Browse files Browse the repository at this point in the history
to later share it with git wire proto v2 implementation
  • Loading branch information
ulugbekna committed Nov 27, 2020
1 parent 307e41e commit b4ad213
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
11 changes: 9 additions & 2 deletions src/not-so-smart/dune
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
(library
(name pkt_line)
(public_name git-nss.pkt-line)
(modules decoder encoder)
(libraries astring fmt))

(library
(name smart)
(public_name git-nss.smart)
(modules smart filter capability state protocol encoder decoder)
(libraries conduit stdlib-shims result rresult domain-name astring fmt))
(modules smart filter capability state protocol)
(libraries git-nss.pkt-line conduit stdlib-shims result rresult domain-name
astring fmt))

(library
(name sigs)
Expand Down
26 changes: 13 additions & 13 deletions src/not-so-smart/protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ end

module Decoder = struct
open Astring
open Decoder
open Pkt_line.Decoder

type error =
[ Decoder.error
type nonrec error =
[ error
| `Invalid_advertised_ref of string
| `Invalid_shallow of string
| `Invalid_negotiation_result of string
Expand All @@ -282,7 +282,7 @@ module Decoder = struct
| `Invalid_pkt_line ]

let pp_error ppf = function
| #Decoder.error as err -> Decoder.pp_error ppf err
| #Pkt_line.Decoder.error as err -> Pkt_line.Decoder.pp_error ppf err
| `Invalid_advertised_ref raw ->
Fmt.pf ppf "Invalid advertised refererence (%S)" raw
| `Invalid_shallow raw -> Fmt.pf ppf "Invalid shallow (%S)" raw
Expand Down Expand Up @@ -630,12 +630,12 @@ module Decoder = struct

let rec bind x ~f =
match x with
| Decoder.Done v -> f v
| Decoder.Read { buffer; off; len; continue; eof } ->
| Done v -> f v
| Read { buffer; off; len; continue; eof } ->
let continue len = bind (continue len) ~f in
let eof () = bind (eof ()) ~f in
Decoder.Read { buffer; off; len; continue; eof }
| Decoder.Error _ as err -> err
Read { buffer; off; len; continue; eof }
| Error _ as err -> err

let ( >>= ) x f = bind x ~f

Expand Down Expand Up @@ -696,7 +696,7 @@ module Decoder = struct
match String.Sub.head pkt with
| Some '\001' ->
let str = String.Sub.(to_string (tail pkt)) in
let decoder' = Decoder.decoder_from str in
let decoder' = decoder_from str in
decode_status decoder' >>= fun res ->
junk_pkt decoder;
prompt_pkt (return res) decoder
Expand All @@ -707,11 +707,11 @@ module Decoder = struct
end

module Encoder = struct
open Encoder
open Pkt_line.Encoder

type error = Encoder.error
type nonrec error = error

let pp_error = Encoder.pp_error
let pp_error = pp_error
let write_space encoder = write encoder " "
let write_zero encoder = write encoder "\000"
let write_new_line encoder = write encoder "\n"
Expand Down Expand Up @@ -858,7 +858,7 @@ module Encoder = struct
let unsafe_encode_packet encoder ~packet =
let pos = encoder.pos in
encoder.pos <- encoder.pos + 4;
Encoder.write encoder packet;
write encoder packet;
let len = encoder.pos - pos in
Bytes.blit_string (Fmt.strf "%04X" len) 0 encoder.payload pos 4

Expand Down
10 changes: 5 additions & 5 deletions src/not-so-smart/protocol.mli
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ module Status : sig
end

module Decoder : sig
open Decoder
open Pkt_line.Decoder

type error =
[ Decoder.error
type nonrec error =
[ error
| `Invalid_advertised_ref of string
| `Invalid_shallow of string
| `Invalid_negotiation_result of string
Expand Down Expand Up @@ -177,9 +177,9 @@ module Decoder : sig
end

module Encoder : sig
open Encoder
open Pkt_line.Encoder

type error = Encoder.error
type nonrec error = error

val pp_error : error Fmt.t
val encode_proto_request : encoder -> Proto_request.t -> error state
Expand Down
2 changes: 2 additions & 0 deletions src/not-so-smart/smart.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module Witness = struct
end

module Value = struct
open Pkt_line

type encoder = Encoder.encoder
type decoder = Decoder.decoder

Expand Down
2 changes: 2 additions & 0 deletions src/not-so-smart/state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module type S = sig
end

module Context = struct
open Pkt_line

type t = {
encoder : Encoder.encoder;
decoder : Decoder.decoder;
Expand Down
2 changes: 2 additions & 0 deletions src/not-so-smart/state.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module type S = sig
end

module Context : sig
open Pkt_line

include
CONTEXT
with type encoder = Encoder.encoder
Expand Down

0 comments on commit b4ad213

Please sign in to comment.