Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce proc-macro dependencies #195

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ antlr-rust = "=0.3.0-beta"
better_any = "=0.2.0"
regex = "1"
yarnspinner_core = { path = "../core", version = "0.2" }
thiserror = "1"
strum = "0.26"
strum_macros = "0.26"
annotate-snippets = "0.10"
serde = { version = "1", features = ["derive"], optional = true }
bevy = { version = "0.13", default-features = false, optional = true }
Expand Down
12 changes: 11 additions & 1 deletion crates/compiler/src/listeners/error_listener/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, So
use antlr_rust::rule_context::CustomRuleContext;
use antlr_rust::token::Token;
use antlr_rust::token_factory::TokenFactory;
use core::fmt;
use std::fmt::{Display, Formatter};
use std::ops::Range;
use yarnspinner_core::prelude::*;
Expand Down Expand Up @@ -184,7 +185,7 @@ impl DiagnosticVec for Vec<Diagnostic> {
/// ## Implementation notes
///
/// The `Info` variant was not implemented because it was unused.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash, strum_macros::Display)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
#[cfg_attr(feature = "bevy", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bevy", reflect(Debug, PartialEq, Default, Hash))]
Expand All @@ -206,3 +207,12 @@ pub enum DiagnosticSeverity {
/// but do not cause the compilation process to fail.
Warning,
}

impl fmt::Display for DiagnosticSeverity {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
DiagnosticSeverity::Error => f.write_str("Error"),
DiagnosticSeverity::Warning => f.write_str("Warning"),
}
}
}
6 changes: 4 additions & 2 deletions crates/compiler/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::listeners::*;
pub use crate::output::{debug_info::*, declaration::*, string_info::*};
use crate::prelude::*;
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Debug, Display};
use thiserror::Error;
use yarnspinner_core::prelude::*;

mod debug_info;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Compilation {

/// A collection of [`Diagnostic`] objects that describe problems that occurred during compilation.
/// At least one of these diagnostics will have a severity of [`DiagnosticSeverity::Error`].
#[derive(Error, Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "bevy", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bevy", reflect(Debug, PartialEq, Hash))]
Expand All @@ -133,6 +133,8 @@ impl Compilation {
)]
pub struct CompilerError(pub Vec<Diagnostic>);

impl Error for CompilerError {}

impl Debug for CompilerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// This looks evil, but we support really nice error messages through Display,
Expand Down
5 changes: 0 additions & 5 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ serde = ["dep:serde", "bevy?/serialize"]
bevy = ["dep:bevy"]

[dependencies]
bytes = "1"
paste = "1"
yarnspinner_macros = { path = "../macros", version = "0.1" }
strum = "0.26"
strum_macros = "0.26"
thiserror = "1"
prost = "0.12"
serde = { version = "1", features = ["derive"], optional = true }
bevy = { version = "0.13", default-features = false, optional = true }
6 changes: 4 additions & 2 deletions crates/core/src/generated/ext.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Contains extensions to generated types that in the original implementation are sprinkled around the repo via partial classes

use crate::prelude::*;
use std::error::Error;
use std::fmt::{Debug, Display};
use thiserror::Error;

impl From<String> for Operand {
fn from(s: String) -> Self {
Expand Down Expand Up @@ -94,7 +94,7 @@ impl From<Operand> for YarnValue {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bevy", reflect(Debug, PartialEq))]
Expand All @@ -104,6 +104,8 @@ impl From<Operand> for YarnValue {
)]
pub struct InvalidOpCodeError(pub i32);

impl Error for InvalidOpCodeError {}

impl Display for InvalidOpCodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?} is not a valid OpCode", self.0)
Expand Down
26 changes: 25 additions & 1 deletion crates/core/src/operator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[cfg(any(feature = "bevy", feature = "serde"))]
use crate::prelude::*;
use core::fmt;
use std::borrow::Cow;

/// The available operators that can be used with Yarn values.
#[derive(Debug, Clone, Copy, PartialEq, Eq, strum_macros::Display)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bevy", reflect(Debug, PartialEq))]
Expand Down Expand Up @@ -83,6 +84,29 @@ pub enum Operator {
Modulo,
}

impl fmt::Display for Operator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Operator::EqualTo => f.write_str("EqualTo"),
Operator::GreaterThan => f.write_str("GreaterThan"),
Operator::GreaterThanOrEqualTo => f.write_str("GreaterThanOrEqualTo"),
Operator::LessThan => f.write_str("LessThan"),
Operator::LessThanOrEqualTo => f.write_str("LessThanOrEqualTo"),
Operator::NotEqualTo => f.write_str("NotEqualTo"),
Operator::Or => f.write_str("Or"),
Operator::And => f.write_str("And"),
Operator::Xor => f.write_str("Xor"),
Operator::Not => f.write_str("Not"),
Operator::UnarySubtract => f.write_str("UnarySubtract"),
Operator::Add => f.write_str("Add"),
Operator::Subtract => f.write_str("Subtract"),
Operator::Multiply => f.write_str("Multiply"),
Operator::Divide => f.write_str("Divide"),
Operator::Modulo => f.write_str("Modulo"),
}
}
}

