Generated Bindings
Pre-generated language bindings for the Matchstick data model.
Overview
Bindings are generated from the canonical Protocol Buffer definitions. Three languages are supported:
| Language | Package | Location |
|---|---|---|
| Rust | matchstick-model | gen/rust/ |
| Python | matchstick_model | gen/python/ |
| TypeScript | @matchstick/model | gen/typescript/ |
Rust
Installation
toml
[dependencies]
matchstick-model = "0.1"
prost = "0.12"Or generate from source:
toml
[build-dependencies]
prost-build = "0.12"Usage
rust
use matchstick_model::events::v1::Event;
use prost::Message;
// Create an event
let event = Event {
id: "sec:form4:abc123".to_string(),
event_type: "FORM_4".to_string(),
issuer: "APPLE INC".to_string(),
cik: "0000320193".to_string(),
confidence: 0.98,
..Default::default()
};
// Serialize to bytes
let bytes = event.encode_to_vec();
// Deserialize from bytes
let decoded = Event::decode(&bytes[..]).unwrap();
assert_eq!(decoded.issuer, "APPLE INC");JSON Serialization
Using prost-types and serde:
rust
use serde_json;
let json = serde_json::to_string(&event).unwrap();
let from_json: Event = serde_json::from_str(&json).unwrap();Python
Installation
bash
pip install matchstick-modelOr generate from source:
bash
pip install grpcio-tools
python -m grpc_tools.protoc -I proto/ --python_out=gen/python/ proto/**/*.protoUsage
python
from matchstick_model.events.v1 import event_pb2
# Create an event
event = event_pb2.Event(
id="sec:form4:abc123",
event_type="FORM_4",
issuer="APPLE INC",
cik="0000320193",
confidence=0.98
)
# Serialize to bytes
data = event.SerializeToString()
# Deserialize from bytes
decoded = event_pb2.Event()
decoded.ParseFromString(data)
assert decoded.issuer == "APPLE INC"JSON Serialization
python
from google.protobuf.json_format import MessageToJson, Parse
# To JSON
json_str = MessageToJson(event)
# From JSON
parsed = Parse(json_str, event_pb2.Event())TypeScript
Installation
bash
npm install @matchstick/modelOr generate from source:
bash
npm install @protobuf-ts/plugin
npx protoc --ts_out=gen/typescript/ -I proto/ proto/**/*.protoUsage
typescript
import { Event } from '@matchstick/model';
// Create an event
const event: Event = {
id: 'sec:form4:abc123',
eventType: 'FORM_4',
issuer: 'APPLE INC',
cik: '0000320193',
confidence: 0.98,
};
// Serialize to JSON (native)
const json = JSON.stringify(event);
// With protobuf-ts for binary serialization
import { Event as EventMessage } from '@matchstick/model/gen/event';
const bytes = EventMessage.toBinary(event);
const decoded = EventMessage.fromBinary(bytes);Regenerating Bindings
To regenerate bindings after schema changes:
bash
# In the matchstick-model repo
./scripts/generate-bindings.sh
# Or manually:
# Rust
prost-build --out-dir=gen/rust/ proto/**/*.proto
# Python
python -m grpc_tools.protoc -I proto/ --python_out=gen/python/ proto/**/*.proto
# TypeScript
npx protoc --ts_out=gen/typescript/ -I proto/ proto/**/*.protoVersion Compatibility
| Model Version | Rust | Python | TypeScript |
|---|---|---|---|
| 0.1.x | 0.1.x | 0.1.x | 0.1.x |
Bindings follow the same semver as the model. Minor version updates are backward compatible.