Skip to content

Commit 2e2b203

Browse files
[typeshare] Support linux-arm64 and support u64 (#1)
## Description * Switching build over to `zigbuild` to support linux arm64 variant * Porting over 1Password#140 to support large number * Adding some default overrides to include base types for stuff such as `HashSet` ## Test Plan * Typeshare built for repo ## Revert Plan * Revert
1 parent da98c89 commit 2e2b203

File tree

18 files changed

+190
-79
lines changed

18 files changed

+190
-79
lines changed

.github/workflows/release.yml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,23 @@ jobs:
8787
include:
8888
- os: ubuntu-20.04
8989
dist-args: --artifacts=global
90+
target: ''
9091
install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.4/cargo-dist-v0.0.4-installer.sh | sh
9192
- os: macos-11
9293
dist-args: --artifacts=local --target=aarch64-apple-darwin --target=x86_64-apple-darwin
94+
target: 'aarch64-apple-darwin x86_64-apple-darwin'
9395
install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.4/cargo-dist-v0.0.4-installer.sh | sh
9496
- os: ubuntu-20.04
9597
dist-args: --artifacts=local --target=x86_64-unknown-linux-gnu
98+
target: 'x86_64-unknown-linux-gnu'
9699
install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.4/cargo-dist-v0.0.4-installer.sh | sh
100+
- os: ubuntu-20.04
101+
dist-args: --artifacts=local --target=aarch64-unknown-linux-gnu
102+
target: 'aarch64-unknown-linux-gnu'
103+
install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.4/cargo-dist-v0.0.4-installer.sh | sh
97104
- os: windows-2019
98105
dist-args: --artifacts=local --target=x86_64-pc-windows-msvc
106+
target: 'x86_64-pc-windows-msvc'
99107
install-dist: irm https://github.com/axodotdev/cargo-dist/releases/download/v0.0.4/cargo-dist-v0.0.4-installer.ps1 | iex
100108

101109
runs-on: ${{ matrix.os }}
@@ -108,17 +116,36 @@ jobs:
108116
- name: Install cargo-dist
109117
run: ${{ matrix.install-dist }}
110118
- name: Run cargo-dist
119+
if: ${{ matrix.target != '' }}
111120
# This logic is a bit janky because it's trying to be a polyglot between
112121
# powershell and bash since this will run on windows, macos, and linux!
113122
# The two platforms don't agree on how to talk about env vars but they
114123
# do agree on 'cat' and '$()' so we use that to marshal values between commands.
115124
run: |
116-
# Actually do builds and make zips and whatnot
117-
cargo dist build --tag=${{ github.ref_name }} --output-format=json ${{ matrix.dist-args }} > dist-manifest.json
118-
echo "dist ran successfully"
125+
pip3 install ziglang
126+
cargo install cargo-zigbuild
127+
rustup target add ${{ matrix.target }}
128+
cargo zigbuild --target ${{ matrix.target }} --release
129+
130+
# Set binary name to typeshare
131+
BINARY_NAME="typeshare"
132+
TARGET_DIR="target/${{ matrix.target }}/release"
133+
134+
# Create zip directory
135+
mkdir -p "dist"
136+
137+
# Create zip file with binary
138+
ZIP_NAME="${BINARY_NAME}-${{ github.ref_name }}-${{ matrix.target }}.zip"
139+
cd ${TARGET_DIR} && zip "../../../dist/${ZIP_NAME}" "${BINARY_NAME}${BINARY_SUFFIX}"
140+
cd ../../..
141+
142+
# Create manifest file similar to cargo-dist
143+
echo "{\"artifacts\": [{\"path\": \"dist/${ZIP_NAME}\"}]}" > dist-manifest.json
144+
145+
echo "Build complete, contents of dist-manifest.json:"
119146
cat dist-manifest.json
120-
121-
# Parse out what we just built and upload it to the Github Release™
147+
148+
# Upload to release
122149
cat dist-manifest.json | jq --raw-output ".artifacts[]?.path | select( . != null )" > uploads.txt
123150
echo "uploading..."
124151
cat uploads.txt

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ci = ["github"]
1313
installers = ["shell", "powershell"]
1414
# Target platforms to build apps for (Rust target-triple syntax)
1515
targets = [
16+
"aarch64-unknown-linux-gnu",
1617
"x86_64-unknown-linux-gnu",
1718
"x86_64-apple-darwin",
1819
"x86_64-pc-windows-msvc",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[typeshare]
2+
struct DisallowedType {
3+
#[typeshare(typescript(type = "bigint"))]
4+
disallowed_type: u64,
5+
#[typeshare(typescript(type = "number"))]
6+
another_disallowed_type: i64,
7+
#[typeshare(typescript(type = "string"))]
8+
#[serde(with = "my_string_serde_impl")]
9+
disallowed_type_serde_with: u64,
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package proto
2+
3+
import "encoding/json"
4+
5+
type DisallowedType struct {
6+
DisallowedType uint64 `json:"disallowed_type"`
7+
AnotherDisallowedType int64 `json:"another_disallowed_type"`
8+
DisallowedTypeSerdeWith uint64 `json:"disallowed_type_serde_with"`
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.agilebits.onepassword
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.SerialName
5+
6+
@Serializable
7+
data class DisallowedType (
8+
val disallowed_type: ULong,
9+
val another_disallowed_type: Long,
10+
val disallowed_type_serde_with: ULong
11+
)
12+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.agilebits
2+
3+
package object onepassword {
4+
5+
type UByte = Byte
6+
type UShort = Short
7+
type UInt = Int
8+
type ULong = Int
9+
10+
}
11+
package onepassword {
12+
13+
case class DisallowedType (
14+
disallowed_type: ULong,
15+
another_disallowed_type: Long,
16+
disallowed_type_serde_with: ULong
17+
)
18+
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
3+
public struct DisallowedType: Codable {
4+
public let disallowed_type: UInt64
5+
public let another_disallowed_type: Int64
6+
public let disallowed_type_serde_with: UInt64
7+
8+
public init(disallowed_type: UInt64, another_disallowed_type: Int64, disallowed_type_serde_with: UInt64) {
9+
self.disallowed_type = disallowed_type
10+
self.another_disallowed_type = another_disallowed_type
11+
self.disallowed_type_serde_with = disallowed_type_serde_with
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface DisallowedType {
2+
disallowed_type: bigint;
3+
another_disallowed_type: number;
4+
disallowed_type_serde_with: string;
5+
}
6+

core/src/language/go.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ impl Language for Go {
148148
SpecialRustType::U32 => "uint32".into(),
149149
SpecialRustType::I54 | SpecialRustType::I64 => "int64".into(),
150150
SpecialRustType::U53 | SpecialRustType::U64 => "uint64".into(),
151+
SpecialRustType::U128 => "uint128".into(),
151152
SpecialRustType::Bool => "bool".into(),
152153
SpecialRustType::F32 => "float32".into(),
153154
SpecialRustType::F64 => "float64".into(),

core/src/language/kotlin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl Language for Kotlin {
9090
SpecialRustType::Bool => "Boolean".into(),
9191
SpecialRustType::F32 => "Float".into(),
9292
SpecialRustType::F64 => "Double".into(),
93+
SpecialRustType::U128 => "BigInteger".into(),
9394
})
9495
}
9596

core/src/language/scala.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl Language for Scala {
111111
SpecialRustType::Bool => "Boolean".into(),
112112
SpecialRustType::F32 => "Float".into(),
113113
SpecialRustType::F64 => "Double".into(),
114+
SpecialRustType::U128 => "BigInt".into(),
114115
})
115116
}
116117

core/src/language/swift.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl Language for Swift {
215215
SpecialRustType::Bool => "Bool".into(),
216216
SpecialRustType::F32 => "Float".into(),
217217
SpecialRustType::F64 => "Double".into(),
218+
SpecialRustType::U128 => "UInt128".into(),
218219
})
219220
}
220221

core/src/language/typescript.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ impl Language for TypeScript {
8080
SpecialRustType::U64
8181
| SpecialRustType::I64
8282
| SpecialRustType::ISize
83-
| SpecialRustType::USize => {
84-
panic!("64 bit types not allowed in Typeshare")
85-
}
83+
| SpecialRustType::U128
84+
| SpecialRustType::USize => Ok("number".into())
8685
}
8786
}
8887

@@ -178,8 +177,9 @@ impl Language for TypeScript {
178177
w: &mut dyn Write,
179178
imports: ScopedCrateTypes<'_>,
180179
) -> std::io::Result<()> {
180+
writeln!(w, "import type {{ HashSet }} from \"./base\";")?;
181181
for (path, ty) in imports {
182-
write!(w, "import {{ ")?;
182+
write!(w, "import type {{ ")?;
183183
let ty_list = ty.iter().join(", ");
184184
write!(w, "{ty_list}")?;
185185
writeln!(w, " }} from \"./{path}\";")?;

core/src/rust_types.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use log::debug;
12
use quote::ToTokens;
23
use std::collections::BTreeSet;
34
use std::str::FromStr;
@@ -206,6 +207,8 @@ pub enum SpecialRustType {
206207
I32,
207208
/// Represents `i64`
208209
I64,
210+
/// Represents `u128`
211+
U128,
209212
/// Represents `u8`
210213
U8,
211214
/// Represents `u16`
@@ -257,6 +260,7 @@ impl TryFrom<&syn::Type> for RustType {
257260
type Error = RustTypeParseError;
258261

259262
fn try_from(ty: &syn::Type) -> Result<Self, Self::Error> {
263+
debug!("Parsing type: {}", ty.to_token_stream());
260264
Ok(match ty {
261265
syn::Type::Tuple(tuple) if tuple.elems.iter().count() == 0 => {
262266
Self::Special(SpecialRustType::Unit)
@@ -305,12 +309,14 @@ impl TryFrom<&syn::Type> for RustType {
305309
"u16" => Self::Special(SpecialRustType::U16),
306310
"u32" => Self::Special(SpecialRustType::U32),
307311
"U53" => Self::Special(SpecialRustType::U53),
308-
"u64" | "i64" | "usize" | "isize" => {
309-
return Err(RustTypeParseError::UnsupportedType(vec![id]))
310-
}
312+
"u64" => Self::Special(SpecialRustType::U64),
313+
"usize" => Self::Special(SpecialRustType::USize),
314+
"u128" => Self::Special(SpecialRustType::U128),
311315
"i8" => Self::Special(SpecialRustType::I8),
312316
"i16" => Self::Special(SpecialRustType::I16),
313317
"i32" => Self::Special(SpecialRustType::I32),
318+
"i64" => Self::Special(SpecialRustType::I64),
319+
"isize" => Self::Special(SpecialRustType::ISize),
314320
"I54" => Self::Special(SpecialRustType::I54),
315321
"f32" => Self::Special(SpecialRustType::F32),
316322
"f64" => Self::Special(SpecialRustType::F64),
@@ -488,6 +494,7 @@ impl SpecialRustType {
488494
| Self::F32
489495
| Self::F64
490496
| Self::I54
497+
| Self::U128
491498
| Self::U53 => ty == self.id(),
492499
}
493500
}
@@ -518,6 +525,7 @@ impl SpecialRustType {
518525
Self::USize => "usize",
519526
Self::U53 => "U53",
520527
Self::I54 => "I54",
528+
Self::U128 => "u128",
521529
}
522530
}
523531
/// Iterate over the generic parameters for this type. Returns an empty iterator
@@ -541,6 +549,7 @@ impl SpecialRustType {
541549
| Self::U16
542550
| Self::U32
543551
| Self::U64
552+
| Self::U128
544553
| Self::ISize
545554
| Self::USize
546555
| Self::Bool

core/src/visitors.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,33 +265,38 @@ impl<'ast, 'a> Visit<'ast> for TypeShareVisitor<'a> {
265265

266266
/// Collect rust structs.
267267
fn visit_item_struct(&mut self, i: &'ast syn::ItemStruct) {
268-
debug!("Visiting {}", i.ident);
268+
debug!("Visiting item {}", i.ident);
269269
if has_typeshare_annotation(&i.attrs) && self.target_os_accepted(&i.attrs) {
270270
debug!("\tParsing {}", i.ident);
271271
self.collect_result(parse_struct(i, self.target_os));
272+
debug!("\tParsed {}", i.ident);
272273
}
273-
274+
debug!("Visited item {}", i.ident);
274275
syn::visit::visit_item_struct(self, i);
275276
}
276277

277278
/// Collect rust enums.
278279
fn visit_item_enum(&mut self, i: &'ast syn::ItemEnum) {
279-
debug!("Visiting {}", i.ident);
280+
debug!("Visiting enum {}", i.ident);
280281
if has_typeshare_annotation(&i.attrs) && self.target_os_accepted(&i.attrs) {
281282
debug!("\tParsing {}", i.ident);
282283
self.collect_result(parse_enum(i, self.target_os));
284+
debug!("\tParsed {}", i.ident);
283285
}
286+
debug!("Visited enum {}", i.ident);
284287

285288
syn::visit::visit_item_enum(self, i);
286289
}
287290

288291
/// Collect rust type aliases.
289292
fn visit_item_type(&mut self, i: &'ast syn::ItemType) {
290-
debug!("Visiting {}", i.ident);
293+
debug!("Visiting type {}", i.ident);
291294
if has_typeshare_annotation(&i.attrs) && self.target_os_accepted(&i.attrs) {
292295
debug!("\tParsing {}", i.ident);
293296
self.collect_result(parse_type_alias(i));
297+
debug!("\tParsed {}", i.ident);
294298
}
299+
debug!("Visited type {}", i.ident);
295300

296301
syn::visit::visit_item_type(self, i);
297302
}
@@ -308,9 +313,11 @@ impl<'ast, 'a> Visit<'ast> for TypeShareVisitor<'a> {
308313
// }
309314

310315
fn visit_file(&mut self, i: &'ast syn::File) {
316+
debug!("Visiting file {}", self.parsed_data.file_name);
311317
if self.target_os_accepted(&i.attrs) {
312318
syn::visit::visit_file(self, i);
313319
}
320+
debug!("Visited file {}", self.parsed_data.file_name);
314321
}
315322
}
316323

core/tests/agnostic_tests.rs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::io::Write;
22
use typeshare_core::{
33
language::{CrateTypes, Language, TypeScript},
44
parser::{self, ParseError},
5-
rust_types::RustTypeParseError,
65
ProcessInputError,
76
};
87
/// Parse and generate types for a single Rust input file.
@@ -33,68 +32,6 @@ pub fn process_input(
3332
Ok(())
3433
}
3534

36-
mod blocklisted_types {
37-
use std::collections::HashMap;
38-
39-
use super::*;
40-
41-
fn assert_type_is_blocklisted(ty: &str, blocklisted_type: &str) {
42-
let source = format!(
43-
r##"
44-
#[typeshare]
45-
#[serde(default, rename_all = "camelCase")]
46-
pub struct Foo {{
47-
pub bar: {ty},
48-
}}
49-
"##,
50-
ty = ty
51-
);
52-
53-
let mut out: Vec<u8> = Vec::new();
54-
assert!(matches!(
55-
process_input(&source, &mut TypeScript::default(), &HashMap::new(), &mut out),
56-
Err(ProcessInputError::ParseError(
57-
ParseError::RustTypeParseError(RustTypeParseError::UnsupportedType(contents))
58-
)) if contents == vec![blocklisted_type.to_owned()]
59-
));
60-
}
61-
62-
#[test]
63-
fn test_i64_blocklisted_struct() {
64-
assert_type_is_blocklisted("i64", "i64");
65-
}
66-
67-
#[test]
68-
fn test_u64_blocklisted_struct() {
69-
assert_type_is_blocklisted("u64", "u64");
70-
}
71-
72-
#[test]
73-
fn test_isize_blocklisted_struct() {
74-
assert_type_is_blocklisted("isize", "isize");
75-
}
76-
77-
#[test]
78-
fn test_usize_blocklisted_in_struct() {
79-
assert_type_is_blocklisted("usize", "usize");
80-
}
81-
82-
#[test]
83-
fn test_optional_blocklisted_struct() {
84-
assert_type_is_blocklisted("Option<i64>", "i64");
85-
}
86-
87-
#[test]
88-
fn test_vec_blocklisted_struct() {
89-
assert_type_is_blocklisted("Vec<i64>", "i64");
90-
}
91-
92-
#[test]
93-
fn test_hashmap_blocklisted_struct() {
94-
assert_type_is_blocklisted("HashMap<String, i64>", "i64");
95-
}
96-
}
97-
9835
mod serde_attributes_on_enums {
9936
use std::collections::HashMap;
10037

0 commit comments

Comments
 (0)