Skip to content

Commit

Permalink
refactor(torii): don't use feature-only type in non-feature code (doj…
Browse files Browse the repository at this point in the history
…oengine#1894)

* put schema module under `client` feature

* dont use client error in schema module
  • Loading branch information
kariy authored Apr 27, 2024
1 parent a3587b3 commit 19fb8e9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
3 changes: 3 additions & 0 deletions crates/torii/client/src/client/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dojo_world::contracts::model::ModelError;
use starknet::core::utils::{CairoShortStringToFeltError, ParseCairoShortStringError};
use torii_grpc::types::schema::SchemaError;

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand All @@ -22,6 +23,8 @@ pub enum Error {
Model(#[from] ModelError),
#[error("Unsupported query")]
UnsupportedQuery,
#[error(transparent)]
Schema(#[from] SchemaError),
}

#[derive(Debug, thiserror::Error)]
Expand Down
17 changes: 7 additions & 10 deletions crates/torii/grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,30 @@ use std::num::ParseIntError;

use futures_util::stream::MapOk;
use futures_util::{Stream, StreamExt, TryStreamExt};
use starknet::core::types::{FromByteSliceError, FromStrError, StateUpdate};
use starknet::core::types::{FromStrError, StateUpdate};
use starknet_crypto::FieldElement;

use crate::proto::world::{
world_client, MetadataRequest, RetrieveEntitiesRequest, RetrieveEntitiesResponse,
SubscribeEntitiesRequest, SubscribeEntityResponse, SubscribeModelsRequest,
SubscribeModelsResponse,
};
use crate::types::schema::Entity;
use crate::types::schema::{self, Entity, SchemaError};
use crate::types::{KeysClause, Query};

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Grpc(tonic::Status),
#[error("Missing expected data")]
MissingExpectedData,
#[error("Unsupported type")]
UnsupportedType,
#[error(transparent)]
ParseStr(FromStrError),
#[error(transparent)]
SliceError(FromByteSliceError),
#[error(transparent)]
ParseInt(ParseIntError),

#[cfg(not(target_arch = "wasm32"))]
#[error(transparent)]
Transport(tonic::transport::Error),
#[error(transparent)]
Schema(#[from] schema::SchemaError),
}

/// A lightweight wrapper around the grpc client.
Expand Down Expand Up @@ -71,7 +66,9 @@ impl WorldClient {
.world_metadata(MetadataRequest {})
.await
.map_err(Error::Grpc)
.and_then(|res| res.into_inner().metadata.ok_or(Error::MissingExpectedData))
.and_then(|res| {
res.into_inner().metadata.ok_or(Error::Schema(SchemaError::MissingExpectedData))
})
.and_then(|metadata| metadata.try_into().map_err(Error::ParseStr))
}

Expand Down
53 changes: 31 additions & 22 deletions crates/torii/grpc/src/types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ use crypto_bigint::{Encoding, U256};
use dojo_types::primitive::Primitive;
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};
use serde::{Deserialize, Serialize};
use starknet::core::types::FromByteSliceError;
use starknet_crypto::FieldElement;

use crate::client::Error as ClientError;
use crate::proto::{self};

#[derive(Debug, thiserror::Error)]
pub enum SchemaError {
#[error("Missing expected data")]
MissingExpectedData,
#[error("Unsupported type")]
UnsupportedType,
#[error(transparent)]
SliceError(#[from] FromByteSliceError),
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub struct Entity {
pub hashed_keys: FieldElement,
Expand All @@ -20,11 +30,10 @@ pub struct Model {
}

impl TryFrom<proto::types::Entity> for Entity {
type Error = ClientError;
type Error = SchemaError;
fn try_from(entity: proto::types::Entity) -> Result<Self, Self::Error> {
Ok(Self {
hashed_keys: FieldElement::from_byte_slice_be(&entity.hashed_keys)
.map_err(ClientError::SliceError)?,
hashed_keys: FieldElement::from_byte_slice_be(&entity.hashed_keys)?,
models: entity
.models
.into_iter()
Expand All @@ -35,7 +44,7 @@ impl TryFrom<proto::types::Entity> for Entity {
}

impl TryFrom<proto::types::Model> for Model {
type Error = ClientError;
type Error = SchemaError;
fn try_from(model: proto::types::Model) -> Result<Self, Self::Error> {
Ok(Self {
name: model.name,
Expand All @@ -49,7 +58,7 @@ impl TryFrom<proto::types::Model> for Model {
}

impl TryFrom<Ty> for proto::types::Ty {
type Error = ClientError;
type Error = SchemaError;
fn try_from(ty: Ty) -> Result<Self, Self::Error> {
let ty_type = match ty {
Ty::Primitive(primitive) => {
Expand All @@ -65,18 +74,18 @@ impl TryFrom<Ty> for proto::types::Ty {
}

impl TryFrom<proto::types::Member> for Member {
type Error = ClientError;
type Error = SchemaError;
fn try_from(member: proto::types::Member) -> Result<Self, Self::Error> {
Ok(Member {
name: member.name,
ty: member.ty.ok_or(ClientError::MissingExpectedData)?.try_into()?,
ty: member.ty.ok_or(SchemaError::MissingExpectedData)?.try_into()?,
key: member.key,
})
}
}

impl TryFrom<Member> for proto::types::Member {
type Error = ClientError;
type Error = SchemaError;
fn try_from(member: Member) -> Result<Self, Self::Error> {
Ok(proto::types::Member {
name: member.name,
Expand Down Expand Up @@ -119,7 +128,7 @@ impl From<Enum> for proto::types::Enum {
}

impl TryFrom<proto::types::Struct> for Struct {
type Error = ClientError;
type Error = SchemaError;
fn try_from(r#struct: proto::types::Struct) -> Result<Self, Self::Error> {
Ok(Struct {
name: r#struct.name,
Expand All @@ -133,7 +142,7 @@ impl TryFrom<proto::types::Struct> for Struct {
}

impl TryFrom<Struct> for proto::types::Struct {
type Error = ClientError;
type Error = SchemaError;
fn try_from(r#struct: Struct) -> Result<Self, Self::Error> {
Ok(proto::types::Struct {
name: r#struct.name,
Expand All @@ -147,7 +156,7 @@ impl TryFrom<Struct> for proto::types::Struct {
}

impl TryFrom<Struct> for proto::types::Model {
type Error = ClientError;
type Error = SchemaError;
fn try_from(r#struct: Struct) -> Result<Self, Self::Error> {
let r#struct: proto::types::Struct = r#struct.try_into()?;

Expand All @@ -167,14 +176,14 @@ impl From<proto::types::Struct> for proto::types::Model {
// warning.
#[allow(deprecated)]
impl TryFrom<proto::types::Primitive> for Primitive {
type Error = ClientError;
type Error = SchemaError;
fn try_from(primitive: proto::types::Primitive) -> Result<Self, Self::Error> {
let primitive_type = primitive.r#type;
let value_type = primitive
.value
.ok_or(ClientError::MissingExpectedData)?
.ok_or(SchemaError::MissingExpectedData)?
.value_type
.ok_or(ClientError::MissingExpectedData)?;
.ok_or(SchemaError::MissingExpectedData)?;

let primitive = match &value_type {
proto::types::value::ValueType::BoolValue(bool) => Primitive::Bool(Some(*bool)),
Expand All @@ -185,7 +194,7 @@ impl TryFrom<proto::types::Primitive> for Primitive {
Some(proto::types::PrimitiveType::U32) => Primitive::U32(Some(*int as u32)),
Some(proto::types::PrimitiveType::U64) => Primitive::U64(Some(*int)),
Some(proto::types::PrimitiveType::Usize) => Primitive::USize(Some(*int as u32)),
_ => return Err(ClientError::UnsupportedType),
_ => return Err(SchemaError::UnsupportedType),
}
}
proto::types::value::ValueType::ByteValue(bytes) => {
Expand All @@ -196,17 +205,17 @@ impl TryFrom<proto::types::Primitive> for Primitive {
| Some(proto::types::PrimitiveType::ContractAddress) => {
Primitive::Felt252(Some(
FieldElement::from_byte_slice_be(bytes)
.map_err(ClientError::SliceError)?,
.map_err(SchemaError::SliceError)?,
))
}
Some(proto::types::PrimitiveType::U256) => {
Primitive::U256(Some(U256::from_be_slice(bytes)))
}
_ => return Err(ClientError::UnsupportedType),
_ => return Err(SchemaError::UnsupportedType),
}
}
_ => {
return Err(ClientError::UnsupportedType);
return Err(SchemaError::UnsupportedType);
}
};

Expand All @@ -215,7 +224,7 @@ impl TryFrom<proto::types::Primitive> for Primitive {
}

impl TryFrom<Primitive> for proto::types::Primitive {
type Error = ClientError;
type Error = SchemaError;
fn try_from(primitive: Primitive) -> Result<Self, Self::Error> {
use proto::types::value::ValueType;

Expand Down Expand Up @@ -252,9 +261,9 @@ impl TryFrom<Primitive> for proto::types::Primitive {
}

impl TryFrom<proto::types::Ty> for Ty {
type Error = ClientError;
type Error = SchemaError;
fn try_from(ty: proto::types::Ty) -> Result<Self, Self::Error> {
match ty.ty_type.ok_or(ClientError::MissingExpectedData)? {
match ty.ty_type.ok_or(SchemaError::MissingExpectedData)? {
proto::types::ty::TyType::Primitive(primitive) => {
Ok(Ty::Primitive(primitive.try_into()?))
}
Expand Down

0 comments on commit 19fb8e9

Please sign in to comment.