Skip to content

Commit 1aa11ce

Browse files
hyperpolymathclaude
andcommitted
feat(v-ecosystem): Idris2 ABI type definitions for 6 API protocols
Dependent-type definitions for each protocol: - v-websocket: Frame, ConnState, CloseCode (LTE proof on AppDefined) - v-jsonrpc: JSON-RPC 2.0 Request/Response, ErrorCode, Batch (non-empty) - v-mqtt: MQTT 3.1.1 PacketType (14 variants), QoS, TopicFilter - v-trpc: Procedure (query/mutation), Router, Middleware, Context - v-soap: SOAP Envelope, Header, Body, Fault, ParsedResponse (sum) - v-capnproto: Segment (LTE position capacity proof), FieldType, Schema Zero believe_me, assert_total, or assert_smaller. Dependent types used for bounds proofs on Segment and CloseCode. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7ac829e commit 1aa11ce

6 files changed

Lines changed: 390 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
-- SPDX-License-Identifier: PMPL-1.0-or-later
2+
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath)
3+
--
4+
-- Idris2 ABI type definitions for the v-capnproto protocol.
5+
-- Segment, message, field, and schema types for zero-copy serialisation.
6+
7+
module Types
8+
9+
import Data.List
10+
import Data.Nat
11+
12+
||| Word size in bytes (Cap'n Proto uses 8-byte alignment).
13+
public export
14+
WordSize : Nat
15+
WordSize = 8
16+
17+
||| Contiguous byte buffer for zero-copy struct/list storage.
18+
||| The position is proven to never exceed capacity.
19+
public export
20+
record Segment where
21+
constructor MkSegment
22+
capacity : Nat
23+
position : Nat
24+
bounded : LTE position capacity
25+
26+
||| Wire type tag for a field within a struct's data section.
27+
public export
28+
data FieldType : Type where
29+
FVoid : FieldType
30+
FBool : FieldType
31+
FUInt8 : FieldType
32+
FUInt16 : FieldType
33+
FUInt32 : FieldType
34+
FUInt64 : FieldType
35+
FFloat32 : FieldType
36+
FFloat64 : FieldType
37+
FText : FieldType -- Length-prefixed UTF-8
38+
FData : FieldType -- Length-prefixed raw bytes
39+
FStruct : FieldType -- Pointer to nested struct
40+
FList : (elemType : FieldType) -> FieldType
41+
42+
||| Descriptor for a single field within a struct schema.
43+
public export
44+
record FieldDescriptor where
45+
constructor MkFieldDescriptor
46+
name : String
47+
fieldType : FieldType
48+
offset : Nat -- Byte offset in data section
49+
ordinal : Nat -- Unique within struct, 0-based
50+
51+
||| Schema defining a struct's layout and fields.
52+
public export
53+
record StructSchema where
54+
constructor MkStructSchema
55+
name : String
56+
dataSize : Nat -- Data section bytes (word-aligned)
57+
pointerCount : Nat
58+
fields : List FieldDescriptor
59+
60+
||| A complete message with root segment and optional extras.
61+
public export
62+
record Message where
63+
constructor MkMessage
64+
rootSegment : Segment
65+
extraSegments : List Segment
66+
67+
||| RPC wire header prepended to each request or response.
68+
public export
69+
record RpcHeader where
70+
constructor MkRpcHeader
71+
methodId : Nat
72+
payloadLen : Nat
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- SPDX-License-Identifier: PMPL-1.0-or-later
2+
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath)
3+
--
4+
-- Idris2 ABI type definitions for the v-jsonrpc protocol.
5+
-- JSON-RPC 2.0 request/response types, error codes, batch requests.
6+
7+
module Types
8+
9+
import Data.List
10+
11+
||| Minimal JSON value subset for RPC payloads.
12+
public export
13+
data JsonValue : Type where
14+
JsonNull : JsonValue
15+
JsonBool : Bool -> JsonValue
16+
JsonInt : Integer -> JsonValue
17+
JsonString : String -> JsonValue
18+
JsonArray : List JsonValue -> JsonValue
19+
JsonObject : List (String, JsonValue) -> JsonValue
20+
21+
||| Request identifier (string, number, or null for notifications).
22+
public export
23+
data RequestId : Type where
24+
IdNum : Integer -> RequestId
25+
IdStr : String -> RequestId
26+
IdNull : RequestId
27+
28+
||| Standard JSON-RPC 2.0 error codes (spec section 5.1).
29+
public export
30+
data ErrorCode : Type where
31+
ParseError : ErrorCode -- -32700
32+
InvalidRequest : ErrorCode -- -32600
33+
MethodNotFound : ErrorCode -- -32601
34+
InvalidParams : ErrorCode -- -32602
35+
InternalError : ErrorCode -- -32603
36+
ServerError : (code : Integer) -> ErrorCode -- [-32099..-32000]
37+
38+
||| Structured error object attached to failure responses.
39+
public export
40+
record RpcError where
41+
constructor MkRpcError
42+
code : ErrorCode
43+
message : String
44+
errData : Maybe JsonValue
45+
46+
||| A single JSON-RPC 2.0 request.
47+
public export
48+
record Request where
49+
constructor MkRequest
50+
method : String
51+
params : Maybe JsonValue
52+
reqId : RequestId
53+
54+
||| A JSON-RPC 2.0 response (exactly one of result or error).
55+
public export
56+
data Response : Type where
57+
Success : (reqId : RequestId) -> (result : JsonValue) -> Response
58+
Failure : (reqId : RequestId) -> (err : RpcError) -> Response
59+
60+
||| Non-empty batch of requests sent as a JSON array.
61+
public export
62+
record Batch where
63+
constructor MkBatch
64+
first : Request
65+
rest : List Request
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- SPDX-License-Identifier: PMPL-1.0-or-later
2+
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath)
3+
--
4+
-- Idris2 ABI type definitions for the v-mqtt protocol.
5+
-- MQTT 3.1.1 packet types, QoS levels, topic filters, connection states.
6+
7+
module Types
8+
9+
import Data.List
10+
11+
||| Quality of Service level (MQTT 3.1.1 section 4.3).
12+
public export
13+
data QoS : Type where
14+
AtMostOnce : QoS -- QoS 0: fire and forget
15+
AtLeastOnce : QoS -- QoS 1: acknowledged delivery
16+
ExactlyOnce : QoS -- QoS 2: four-step handshake
17+
18+
||| MQTT control packet type (4-bit code, upper nibble of byte 1).
19+
public export
20+
data PacketType : Type where
21+
Connect : PacketType
22+
ConnAck : PacketType
23+
Publish : PacketType
24+
PubAck : PacketType
25+
PubRec : PacketType
26+
PubRel : PacketType
27+
PubComp : PacketType
28+
Subscribe : PacketType
29+
SubAck : PacketType
30+
Unsubscribe : PacketType
31+
UnsubAck : PacketType
32+
PingReq : PacketType
33+
PingResp : PacketType
34+
Disconnect : PacketType
35+
36+
||| CONNACK return codes (section 3.2.2.3).
37+
public export
38+
data ConnReturnCode : Type where
39+
Accepted : ConnReturnCode
40+
BadProtocolVersion : ConnReturnCode
41+
IdentifierRejected : ConnReturnCode
42+
ServerUnavailable : ConnReturnCode
43+
BadCredentials : ConnReturnCode
44+
NotAuthorised : ConnReturnCode
45+
46+
||| Client connection lifecycle state.
47+
public export
48+
data ConnState : Type where
49+
Disconnected : ConnState
50+
Connecting : ConnState
51+
Connected : ConnState
52+
53+
||| Topic filter for subscriptions (may contain + and # wildcards).
54+
public export
55+
record TopicFilter where
56+
constructor MkTopicFilter
57+
pattern : String
58+
qos : QoS
59+
60+
||| A PUBLISH message with delivery metadata.
61+
public export
62+
record PublishMsg where
63+
constructor MkPublishMsg
64+
topic : String
65+
payload : List Bits8
66+
qos : QoS
67+
retain : Bool
68+
packetId : Nat
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
-- SPDX-License-Identifier: PMPL-1.0-or-later
2+
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath)
3+
--
4+
-- Idris2 ABI type definitions for the v-soap protocol.
5+
-- SOAP envelope, header, body, and fault types for 1.1 and 1.2.
6+
7+
module Types
8+
9+
import Data.List
10+
11+
||| SOAP protocol version, selecting namespace and content type.
12+
public export
13+
data SoapVersion : Type where
14+
V11 : SoapVersion -- schemas.xmlsoap.org/soap/envelope/
15+
V12 : SoapVersion -- www.w3.org/2003/05/soap-envelope
16+
17+
||| A single SOAP header block carrying metadata.
18+
public export
19+
record Header where
20+
constructor MkHeader
21+
namespace : String -- XML namespace URI (may be empty)
22+
name : String -- Local element name
23+
value : String -- Text content
24+
25+
||| The SOAP Body element wrapping application payload as raw XML.
26+
public export
27+
record Body where
28+
constructor MkBody
29+
content : String
30+
31+
||| SOAP Fault (1.1 section 4.4 / 1.2 section 5.4).
32+
public export
33+
record Fault where
34+
constructor MkFault
35+
faultCode : String -- e.g. "soap:Server"
36+
faultString : String -- Human-readable description
37+
detail : Maybe String -- Optional detail XML
38+
39+
||| Complete SOAP envelope: version, headers, body, and action.
40+
public export
41+
record Envelope where
42+
constructor MkEnvelope
43+
version : SoapVersion
44+
headers : List Header
45+
body : Body
46+
action : String -- SOAPAction header / Content-Type param
47+
48+
||| Parsed SOAP response: either body content or a fault.
49+
public export
50+
data ParsedResponse : Type where
51+
ResponseOk : (statusCode : Nat) -> (body : Body) -> ParsedResponse
52+
ResponseFault : (statusCode : Nat) -> (fault : Fault) -> ParsedResponse
53+
54+
||| SOAP actor/role for the mustUnderstand processing model.
55+
public export
56+
data Actor : Type where
57+
UltimateReceiver : Actor
58+
Intermediary : (uri : String) -> Actor
59+
Next : Actor -- SOAP 1.2 only
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- SPDX-License-Identifier: PMPL-1.0-or-later
2+
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath)
3+
--
4+
-- Idris2 ABI type definitions for the v-trpc protocol.
5+
-- Procedure types (query/mutation), router, middleware chain.
6+
7+
module Types
8+
9+
import Data.List
10+
11+
||| Kind of tRPC procedure: read-only query or state-changing mutation.
12+
public export
13+
data ProcedureKind : Type where
14+
Query : ProcedureKind -- Idempotent read (GET or POST)
15+
Mutation : ProcedureKind -- State-changing write (POST only)
16+
17+
||| Opaque value type for procedure input/output at the ABI level.
18+
public export
19+
data TrpcValue : Type where
20+
ValNull : TrpcValue
21+
ValBool : Bool -> TrpcValue
22+
ValInt : Integer -> TrpcValue
23+
ValStr : String -> TrpcValue
24+
ValArray : List TrpcValue -> TrpcValue
25+
ValObject : List (String, TrpcValue) -> TrpcValue
26+
27+
||| Error returned when a procedure invocation fails.
28+
public export
29+
record TrpcError where
30+
constructor MkTrpcError
31+
code : String -- Machine-readable code (e.g. "NOT_FOUND")
32+
message : String
33+
errData : Maybe TrpcValue
34+
35+
||| Result of invoking a procedure: success or typed error.
36+
public export
37+
data ProcedureResult : Type where
38+
ProcSuccess : (output : TrpcValue) -> ProcedureResult
39+
ProcError : (err : TrpcError) -> ProcedureResult
40+
41+
||| A registered procedure with name, kind, and validation flag.
42+
public export
43+
record Procedure where
44+
constructor MkProcedure
45+
name : String
46+
kind : ProcedureKind
47+
hasValidator : Bool
48+
49+
||| A middleware step in the request processing chain.
50+
public export
51+
record Middleware where
52+
constructor MkMiddleware
53+
name : String -- Human-readable name for logging
54+
priority : Nat -- Lower runs first
55+
56+
||| Per-request context threaded through middleware and handlers.
57+
public export
58+
record Context where
59+
constructor MkContext
60+
headers : List (String, String)
61+
meta : List (String, String)
62+
63+
||| Router collecting procedures and middleware into a unit.
64+
public export
65+
record Router where
66+
constructor MkRouter
67+
procedures : List Procedure
68+
middleware : List Middleware
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
-- SPDX-License-Identifier: PMPL-1.0-or-later
2+
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath)
3+
--
4+
-- Idris2 ABI type definitions for the v-websocket protocol.
5+
-- WebSocket message types, connection states, and room membership.
6+
7+
module Types
8+
9+
import Data.List
10+
11+
||| WebSocket opcode as defined by RFC 6455 section 5.2.
12+
public export
13+
data Opcode : Type where
14+
Continuation : Opcode
15+
Text : Opcode
16+
Binary : Opcode
17+
Close : Opcode
18+
Ping : Opcode
19+
Pong : Opcode
20+
21+
||| Lifecycle state of a WebSocket connection.
22+
public export
23+
data ConnState : Type where
24+
Connecting : ConnState -- TCP up, upgrade not yet sent
25+
Open : ConnState -- Upgrade accepted, frames may flow
26+
Closing : ConnState -- Close sent, awaiting peer ack
27+
Closed : ConnState -- Fully terminated
28+
29+
||| A WebSocket frame with opcode, fin bit, and payload.
30+
public export
31+
record Frame where
32+
constructor MkFrame
33+
opcode : Opcode
34+
fin : Bool
35+
payload : List Bits8
36+
payloadLen : Nat
37+
38+
||| Unique identifier for a connected client.
39+
public export
40+
record ClientId where
41+
constructor MkClientId
42+
value : String
43+
44+
||| Named room grouping clients for broadcast delivery.
45+
public export
46+
record Room where
47+
constructor MkRoom
48+
name : String
49+
members : List ClientId
50+
51+
||| Close status code per RFC 6455 section 7.4.1.
52+
public export
53+
data CloseCode : Type where
54+
NormalClosure : CloseCode -- 1000
55+
GoingAway : CloseCode -- 1001
56+
ProtocolError : CloseCode -- 1002
57+
UnsupportedData : CloseCode -- 1003
58+
AppDefined : (code : Nat) -> {auto prf : LTE 4000 code} -> CloseCode

0 commit comments

Comments
 (0)