ForgeDB Types
Core type definitions for ForgeDB schemas and generated code.
Overview
The forgedb-types crate provides type definitions that match ForgeDB's schema language types, enabling type-safe serialization, validation, and storage operations. This is a foundational crate used by generated code and other ForgeDB runtime libraries.
Features
- Type-safe primitives - Wrappers for all ForgeDB schema types
- Serde support - Automatic JSON/binary serialization
- Zero-cost abstractions - Minimal runtime overhead
- Comprehensive documentation - Full API docs with examples
- Extensive testing - >80% test coverage
Supported Types
Primitive Types
| ForgeDB Type | Rust Type | Description |
|---|---|---|
i32 |
i32 |
32-bit signed integer |
i64 |
i64 |
64-bit signed integer |
f64 |
f64 |
64-bit floating point |
bool |
bool |
Boolean value |
string |
String |
UTF-8 encoded text |
uuid |
Uuid |
Universally unique identifier |
timestamp |
Timestamp |
Unix timestamp (seconds since epoch) |
Special Types
Timestamp
A wrapper around i64 that represents Unix timestamps (seconds since January 1, 1970 00:00:00 UTC).
use Timestamp;
// Create from current time
let now = now;
// Create from seconds
let ts = from_seconds;
// Get underlying value
let seconds = ts.as_seconds;
// Convert to/from i64
let ts: Timestamp = 1234567890_i64.into;
let seconds: i64 = ts.into;
Value
A generic enum that can hold any ForgeDB primitive type, useful for working with heterogeneous data.
use ;
// Create values
let int_val = I32;
let str_val = String;
let uuid_val = Uuid;
// Query type information
assert_eq!;
assert!;
assert!;
// Serialize to JSON
let json = to_string.unwrap;
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage Examples
Basic Types
use ;
// Work with timestamps
let ts = now;
println!;
// Create typed values
let values = vec!;
// Check types
for val in &values
Serialization
use ;
use serde_json;
// Serialize a value
let val = I32;
let json = to_string.unwrap;
println!;
// Deserialize back
let deserialized: Value = from_str.unwrap;
assert_eq!;
// Timestamps serialize as plain i64
let ts = from_seconds;
let json = to_string.unwrap;
assert_eq!;
Type Conversions
use Value;
// Convenient From implementations
let val: Value = 42_i32.into;
let val: Value = 3.14_f64.into;
let val: Value = true.into;
let val: Value = "hello".into;
// Type checking
if val.is_numeric
if val.is_string
API Documentation
Core Types
Timestamp
Timestamp::from_seconds(i64) -> Timestamp- Create from seconds since epochTimestamp::now() -> Timestamp- Get current timestampts.as_seconds() -> i64- Get seconds value- Implements:
Debug,Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Hash,Serialize,Deserialize
Value
Value::type_name() -> &str- Get type name stringValue::is_numeric() -> bool- Check if numeric type (i32/i64/f64)Value::is_string() -> bool- Check if string type- Implements:
Debug,Clone,PartialEq,Serialize,Deserialize - Variants:
I32(i32),I64(i64),F64(f64),Bool(bool),String(String),Uuid(Uuid),Timestamp(Timestamp)
Uuid
Re-exported from the uuid crate. See its documentation for full API.
Design Decisions
Why Wrap Timestamps?
While ForgeDB stores timestamps as plain i64 values, we provide a Timestamp wrapper type for:
- Type safety (prevents mixing timestamps with other integers)
- Convenient constructors (
now(),from_seconds()) - Clear intent in APIs
- Future extensibility (e.g., different time units)
The wrapper has zero runtime overhead due to #[repr(transparent)] and is serialized as a plain i64.
Why Use Value Enum?
The Value enum provides:
- Type-safe heterogeneous collections
- Runtime type information
- Generic storage for dynamic data
- JSON/binary serialization with type tags
Testing
# Run all tests
# Run with output
# Run doc tests
Performance
All types are designed for zero or minimal overhead:
Timestampis#[repr(transparent)]overi64- No heap allocations except for
StringandValue::String Copytypes where appropriate for stack efficiency- Serde uses derive macros for optimal code generation
Dependencies
Dev dependencies:
serde_json- JSON testing
Contributing
This crate is part of the ForgeDB project.
Documentation
For more information about ForgeDB:
- ForgeDB Architecture - System design and component architecture
- Public Crates Guide - Complete runtime library documentation
- Development Guide - Development setup and workflow
- Contributing Guide - How to contribute
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.