Skip to content

Commit

Permalink
Expose CompoundType publicly
Browse files Browse the repository at this point in the history
This is needed to be able to construct more advanced types, such as
arrays and maps, using the public API.

Also make the `CompoundType::from_type` function publicly so it is
possible to construct compound types in public functions using the
public API.
  • Loading branch information
zakcutner committed Nov 12, 2024
1 parent 0a82883 commit 44fe2d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
9 changes: 1 addition & 8 deletions engine/src/lhs_types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,8 @@ impl<'a, V: IntoValue<'a>> FromIterator<V> for TypedArray<'a, V> {
}
}

const fn compound_from_type(ty: Type) -> CompoundType {
match CompoundType::from_type(ty) {
Some(ty) => ty,
None => panic!("Could not convert type to compound type"),
}
}

impl<'a, V: IntoValue<'a>> IntoValue<'a> for TypedArray<'a, V> {
const TYPE: Type = Type::Array(compound_from_type(V::TYPE));
const TYPE: Type = Type::Array(CompoundType::from_type(V::TYPE));

#[inline]
fn into_value(self) -> LhsValue<'a> {
Expand Down
4 changes: 2 additions & 2 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub use self::{
UnknownFieldError,
},
types::{
ExpectedType, ExpectedTypeList, GetType, LhsValue, LhsValueMut, RhsValue, RhsValues, Type,
TypeMismatchError,
CompoundType, ExpectedType, ExpectedTypeList, GetType, LhsValue, LhsValueMut, RhsValue,
RhsValues, Type, TypeMismatchError,
},
};
16 changes: 11 additions & 5 deletions engine/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ pub enum ExpectedType {
/// Fully identified expected type
Type(Type),
/// Loosely identified array type
/// Usefull when expecting an array without
/// Useful when expecting an array without
/// knowing of which specific value type
Array,
/// Loosely identified map type
/// Usefull when expecting a map without
/// Useful when expecting a map without
/// knowing of which specific value type
Map,
}
Expand Down Expand Up @@ -986,6 +986,8 @@ enum Layer {
Map,
}

/// A types for field values that can be nested within another type such as an
/// array or map.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CompoundType {
layers: u32,
Expand Down Expand Up @@ -1023,15 +1025,19 @@ impl CompoundType {
}
}

/// Converts a [`Type`] into [`CompoundType`].
#[inline]
pub(crate) const fn from_type(ty: Type) -> Option<Self> {
match ty {
pub const fn from_type(ty: Type) -> Self {
match match ty {
Type::Bool => Some(Self::new(PrimitiveType::Bool)),
Type::Bytes => Some(Self::new(PrimitiveType::Bytes)),
Type::Int => Some(Self::new(PrimitiveType::Int)),
Type::Ip => Some(Self::new(PrimitiveType::Ip)),
Type::Array(ty) => ty.push(Layer::Array),
Type::Map(ty) => ty.push(Layer::Map),
} {
Some(ty) => ty,
None => panic!("Could not convert type to compound type"),
}
}

Expand Down Expand Up @@ -1078,7 +1084,7 @@ impl From<PrimitiveType> for CompoundType {
impl From<Type> for CompoundType {
#[inline]
fn from(ty: Type) -> Self {
Self::from_type(ty).unwrap()
Self::from_type(ty)
}
}

Expand Down

0 comments on commit 44fe2d6

Please sign in to comment.