pub struct Schema {
pub version: SchemaVersion,
pub types: BTreeMap<String, Vec<FieldDef>>,
}Expand description
Schema definition with versioning.
Represents a complete schema for a type, including version information and field definitions.
§Examples
use hedl_core::schema_version::{Schema, SchemaVersion, FieldDef};
let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
schema.add_type("User", vec![
FieldDef::required("id"),
FieldDef::required("name"),
FieldDef::optional("email"),
]);Fields§
§version: SchemaVersionSchema version.
types: BTreeMap<String, Vec<FieldDef>>Type definitions: type name -> field definitions.
Implementations§
Source§impl Schema
impl Schema
Sourcepub fn new(version: SchemaVersion) -> Self
pub fn new(version: SchemaVersion) -> Self
Sourcepub fn add_type(&mut self, name: &str, fields: Vec<FieldDef>)
pub fn add_type(&mut self, name: &str, fields: Vec<FieldDef>)
Add a type definition to the schema.
§Arguments
name- The type namefields- Field definitions for the type
§Examples
use hedl_core::schema_version::{Schema, SchemaVersion, FieldDef};
let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
schema.add_type("User", vec![
FieldDef::required("id"),
FieldDef::required("name"),
]);
assert!(schema.types.contains_key("User"));Sourcepub fn get_fields(&self, type_name: &str) -> Option<&Vec<FieldDef>>
pub fn get_fields(&self, type_name: &str) -> Option<&Vec<FieldDef>>
Get field definitions for a type.
§Arguments
type_name- The type name to look up
§Returns
Some(&Vec<FieldDef>) if the type exists, None otherwise
§Examples
use hedl_core::schema_version::{Schema, SchemaVersion, FieldDef};
let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
schema.add_type("User", vec![FieldDef::required("id")]);
assert!(schema.get_fields("User").is_some());
assert!(schema.get_fields("Post").is_none());Sourcepub fn is_compatible_with(&self, other: &Self) -> bool
pub fn is_compatible_with(&self, other: &Self) -> bool
Check if this schema is compatible with another schema.
Schemas are compatible if their versions are compatible.
§Arguments
other- The schema to check compatibility with
§Returns
true if this schema can read data written for other
§Examples
use hedl_core::schema_version::{Schema, SchemaVersion};
let v1 = Schema::new(SchemaVersion::new(1, 0, 0));
let v1_1 = Schema::new(SchemaVersion::new(1, 1, 0));
let v2 = Schema::new(SchemaVersion::new(2, 0, 0));
assert!(v1_1.is_compatible_with(&v1));
assert!(!v1.is_compatible_with(&v1_1));
assert!(!v2.is_compatible_with(&v1));Trait Implementations§
Auto Trait Implementations§
impl Freeze for Schema
impl RefUnwindSafe for Schema
impl Send for Schema
impl Sync for Schema
impl Unpin for Schema
impl UnwindSafe for Schema
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more