|
| 1 | +use std::{borrow::Cow, fmt}; |
| 2 | + |
| 3 | +use super::{AccessError, ReflectPathError}; |
| 4 | +use crate::{Reflect, ReflectMut, ReflectRef, VariantType}; |
| 5 | +use thiserror::Error; |
| 6 | + |
| 7 | +type InnerResult<T> = Result<Option<T>, Error<'static>>; |
| 8 | + |
| 9 | +#[derive(Debug, PartialEq, Eq, Error)] |
| 10 | +pub(super) enum Error<'a> { |
| 11 | + #[error( |
| 12 | + "the current {ty} doesn't have the {} {}", |
| 13 | + access.kind(), |
| 14 | + access.display_value(), |
| 15 | + )] |
| 16 | + Access { ty: TypeShape, access: Access<'a> }, |
| 17 | + |
| 18 | + #[error("invalid type shape: expected {expected} but found a reflect {actual}")] |
| 19 | + Type { |
| 20 | + expected: TypeShape, |
| 21 | + actual: TypeShape, |
| 22 | + }, |
| 23 | + |
| 24 | + #[error("invalid enum access: expected {expected} variant but found {actual} variant")] |
| 25 | + Enum { |
| 26 | + expected: TypeShape, |
| 27 | + actual: TypeShape, |
| 28 | + }, |
| 29 | +} |
| 30 | + |
| 31 | +impl<'a> Error<'a> { |
| 32 | + fn with_offset(self, offset: usize) -> ReflectPathError<'a> { |
| 33 | + let error = AccessError(self); |
| 34 | + ReflectPathError::InvalidAccess { offset, error } |
| 35 | + } |
| 36 | + |
| 37 | + fn access(ty: TypeShape, access: Access<'a>) -> Self { |
| 38 | + Self::Access { ty, access } |
| 39 | + } |
| 40 | +} |
| 41 | +impl Error<'static> { |
| 42 | + fn bad_enum_variant(expected: TypeShape, actual: impl Into<TypeShape>) -> Self { |
| 43 | + let actual = actual.into(); |
| 44 | + Error::Enum { expected, actual } |
| 45 | + } |
| 46 | + fn bad_type(expected: TypeShape, actual: impl Into<TypeShape>) -> Self { |
| 47 | + let actual = actual.into(); |
| 48 | + Error::Type { expected, actual } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 53 | +pub(super) enum TypeShape { |
| 54 | + Struct, |
| 55 | + TupleStruct, |
| 56 | + Tuple, |
| 57 | + List, |
| 58 | + Array, |
| 59 | + Map, |
| 60 | + Enum, |
| 61 | + Value, |
| 62 | + Unit, |
| 63 | +} |
| 64 | + |
| 65 | +impl fmt::Display for TypeShape { |
| 66 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 67 | + let name = match self { |
| 68 | + TypeShape::Struct => "struct", |
| 69 | + TypeShape::TupleStruct => "tuple struct", |
| 70 | + TypeShape::Tuple => "tuple", |
| 71 | + TypeShape::List => "list", |
| 72 | + TypeShape::Array => "array", |
| 73 | + TypeShape::Map => "map", |
| 74 | + TypeShape::Enum => "enum", |
| 75 | + TypeShape::Value => "value", |
| 76 | + TypeShape::Unit => "unit", |
| 77 | + }; |
| 78 | + write!(f, "{name}") |
| 79 | + } |
| 80 | +} |
| 81 | +impl<'a> From<ReflectRef<'a>> for TypeShape { |
| 82 | + fn from(value: ReflectRef<'a>) -> Self { |
| 83 | + match value { |
| 84 | + ReflectRef::Struct(_) => TypeShape::Struct, |
| 85 | + ReflectRef::TupleStruct(_) => TypeShape::TupleStruct, |
| 86 | + ReflectRef::Tuple(_) => TypeShape::Tuple, |
| 87 | + ReflectRef::List(_) => TypeShape::List, |
| 88 | + ReflectRef::Array(_) => TypeShape::Array, |
| 89 | + ReflectRef::Map(_) => TypeShape::Map, |
| 90 | + ReflectRef::Enum(_) => TypeShape::Enum, |
| 91 | + ReflectRef::Value(_) => TypeShape::Value, |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +impl From<VariantType> for TypeShape { |
| 96 | + fn from(value: VariantType) -> Self { |
| 97 | + match value { |
| 98 | + VariantType::Struct => TypeShape::Struct, |
| 99 | + VariantType::Tuple => TypeShape::Tuple, |
| 100 | + VariantType::Unit => TypeShape::Unit, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// A singular element access within a path. |
| 106 | +/// |
| 107 | +/// Can be applied to a `dyn Reflect` to get a reference to the targeted element. |
| 108 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 109 | +pub(super) enum Access<'a> { |
| 110 | + Field(Cow<'a, str>), |
| 111 | + FieldIndex(usize), |
| 112 | + TupleIndex(usize), |
| 113 | + ListIndex(usize), |
| 114 | +} |
| 115 | + |
| 116 | +impl fmt::Display for Access<'_> { |
| 117 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 118 | + match self { |
| 119 | + Access::Field(field) => write!(f, ".{field}"), |
| 120 | + Access::FieldIndex(index) => write!(f, "#{index}"), |
| 121 | + Access::TupleIndex(index) => write!(f, ".{index}"), |
| 122 | + Access::ListIndex(index) => write!(f, "[{index}]"), |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<'a> Access<'a> { |
| 128 | + pub(super) fn into_owned(self) -> Access<'static> { |
| 129 | + match self { |
| 130 | + Self::Field(value) => Access::Field(value.to_string().into()), |
| 131 | + Self::FieldIndex(value) => Access::FieldIndex(value), |
| 132 | + Self::TupleIndex(value) => Access::TupleIndex(value), |
| 133 | + Self::ListIndex(value) => Access::ListIndex(value), |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + fn display_value(&self) -> &dyn fmt::Display { |
| 138 | + match self { |
| 139 | + Self::Field(value) => value, |
| 140 | + Self::FieldIndex(value) | Self::TupleIndex(value) | Self::ListIndex(value) => value, |
| 141 | + } |
| 142 | + } |
| 143 | + fn kind(&self) -> &'static str { |
| 144 | + match self { |
| 145 | + Self::Field(_) => "field", |
| 146 | + Self::FieldIndex(_) => "field index", |
| 147 | + Self::TupleIndex(_) | Self::ListIndex(_) => "index", |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + pub(super) fn element<'r>( |
| 152 | + &self, |
| 153 | + base: &'r dyn Reflect, |
| 154 | + offset: usize, |
| 155 | + ) -> Result<&'r dyn Reflect, ReflectPathError<'a>> { |
| 156 | + let ty = base.reflect_ref().into(); |
| 157 | + self.element_inner(base) |
| 158 | + .and_then(|maybe| maybe.ok_or(Error::access(ty, self.clone()))) |
| 159 | + .map_err(|err| err.with_offset(offset)) |
| 160 | + } |
| 161 | + |
| 162 | + fn element_inner<'r>(&self, base: &'r dyn Reflect) -> InnerResult<&'r dyn Reflect> { |
| 163 | + use ReflectRef::*; |
| 164 | + match (self, base.reflect_ref()) { |
| 165 | + (Self::Field(field), Struct(struct_ref)) => Ok(struct_ref.field(field.as_ref())), |
| 166 | + (Self::Field(field), Enum(enum_ref)) => match enum_ref.variant_type() { |
| 167 | + VariantType::Struct => Ok(enum_ref.field(field.as_ref())), |
| 168 | + actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)), |
| 169 | + }, |
| 170 | + (&Self::FieldIndex(index), Struct(struct_ref)) => Ok(struct_ref.field_at(index)), |
| 171 | + (&Self::FieldIndex(index), Enum(enum_ref)) => match enum_ref.variant_type() { |
| 172 | + VariantType::Struct => Ok(enum_ref.field_at(index)), |
| 173 | + actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)), |
| 174 | + }, |
| 175 | + (&Self::TupleIndex(index), TupleStruct(tuple)) => Ok(tuple.field(index)), |
| 176 | + (&Self::TupleIndex(index), Tuple(tuple)) => Ok(tuple.field(index)), |
| 177 | + (&Self::TupleIndex(index), Enum(enum_ref)) => match enum_ref.variant_type() { |
| 178 | + VariantType::Tuple => Ok(enum_ref.field_at(index)), |
| 179 | + actual => Err(Error::bad_enum_variant(TypeShape::Tuple, actual)), |
| 180 | + }, |
| 181 | + (&Self::ListIndex(index), List(list)) => Ok(list.get(index)), |
| 182 | + (&Self::ListIndex(index), Array(list)) => Ok(list.get(index)), |
| 183 | + (&Self::ListIndex(_), actual) => Err(Error::bad_type(TypeShape::List, actual)), |
| 184 | + (_, actual) => Err(Error::bad_type(TypeShape::Struct, actual)), |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + pub(super) fn element_mut<'r>( |
| 189 | + &self, |
| 190 | + base: &'r mut dyn Reflect, |
| 191 | + offset: usize, |
| 192 | + ) -> Result<&'r mut dyn Reflect, ReflectPathError<'a>> { |
| 193 | + let ty = base.reflect_ref().into(); |
| 194 | + self.element_inner_mut(base) |
| 195 | + .and_then(|maybe| maybe.ok_or(Error::access(ty, self.clone()))) |
| 196 | + .map_err(|err| err.with_offset(offset)) |
| 197 | + } |
| 198 | + |
| 199 | + fn element_inner_mut<'r>(&self, base: &'r mut dyn Reflect) -> InnerResult<&'r mut dyn Reflect> { |
| 200 | + use ReflectMut::*; |
| 201 | + let base_shape: TypeShape = base.reflect_ref().into(); |
| 202 | + match (self, base.reflect_mut()) { |
| 203 | + (Self::Field(field), Struct(struct_mut)) => Ok(struct_mut.field_mut(field.as_ref())), |
| 204 | + (Self::Field(field), Enum(enum_mut)) => match enum_mut.variant_type() { |
| 205 | + VariantType::Struct => Ok(enum_mut.field_mut(field.as_ref())), |
| 206 | + actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)), |
| 207 | + }, |
| 208 | + (&Self::FieldIndex(index), Struct(struct_mut)) => Ok(struct_mut.field_at_mut(index)), |
| 209 | + (&Self::FieldIndex(index), Enum(enum_mut)) => match enum_mut.variant_type() { |
| 210 | + VariantType::Struct => Ok(enum_mut.field_at_mut(index)), |
| 211 | + actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)), |
| 212 | + }, |
| 213 | + (&Self::TupleIndex(index), TupleStruct(tuple)) => Ok(tuple.field_mut(index)), |
| 214 | + (&Self::TupleIndex(index), Tuple(tuple)) => Ok(tuple.field_mut(index)), |
| 215 | + (&Self::TupleIndex(index), Enum(enum_mut)) => match enum_mut.variant_type() { |
| 216 | + VariantType::Tuple => Ok(enum_mut.field_at_mut(index)), |
| 217 | + actual => Err(Error::bad_enum_variant(TypeShape::Tuple, actual)), |
| 218 | + }, |
| 219 | + (&Self::ListIndex(index), List(list)) => Ok(list.get_mut(index)), |
| 220 | + (&Self::ListIndex(index), Array(list)) => Ok(list.get_mut(index)), |
| 221 | + (&Self::ListIndex(_), _) => Err(Error::bad_type(TypeShape::List, base_shape)), |
| 222 | + (_, _) => Err(Error::bad_type(TypeShape::Struct, base_shape)), |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments