Skip to content

Commit

Permalink
Replace extension traits and From impls with one coherent `TypedValue…
Browse files Browse the repository at this point in the history
…` trait
  • Loading branch information
bash committed Jun 10, 2024
1 parent 8713a48 commit 9ddd2ba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 61 deletions.
10 changes: 5 additions & 5 deletions crates/core/src/internal_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Adapted from <https://github.com/YarnSpinnerTool/YarnSpinner/blob/da39c7195107d8211f21c263e4084f773b84eaff/YarnSpinner/Value.cs>
use crate::prelude::*;
use crate::types::Type;
use crate::types::{Type, TypedValue as _};

/// A value as it appears to the compiler. It has additional type checker information
/// and may represent values not constructable by the user, like functions.
Expand Down Expand Up @@ -32,7 +32,7 @@ macro_rules! impl_from {
impl From<$from_type> for InternalValue {
fn from(value: $from_type) -> Self {
Self {
r#type: (&value).into(),
r#type: value.r#type(),
raw_value: value.into(),
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ impl_from![bool, f32, f64, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usi
impl From<&str> for InternalValue {
fn from(value: &str) -> Self {
Self {
r#type: value.into(),
r#type: value.r#type(),
raw_value: value.into(),
}
}
Expand All @@ -76,7 +76,7 @@ impl From<&str> for InternalValue {
impl From<String> for InternalValue {
fn from(value: String) -> Self {
Self {
r#type: (&value).into(),
r#type: value.r#type(),
raw_value: value.into(),
}
}
Expand All @@ -91,7 +91,7 @@ impl From<InternalValue> for String {
impl From<YarnValue> for InternalValue {
fn from(value: YarnValue) -> Self {
Self {
r#type: (&value).into(),
r#type: value.r#type(),
raw_value: value,
}
}
Expand Down
73 changes: 17 additions & 56 deletions crates/core/src/types/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,30 +167,18 @@ impl TypeProperties {
// The following is implemented on [`Types`] in the original implementation, but implementing it
// on [`Type`] results in more compile-time safety.

macro_rules! impl_type {
($($yarn_type:expr => [$($ext:ident for $base_type:path,)*] ,)*) => {
/// A trait that assigns Rust values a Yarn [`Type`].
pub trait TypedValue {
#[allow(missing_docs)]
fn r#type(&self) -> Type;
}

macro_rules! impl_typed_value {
($([$($rust_type:ty),* $(,)?] => $yarn_type:expr), *$(,)?) => {
$(
$(
/// Convenience trait for getting a [`Type`] out of a base type.
#[allow(non_camel_case_types)]
pub trait $ext {
/// Get the corresponding [`Type`]
fn r#type() -> Type;
}
impl $ext for $base_type {
fn r#type() -> Type {
$yarn_type
}
}

impl From<&$base_type> for Type {
fn from(_value: &$base_type) -> Self {
$yarn_type
}
}

impl From<$base_type> for Type {
fn from(_value: $base_type) -> Self {
impl TypedValue for $rust_type {
fn r#type(&self) -> Type {
$yarn_type
}
}
Expand All @@ -199,31 +187,10 @@ macro_rules! impl_type {
};
}

impl_type! {
Type::Number => [
f32Ext for f32,
f64Ext for f64,
i8Ext for i8,
i16Ext for i16,
i32Ext for i32,
i64Ext for i64,
i128Ext for i128,
u8Ext for u8,
u16Ext for u16,
u32Ext for u32,
u64Ext for u64,
u128Ext for u128,
usizeExt for usize,
isizeExt for isize,
],
Type::String => [StringExt for String,],
Type::Boolean => [boolExt for bool,],
}

impl From<&str> for Type {
fn from(_value: &str) -> Self {
Type::String
}
impl_typed_value! {
[f32, f64, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize] => Type::Number,
[String, str] => Type::String,
[bool] => Type::Boolean,
}

macro_rules! type_ids {
Expand Down Expand Up @@ -257,15 +224,9 @@ impl TryFrom<TypeId> for Type {
}
}

impl From<YarnValue> for Type {
fn from(value: YarnValue) -> Self {
Self::from(&value)
}
}

impl From<&YarnValue> for Type {
fn from(value: &YarnValue) -> Self {
match value {
impl TypedValue for YarnValue {
fn r#type(&self) -> Type {
match self {
YarnValue::Number(_) => Type::Number,
YarnValue::String(_) => Type::String,
YarnValue::Boolean(_) => Type::Boolean,
Expand Down

0 comments on commit 9ddd2ba

Please sign in to comment.