Skip to content

Commit f60305e

Browse files
authored
feat: Honor serde rename and serde skip (#72)
1 parent a54c53b commit f60305e

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

src/to_typescript/structs.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,23 @@ pub fn process_fields<'a>(
9898

9999
// Check if the field has the serde flatten attribute, if so, skip it
100100
let has_flatten_attr = utils::get_attribute_arg("serde", "flatten", &field.attrs).is_some();
101-
if has_flatten_attr {
101+
let serde_skip = utils::get_attribute_arg("serde", "skip", &field.attrs).is_some();
102+
let serde_rename = utils::get_attribute_arg("serde", "rename", &field.attrs);
103+
if has_flatten_attr || serde_skip {
102104
continue;
103105
}
104106

105107
let comments = utils::get_comments(field.attrs);
106108

107109
state.write_comments(&comments, 2);
108-
let field_name = if let Some(name_case) = case {
110+
111+
let field_name = if let Some(field_name) = serde_rename {
112+
if field_name.contains(" ") {
113+
format!("\"{field_name}\"")
114+
} else {
115+
field_name
116+
}
117+
} else if let Some(name_case) = case {
109118
field
110119
.ident
111120
.map(|id| id.unraw().to_string().to_case(name_case))

test/issue-42-serde-rename/rust.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[tsync]
2+
struct TestThingey {
3+
#[serde(rename = "im_correct ")]
4+
not_me: String,
5+
how_bout_me: String,
6+
#[serde(skip)]
7+
skip_me: SuperSecretStructThatsInternal,
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
4+
5+
cd $SCRIPT_DIR
6+
7+
cargo run -- -i rust.rs -o typescript.d.ts
8+
cargo run -- -i rust.rs -o typescript.ts
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* This file is generated and managed by tsync */
2+
3+
interface TestThingey {
4+
"im_correct ": string;
5+
how_bout_me: string;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* This file is generated and managed by tsync */
2+
3+
export interface TestThingey {
4+
"im_correct ": string;
5+
how_bout_me: string;
6+
}

test/test_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cd $SCRIPT_DIR
1414
./enum_numeric/tsync.sh
1515
./doc_comments/tsync.sh
1616
./generic/tsync.sh
17+
./issue-42-serde-rename/tsync.sh
1718
./issue-43/tsync.sh
1819
./issue-55/tsync.sh
1920
./issue-58/tsync.sh

0 commit comments

Comments
 (0)