Skip to content

Commit

Permalink
fix macro issue with serde
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistonight committed Jan 18, 2024
1 parent 0ef4bfe commit b38fd41
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 61 deletions.
4 changes: 1 addition & 3 deletions compiler-base/src/lang/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
use std::borrow::Cow;
use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::env;
use crate::lang::{self, DocPoorText};
use crate::macros::derive_wasm;

/// One diagnostic message
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct DocDiagnostic {
/// The diagnostic message. Poor text is used to automatically make links in the message
Expand Down
6 changes: 2 additions & 4 deletions compiler-base/src/lang/poor/poor_text.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
//! Implementations for extended utils of [`DocPoorText`]

use serde::{Deserialize, Serialize};

use crate::macros::derive_wasm;

/// Document poor text
///
/// This is a collection of [`DocPoorTextBlock`]s
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct DocPoorText(pub Vec<DocPoorTextBlock>);

/// Document poor text block. Just text or link
#[derive(PartialEq, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Debug, Clone)]
#[derive_wasm]
#[serde(tag = "type", content = "data")]
pub enum DocPoorTextBlock {
Expand Down
6 changes: 2 additions & 4 deletions compiler-base/src/lang/rich/rich_text.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::macros::derive_wasm;

/// Document rich text
///
/// This is a collection of [`DocRichTextBlock`]s
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct DocRichText(pub Vec<DocRichTextBlock>);

/// Document rich text block
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct DocRichTextBlock {
/// The tag name of the text
Expand Down
4 changes: 2 additions & 2 deletions compiler-base/src/util/string_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use std::collections::BTreeMap;
use std::ops::{Deref, DerefMut};

use serde::{Deserialize, Serialize};
use serde::Serialize;

use crate::macros::derive_wasm;

#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
#[repr(transparent)]
pub struct StringMap<T>(
Expand Down
4 changes: 1 addition & 3 deletions compiler-core/src/comp/line/marker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde::{Deserialize, Serialize};

use crate::json::{Cast, Coerce, SafeRouteBlob};
use crate::macros::derive_wasm;
use crate::prep::GameCoord;
Expand All @@ -8,7 +6,7 @@ use crate::prop;
use super::{CompError, LineContext};

/// Data of a marker specified by the `markers` property
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct CompMarker {
/// The coord of the marker
Expand Down
4 changes: 1 addition & 3 deletions compiler-core/src/comp/line/movement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde::{Deserialize, Serialize};

use crate::json::{Cast, Coerce, SafeRouteBlob, SafeRouteObject};
use crate::macros::derive_wasm;
use crate::prep::GameCoord;
Expand All @@ -8,7 +6,7 @@ use crate::prop;
use super::{CompError, LineContext};

/// Compiled map movement
#[derive(PartialEq, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Debug, Clone)]
#[derive_wasm]
#[serde(tag = "type")]
pub enum CompMovement {
Expand Down
4 changes: 1 addition & 3 deletions compiler-core/src/comp/line/note.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde::{Deserialize, Serialize};

use crate::comp::CompError;
use crate::json::{Cast, Coerce, SafeRouteBlob};
use crate::lang;
Expand All @@ -9,7 +7,7 @@ use crate::macros::derive_wasm;
use super::LineContext;

/// Document note block
#[derive(PartialEq, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Debug, Clone)]
#[derive_wasm]
#[serde(tag = "type")]
pub enum DocNote {
Expand Down
4 changes: 1 addition & 3 deletions compiler-core/src/exec/exec_doc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::borrow::Cow;

use serde::{Serialize, Deserialize};

use crate::macros::derive_wasm;
use crate::lang::{DocDiagnostic, DocRichText};
use crate::comp::CompDoc;
Expand All @@ -13,7 +11,7 @@ use super::{MapBuilder, ExecSection};
///
/// This is the final output of compiler with
/// map items separated from doc items
#[derive(Default, Serialize, Deserialize, Debug, Clone)]
#[derive(Default, Debug, Clone)]
#[derive_wasm]
pub struct ExecDoc<'p> {
/// Project metadata
Expand Down
4 changes: 1 addition & 3 deletions compiler-core/src/exec/exec_line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde::{Serialize, Deserialize};

use crate::macros::derive_wasm;
use crate::comp::{CompLine, CompMovement, DocNote};
use crate::prep::{GameCoord, RouteConfig};
Expand All @@ -8,7 +6,7 @@ use crate::lang::{DocRichText, DocRichTextBlock, DocDiagnostic, IntoDiagnostic};
use super::{MapBuilder, ExecError, MapIcon, MapMarker};

/// A line in the executed document
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct ExecLine {
/// Section number
Expand Down
5 changes: 1 addition & 4 deletions compiler-core/src/exec/exec_section.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

use serde::{Serialize, Deserialize};

use crate::macros::derive_wasm;
use crate::env::yield_budget;
use crate::comp::CompSection;
Expand All @@ -9,7 +6,7 @@ use crate::prep::RouteConfig;
use super::{MapSection, ExecLine, MapBuilder};

/// A section in the executed document
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct ExecSection {
/// Name of the section
Expand Down
10 changes: 4 additions & 6 deletions compiler-core/src/exec/map.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Convert declared movements and colors to lines

use serde::{Serialize, Deserialize};

use crate::macros::derive_wasm;
use crate::prep::GameCoord;

/// Map features for one section
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapSection {
/// The icons
Expand All @@ -18,7 +16,7 @@ pub struct MapSection {
}

/// Icon on the map
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapIcon {
/// Internal icon name (usually kebab-case)
Expand All @@ -34,7 +32,7 @@ pub struct MapIcon {
}

/// Markers on the map
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapMarker {
pub coord: GameCoord,
Expand All @@ -50,7 +48,7 @@ pub struct MapMarker {
///
/// The coordinates do not have to be on the same map layer.
/// The map will automatically split the path if it croses map layers.
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapLine {
/// Color of the line
Expand Down
6 changes: 2 additions & 4 deletions compiler-core/src/prep/config/map/coord_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! Packs map.coord-map into [`MapCoordMap`]

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::macros::derive_wasm;
Expand All @@ -13,7 +11,7 @@ use crate::prop;
/// to the x (horizontal) and z (height) axis of the map.
///
/// Default value of 0 will be assigned to the unmapped axis.
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapCoordMap {
/// Mapping for 2d coordinates in the route.
Expand All @@ -25,7 +23,7 @@ pub struct MapCoordMap {
}

/// Axis of the map
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub enum Axis {
/// Horizontal axis
Expand Down
7 changes: 3 additions & 4 deletions compiler-core/src/prep/config/map/layer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Packs json blob into [`MapLayerAttr`]

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::json::{Cast, Coerce};
Expand All @@ -11,7 +10,7 @@ use crate::prop;
// use super::{ConfigTrace, PrepError, PackerResult};

/// Attribute (definition) of a map layer in the route
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapLayer {
/// Display name of the layer
Expand Down Expand Up @@ -57,7 +56,7 @@ pub struct MapLayer {
/// ```no-compile
/// (x, y) -> (x * scale[0] + translate[0], y * scale[1] + translate[1])
/// ```
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapTilesetTransform {
/// The scale of the transformation
Expand All @@ -69,7 +68,7 @@ pub struct MapTilesetTransform {
/// Attribution to display on the map
///
/// (displayed as &copy; LINK)
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapAttribution {
/// Url of the attribution
Expand Down
5 changes: 2 additions & 3 deletions compiler-core/src/prep/config/map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Process the `map` config property

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::macros::derive_wasm;
Expand All @@ -18,7 +17,7 @@ pub use layer::*;
/// Metadata of the map
///
/// This includes configuration like map layers, coordinates, etc.
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct MapMetadata {
/// The map layers. First is the lowest layer.
Expand All @@ -34,7 +33,7 @@ pub struct MapMetadata {
}

/// Coordinates representing a point (x, y, z) in the game
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct GameCoord(pub f64, pub f64, pub f64);

Expand Down
5 changes: 2 additions & 3 deletions compiler-core/src/prep/config/tag.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Process the `tags` property

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::env::yield_budget;
Expand Down Expand Up @@ -66,7 +65,7 @@ impl<'a> PreparedConfig<'a> {
/// Document tag type
///
/// Used to style text and provide extra function to the engine
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct DocTag {
/// Bold style
Expand Down Expand Up @@ -126,7 +125,7 @@ impl DocTag {
}

/// Used to specify color for [`DocTag`]s.
#[derive(PartialEq, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Debug, Clone)]
#[derive_wasm]
#[serde(untagged)]
pub enum DocTagColor {
Expand Down
5 changes: 2 additions & 3 deletions compiler-core/src/prep/entry_point.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::borrow::Cow;
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::json::{Coerce, Cast};
Expand All @@ -13,7 +12,7 @@ use crate::env::yield_budget;
use super::{PrepError, PrepResult, Setting};

/// Compiler entry points (name, path) pairs
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct EntryPoints(pub StringMap<String>);

Expand All @@ -37,7 +36,7 @@ impl EntryPoints {
}
}

#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct EntryPointsSorted(pub Vec<(String, String)>);

Expand Down
5 changes: 2 additions & 3 deletions compiler-core/src/prep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::collections::BTreeMap;

use derivative::Derivative;
use instant::Instant;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::env::join_futures;
Expand Down Expand Up @@ -66,7 +65,7 @@ pub enum PrepDoc {
}

/// Config of the route project
#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct RouteConfig {
#[serde(flatten)]
Expand All @@ -83,7 +82,7 @@ pub struct RouteConfig {
pub splits: Vec<String>,
}

#[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq, Default, Debug, Clone)]
#[derive_wasm]
pub struct RouteMetadata {
/// Source of the route, could be a URL or any string
Expand Down
2 changes: 2 additions & 0 deletions compiler-macros/src/derive_wasm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ pub fn expand(input: TokenStream) -> TokenStream {
#[automatically_derived]
mod #derive_wasm_mod {
use super::*;
use serde::{Serialize, Deserialize};
#cfg_feature_wasm
use #wasm::{tsify, wasm_bindgen, serde_wasm_bindgen};

#[derive(serde::Serialize, serde::Deserialize)]
#derive_tsify
#[serde(rename_all(serialize = "camelCase", deserialize = "kebab-case"))]
#parsed
Expand Down
6 changes: 3 additions & 3 deletions compiler-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ mod async_impl;
/// A wrapper to add WASM support to a type so it can be send across the WASM ABI boundary.
/// All derived code/attributes are behind the `wasm` feature gate.
///
/// The target type should also derive `serde::Serialize` and `serde::Deserialize`.
/// `serde::Serialize` and `serde::Deserialize` will be automatically added
/// regardless of the `wasm` feature.
///
/// Under the hood, this uses `serde_wasm_bindgen` and `tsify` to generate the WASM ABI.
/// Make sure these two crates and the `tsify/js` feature is activated with the `wasm` feature.
Expand All @@ -71,9 +72,8 @@ mod async_impl;
///
/// # Example
/// ```ignore
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// #[derive(Debug, Clone)]
/// #[derive_wasm]
/// #[serde(rename_all = "camelCase")]
/// pub struct MyStruct {
/// pub a_thing: i32,
/// }
Expand Down
Loading

0 comments on commit b38fd41

Please sign in to comment.