From 3bf21a160599151ef0acbeb3a33e6976c137c42a Mon Sep 17 00:00:00 2001 From: Haris <4259838+Wulf@users.noreply.github.com> Date: Mon, 7 Aug 2023 00:23:03 -0400 Subject: [PATCH] Minor updates to enum tests --- test/enum/rust.rs | 57 ++++++++++++++++++---------------- test/enum/typescript.d.ts | 65 +++++++++++++++++++++++---------------- test/enum/typescript.ts | 65 +++++++++++++++++++++++---------------- 3 files changed, 108 insertions(+), 79 deletions(-) diff --git a/test/enum/rust.rs b/test/enum/rust.rs index 1ad84c7..b75ba04 100644 --- a/test/enum/rust.rs +++ b/test/enum/rust.rs @@ -1,41 +1,43 @@ /// test/rust.rs use tsync::tsync; +/// Internally tagged enums have a key-value pair +/// that discrimate which variant it belongs to #[derive(Serialize, Deserialize)] +#[serde(tag = "typetypetype")] #[serde(renameAll = "kebab-case")] -#[serde(tag = "last_precedent")] +#[serde(tag = "type")] #[tsync] -enum Message { - /// Per Enum case Docs One - UnitCaseLeft, - /// Per Enum case Docs Two - RequestLongTake { - id: String, - method: String, - params: i32, - }, - Response(Response), +enum InternalTopping { + /// Tasty! + /// Not vegetarian + Pepperoni, + /// For cheese lovers + ExtraCheese { kind: String }, + /// Custom toppings + /// May expire soon + Custom(CustomTopping), } -/// The default enum conversion uses external tagging +/// Externally tagged enums ascribe the value to a key +/// that is the same as the variant name #[tsync] -enum ExternalMessage { - /// Per Enum case Docs One - UnitCaseLeft, - /// Per Enum case Docs Two - RequestLongTake { - id: String, - method: String, - params: i32, - }, - /// Newtype variant with exactly one variable - Response(Response), +enum ExternalTopping { + /// Tasty! + /// Not vegetarian + Pepperoni, + /// For cheese lovers + ExtraCheese { kind: String }, + /// Custom toppings + /// May expire soon + /// Note: this test case is specifically for specifying a single type in the tuple + Custom(CustomTopping), } #[tsync] -struct Response { - id: String, - result: NaiveDateTime, +struct CustomTopping { + name: String, + expires_in: NaiveDateTime, } /// All Unit Enums go to union of constant strings @@ -46,6 +48,7 @@ enum Animal { Dog, Cat, } + #[tsync] #[serde(rename_all = "snake_case")] enum AnimalTwo { @@ -60,4 +63,4 @@ enum Foo { Bar, // 0 Baz = 123, // 123 Quux, // 124 -} \ No newline at end of file +} diff --git a/test/enum/typescript.d.ts b/test/enum/typescript.d.ts index f9c9bf5..f887910 100644 --- a/test/enum/typescript.d.ts +++ b/test/enum/typescript.d.ts @@ -1,41 +1,54 @@ /* This file is generated and managed by tsync */ -type Message = - | Message__UnitCaseLeft - | Message__RequestLongTake; +/** + * Internally tagged enums have a key-value pair + * that discrimate which variant it belongs to + */ +type InternalTopping = + | InternalTopping__Pepperoni + | InternalTopping__ExtraCheese; -/** Per Enum case Docs One */ -type Message__UnitCaseLeft = { - last_precedent: "UnitCaseLeft"; +/** + * Tasty! + * Not vegetarian + */ +type InternalTopping__Pepperoni = { + type: "Pepperoni"; }; -/** Per Enum case Docs Two */ -type Message__RequestLongTake = { - last_precedent: "RequestLongTake"; - id: string; - method: string; - params: number; +/** For cheese lovers */ +type InternalTopping__ExtraCheese = { + type: "ExtraCheese"; + kind: string; }; -/** The default enum conversion uses external tagging */ -type ExternalMessage = - /** Per Enum case Docs One */ +/** + * Externally tagged enums ascribe the value to a key + * that is the same as the variant name + */ +type ExternalTopping = + /** + * Tasty! + * Not vegetarian + */ | { - "UnitCaseLeft": {} + "Pepperoni": {} } - /** Per Enum case Docs Two */ + /** For cheese lovers */ | { - "RequestLongTake": { - id: string; - method: string; - params: number; + "ExtraCheese": { + kind: string; } } - /** Newtype variant with exactly one variable */ - | { "Response": Response }; + /** + * Custom toppings + * May expire soon + * Note: this test case is specifically for specifying a single type in the tuple + */ + | { "Custom": CustomTopping }; -interface Response { - id: string; - result: Date; +interface CustomTopping { + name: string; + expires_in: Date; } /** diff --git a/test/enum/typescript.ts b/test/enum/typescript.ts index ada5695..ae7468c 100644 --- a/test/enum/typescript.ts +++ b/test/enum/typescript.ts @@ -1,41 +1,54 @@ /* This file is generated and managed by tsync */ -export type Message = - | Message__UnitCaseLeft - | Message__RequestLongTake; +/** + * Internally tagged enums have a key-value pair + * that discrimate which variant it belongs to + */ +export type InternalTopping = + | InternalTopping__Pepperoni + | InternalTopping__ExtraCheese; -/** Per Enum case Docs One */ -type Message__UnitCaseLeft = { - last_precedent: "UnitCaseLeft"; +/** + * Tasty! + * Not vegetarian + */ +type InternalTopping__Pepperoni = { + type: "Pepperoni"; }; -/** Per Enum case Docs Two */ -type Message__RequestLongTake = { - last_precedent: "RequestLongTake"; - id: string; - method: string; - params: number; +/** For cheese lovers */ +type InternalTopping__ExtraCheese = { + type: "ExtraCheese"; + kind: string; }; -/** The default enum conversion uses external tagging */ -export type ExternalMessage = - /** Per Enum case Docs One */ +/** + * Externally tagged enums ascribe the value to a key + * that is the same as the variant name + */ +export type ExternalTopping = + /** + * Tasty! + * Not vegetarian + */ | { - "UnitCaseLeft": {} + "Pepperoni": {} } - /** Per Enum case Docs Two */ + /** For cheese lovers */ | { - "RequestLongTake": { - id: string; - method: string; - params: number; + "ExtraCheese": { + kind: string; } } - /** Newtype variant with exactly one variable */ - | { "Response": Response }; + /** + * Custom toppings + * May expire soon + * Note: this test case is specifically for specifying a single type in the tuple + */ + | { "Custom": CustomTopping }; -export interface Response { - id: string; - result: Date; +export interface CustomTopping { + name: string; + expires_in: Date; } /**