/// Implementing this is probably bad practice, but this greatly reduces boilerplate when used with `yarn_fn_registry!`
impl From<Operator> for Cow<'static, str> {
fn from(value: Operator) -> Self {
Expand Down
78 changes: 50 additions & 28 deletions crates/core/src/types/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use crate::types::boolean::boolean_type_properties;
use crate::types::number::number_type_properties;
use crate::types::string::string_type_properties;
use crate::types::*;
use paste::paste;
use std::any::TypeId;
use std::error::Error;
use std::fmt::{Debug, Display};
use thiserror::Error;

/// All types in the virtual machine, both built-in, i.e. usable in Yarn scripts, and internal.
///
Expand Down Expand Up @@ -169,33 +168,30 @@ impl TypeProperties {
// on [`Type`] results in more compile-time safety.

macro_rules! impl_type {
($($yarn_type:pat => [$($base_type:path,)*] ,)*) => {
($($yarn_type:expr => [$($ext:ident for $base_type:path,)*] ,)*) => {
$(
$(

paste! {
/// Convenience trait for getting a [`Type`] out of a base type.
#[allow(non_camel_case_types)]
pub trait [<$base_type Ext>] {
/// Get the corresponding [`Type`]
fn r#type() -> Type;
}
impl [<$base_type Ext>] for $base_type {
fn r#type() -> Type {
$yarn_type
}
/// 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 {
$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 {
$yarn_type
}
}
)*
Expand All @@ -204,9 +200,24 @@ macro_rules! impl_type {
}

impl_type! {
Type::Number => [f32, f64, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize,],
Type::String => [String,],
Type::Boolean => [bool,],
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 {
Expand Down Expand Up @@ -262,10 +273,21 @@ impl From<&YarnValue> for Type {
}
}

#[derive(Error, Debug)]
#[derive(Debug)]
/// Represents a failure to dynamically convert a [`TypeId`] to a [`Type`].
#[allow(missing_docs)]
pub enum InvalidDowncastError {
#[error("Cannot convert TypeId {:?} to a Yarn Spinner `Type`", .0)]
InvalidTypeId(TypeId),
}

impl Error for InvalidDowncastError {}

impl Display for InvalidDowncastError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InvalidDowncastError::InvalidTypeId(id) => {
write!(f, "Cannot convert TypeId {id:?} to a Yarn Spinner `Type`")
}
}
}
}
51 changes: 43 additions & 8 deletions crates/core/src/yarn_value.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Implements a subset of dotnet's [`Convert`](https://learn.microsoft.com/en-us/dotnet/api/system.convert?view=net-8.0) type.
#[cfg(any(feature = "bevy", feature = "serde"))]
use crate::prelude::*;
use std::error::Error;
use std::fmt::{Display, Formatter};
use thiserror::Error;

/// Represents a Yarn value. The chosen variant corresponds to the last assignment of the value,
/// with the type being inferred from the type checker.
Expand Down Expand Up @@ -205,15 +205,50 @@ impl IntoYarnValueFromNonYarnValue for bool {
}

/// Represents a failure to convert one variant of [`YarnValue`] to a base type.
#[derive(Error, Debug)]
#[derive(Debug)]
#[allow(missing_docs)]
pub enum YarnValueCastError {
#[error(transparent)]
ParseFloatError(#[from] std::num::ParseFloatError),
#[error(transparent)]
ParseIntError(#[from] std::num::ParseIntError),
#[error(transparent)]
ParseBoolError(#[from] std::str::ParseBoolError),
ParseFloatError(std::num::ParseFloatError),
ParseIntError(std::num::ParseIntError),
ParseBoolError(std::str::ParseBoolError),
}

impl Error for YarnValueCastError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
YarnValueCastError::ParseFloatError(e) => Some(e),
YarnValueCastError::ParseIntError(e) => Some(e),
YarnValueCastError::ParseBoolError(e) => Some(e),
}
}
}

impl Display for YarnValueCastError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
YarnValueCastError::ParseFloatError(e) => Display::fmt(e, f),
YarnValueCastError::ParseIntError(e) => Display::fmt(e, f),
YarnValueCastError::ParseBoolError(e) => Display::fmt(e, f),
}
}
}

impl From<std::num::ParseFloatError> for YarnValueCastError {
fn from(value: std::num::ParseFloatError) -> Self {
Self::ParseFloatError(value)
}
}

impl From<std::num::ParseIntError> for YarnValueCastError {
fn from(value: std::num::ParseIntError) -> Self {
Self::ParseIntError(value)
}
}

impl From<std::str::ParseBoolError> for YarnValueCastError {
fn from(value: std::str::ParseBoolError) -> Self {
Self::ParseBoolError(value)
}
}

impl Display for YarnValue {
Expand Down
1 change: 0 additions & 1 deletion crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ icu_locid = { version = "1", features = ["std"] }
fixed_decimal = { version = "0.5", features = ["ryu", "std"] }
once_cell = "1"
regex = "1"
thiserror = "1"
serde = { version = "1", features = ["derive"], optional = true }
bevy = { version = "0.13", default-features = false, optional = true }
Loading
Loading