Skip to content

Commit 4d6cb02

Browse files
authored
Custom typemapping (#73)
* add ts_type * Update test all
1 parent e38ff06 commit 4d6cb02

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

src/to_typescript/structs.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub fn process_fields<'a>(
100100
let has_flatten_attr = utils::get_attribute_arg("serde", "flatten", &field.attrs).is_some();
101101
let serde_skip = utils::get_attribute_arg("serde", "skip", &field.attrs).is_some();
102102
let serde_rename = utils::get_attribute_arg("serde", "rename", &field.attrs);
103+
let type_override = utils::get_attribute_arg("tsync", "ts_type", &field.attrs);
103104
if has_flatten_attr || serde_skip {
104105
continue;
105106
}
@@ -123,7 +124,12 @@ pub fn process_fields<'a>(
123124
field.ident.map(|i| i.unraw().to_string()).unwrap()
124125
};
125126

126-
let field_type = convert_type(&field.ty);
127+
let mut field_type = convert_type(&field.ty);
128+
129+
if let Some(type_override) = type_override {
130+
field_type.ts_type = type_override
131+
}
132+
127133
state.types.push_str(&format!(
128134
"{space}{field_name}{optional_parameter_token}: {field_type};\n",
129135
space = space,

test/issue-34/rust.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[tsync]
2+
struct SomeStruct {
3+
#[serde(skip)]
4+
foo: ThisIsARandomExternalType,
5+
#[tsync(ts_type = "any")]
6+
foo2: ThisIsARandomExternalType,
7+
}

test/issue-34/tsync.sh

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

test/issue-34/typescript.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* This file is generated and managed by tsync */
2+
3+
interface SomeStruct {
4+
foo2: any;
5+
}

test/issue-34/typescript.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* This file is generated and managed by tsync */
2+
3+
export interface SomeStruct {
4+
foo2: any;
5+
}

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-34/tsync.sh
1718
./issue-42-serde-rename/tsync.sh
1819
./issue-43/tsync.sh
1920
./issue-55/tsync.sh

0 commit comments

Comments
 (0)