From 5d0347af67db3b8d0e7b07a82d0652827fd9245f Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Thu, 21 Sep 2023 21:53:59 +0800 Subject: [PATCH] wip --- macros/Cargo.toml | 6 +++--- src/datatype/enum.rs | 8 +++++++ src/datatype/mod.rs | 22 +++++++++++++++++++ tests/deprecated.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++ tests/ts.rs | 39 ---------------------------------- 5 files changed, 83 insertions(+), 42 deletions(-) create mode 100644 tests/deprecated.rs diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 34d2861..3643060 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -10,14 +10,14 @@ documentation = "https://docs.rs/specta/latest/specta" keywords = ["async", "specta", "rspc", "typescript", "typesafe"] categories = ["web-programming", "asynchronous"] +[lib] +proc-macro = true + [features] serde = [] functions = [] export = [] -[lib] -proc-macro = true - [dependencies] Inflector = { version = "0.11.4", default-features = false } itertools = "0.10.5" diff --git a/src/datatype/enum.rs b/src/datatype/enum.rs index cedc147..a1e0e27 100644 --- a/src/datatype/enum.rs +++ b/src/datatype/enum.rs @@ -72,6 +72,14 @@ pub enum EnumRepr { }, } +// TODO: Refactor into this instead +// pub struct EnumVariant { +// name: Cow<'static, str>, +// deprecated: Option, +// comments: Vec>, +// variant: EnumVariantTy, +// } + /// Type of an [`EnumType`] variant. #[derive(Debug, Clone, PartialEq)] pub enum EnumVariant { diff --git a/src/datatype/mod.rs b/src/datatype/mod.rs index 3701361..4c63b95 100644 --- a/src/datatype/mod.rs +++ b/src/datatype/mod.rs @@ -100,6 +100,28 @@ impl DataTypeReference { } } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum DeprecatedType { + /// A type that has been deprecated without a message. + /// + /// Eg. `#[deprecated]` + Deprecated, + /// A type that has been deprecated with a message. + /// + /// Eg. `#[deprecated = "Use something else"]` + #[non_exhaustive] + DeprecatedWithMsg { message: Cow<'static, str> }, + /// A type that has been deprecated with a message and a `since` version. + /// + /// Eg. `#[deprecated(since = "1.0.0", message = "Use something else")]` + #[non_exhaustive] + DeprecatedWithSince { + since: Option>, + message: Option>, + }, +} + /// A generic ("placeholder") argument to a Specta-enabled type. /// /// A generic does not hold a specific type instead it acts as a slot where a type can be provided when referencing this type. diff --git a/tests/deprecated.rs b/tests/deprecated.rs new file mode 100644 index 0000000..424363d --- /dev/null +++ b/tests/deprecated.rs @@ -0,0 +1,50 @@ +#[allow(deprecated)] +#[derive(Type)] +#[specta(export = false)] +#[deprecated] +struct DeprecatedType { + a: i32, +} + +#[derive(Type)] +#[specta(export = false)] +#[deprecated = "Look at you big man using a deprecation message"] +struct DeprecatedTypeWithMsg { + a: i32, +} + +#[derive(Type)] +#[specta(export = false)] +#[deprecated(note = "Look at you big man using a deprecation message")] +struct DeprecatedTypeWithMsg2 { + a: i32, +} + +#[derive(Type)] +#[specta(export = false)] +struct DeprecatedFields { + a: i32, + #[deprecated] + b: String, + #[deprecated = "This field is cringe!"] + c: String, + #[deprecated(note = "This field is cringe!")] + d: String, +} + +#[derive(Type)] +#[specta(export = false)] +pub struct DeprecatedTupleVariant( + #[deprecated] String, + #[deprecated = "Nope"] String, + #[deprecated(note = "Nope")] i32, +); + +#[test] +fn test_deprecated_types() { + assert_ts_export!(DeprecatedType, ""); + assert_ts_export!(DeprecatedTypeWithMsg, ""); + assert_ts_export!(DeprecatedTypeWithMsg2, ""); + assert_ts_export!(DeprecatedFields, ""); + assert_ts_export!(DeprecatedTupleVariant, ""); +} diff --git a/tests/ts.rs b/tests/ts.rs index 9516147..220b7bf 100644 --- a/tests/ts.rs +++ b/tests/ts.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - use std::{ cell::RefCell, collections::HashMap, @@ -249,10 +247,6 @@ fn typescript_types() { MyEmptyInput, "export type MyEmptyInput = Record" ); - - // assert_ts_export!(DeprecatedType, ""); - // assert_ts_export!(DeprecatedTypeWithMsg, ""); - // assert_ts_export!(DeprecatedFields, ""); } #[derive(Type)] @@ -537,36 +531,3 @@ pub struct EnumReferenceRecordKey { #[serde(rename_all = "camelCase")] #[serde(default)] pub(super) struct MyEmptyInput {} - -// #[derive(Type)] -// #[specta(export = false)] -// #[deprecated] -// struct DeprecatedType { -// a: i32, -// } - -// #[derive(Type)] -// #[specta(export = false)] -// #[deprecated = "Look at you big man using a deprecation message"] -// struct DeprecatedTypeWithMsg { -// a: i32, -// } - -// #[derive(Type)] -// #[specta(export = false)] -// #[deprecated(note = "Look at you big man using a deprecation message")] -// struct DeprecatedTypeWithMsg2 { -// a: i32, -// } - -// #[derive(Type)] -// #[specta(export = false)] -// struct DeprecatedFields { -// a: i32, -// // #[deprecated] -// b: String, -// #[deprecated = "This field is cringe!"] -// c: String, -// #[deprecated(note = "This field is cringe!")] -// d: String, -// }