Skip to content

Commit

Permalink
Fix has_attribute (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wulf committed Sep 29, 2023
1 parent 2323480 commit f795311
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tsync"
description = "Generate typescript types from rust code."
version = "2.0.0"
version = "2.0.1"
readme = "README.md"
repository = "https://github.com/Wulf/tsync"
license = "MIT OR Apache-2.0"
Expand Down
36 changes: 35 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use quote::ToTokens;
use syn::{punctuated::Punctuated, Attribute, MetaNameValue, Token};
use syn::{punctuated::Punctuated, Attribute, ExprPath, MetaNameValue, Token};

pub fn has_attribute(needle: &str, attributes: &[syn::Attribute]) -> bool {
attributes.iter().any(|attr| {
Expand All @@ -11,6 +11,9 @@ pub fn has_attribute(needle: &str, attributes: &[syn::Attribute]) -> bool {
}

/// Get the value matching an attribute and argument combination
///
/// For #[serde(tag = "type")], get_attribute_arg("serde", "tag", attributes) will return Some("type")
/// For #[derive(Serialize_repr)], get_attribute_arg("derive", "Serialize_repr", attributes) will return Some("Serialize_repr")
pub fn get_attribute_arg(needle: &str, arg: &str, attributes: &[syn::Attribute]) -> Option<String> {
if let Some(attr) = get_attribute(needle, attributes) {
// check if attribute list contains the argument we are interested in
Expand Down Expand Up @@ -40,6 +43,37 @@ pub fn get_attribute_arg(needle: &str, arg: &str, attributes: &[syn::Attribute])
);

if name_value_pairs.is_err() {
let comma_seperated_values = ::syn::parse::Parser::parse2(
Punctuated::<syn::Expr, Token![,]>::parse_terminated,
group.stream(),
);

if comma_seperated_values.is_err() {
continue;
}

let comma_seperated_values = comma_seperated_values.unwrap();

for comma_seperated_value in comma_seperated_values {
match comma_seperated_value {
syn::Expr::Path(expr_path) => {
let segments = expr_path.path.segments;

if segments.is_empty() {
continue;
}

if segments[0].ident.to_string().eq(arg) {
found = true;
value = String::from(arg);

break;
}
}
_ => continue,
}
}

continue;
}

Expand Down
9 changes: 0 additions & 9 deletions test/enum/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,3 @@ enum AnimalTwo {
DogLongExtra = 2,
Cat,
}

/// Integer enums should follow rust discrimination if literals (doesn't evaluate expression)
#[derive(Serialize_repr)]
#[tsync]
enum Foo {
Bar, // 0
Baz = 123, // 123
Quux, // 124
}
4 changes: 0 additions & 4 deletions test/enum/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,3 @@ type Animal =

type AnimalTwo =
| "dog_long_extra" | "cat";

/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */
type Foo =
| "Bar" | "Baz" | "Quux";
4 changes: 0 additions & 4 deletions test/enum/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,3 @@ export type Animal =

export type AnimalTwo =
| "dog_long_extra" | "cat";

/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */
export type Foo =
| "Bar" | "Baz" | "Quux";
10 changes: 10 additions & 0 deletions test/enum_numeric/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use tsync::tsync;

/// Integer enums should follow rust discrimination if literals (doesn't evaluate expression)
#[derive(Serialize_repr)]
#[tsync]
enum Foo {
Bar, // 0
Baz = 123, // 123
Quux, // 124
}
8 changes: 8 additions & 0 deletions test/enum_numeric/tsync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

cd $SCRIPT_DIR

cargo run -- -i rust.rs -o typescript.d.ts
cargo run -- -i rust.rs -o typescript.ts
8 changes: 8 additions & 0 deletions test/enum_numeric/typescript.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This file is generated and managed by tsync */

/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */
declare enum Foo {
Bar = 0,
Baz = 123,
Quux = 124,
}
8 changes: 8 additions & 0 deletions test/enum_numeric/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This file is generated and managed by tsync */

/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */
export enum Foo {
Bar = 0,
Baz = 123,
Quux = 124,
}

0 comments on commit f795311

Please sign in to comment.