Skip to content

Commit edccd96

Browse files
authored
Add const support for integer types (#218)
* Add Rust constant type * Add constant parsing functionality * Add basic structure for generating consts * Add Go const generation * Add Python const generation * Add Typescript const generation * Add test for generating constants
1 parent 0699c46 commit edccd96

File tree

16 files changed

+260
-19
lines changed

16 files changed

+260
-19
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[typeshare]
2+
pub const MY_VAR: u32 = 12;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package proto
2+
3+
import "encoding/json"
4+
5+
const MyVar uint32 = 12
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from __future__ import annotations
2+
3+
4+
5+
6+
MY_VAR: int = 12
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const MY_VAR: number = 12;

core/src/language/go.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
use crate::language::SupportedLanguage;
44
use crate::parser::ParsedData;
55
use crate::rename::RenameExt;
6-
use crate::rust_types::{RustItem, RustTypeFormatError, SpecialRustType};
6+
use crate::rust_types::{RustConst, RustConstExpr, RustItem, RustTypeFormatError, SpecialRustType};
77
use crate::{
88
language::Language,
99
rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
@@ -58,6 +58,7 @@ impl Language for Go {
5858
structs,
5959
enums,
6060
aliases,
61+
consts,
6162
..
6263
} = data;
6364

@@ -66,6 +67,7 @@ impl Language for Go {
6667
.map(RustItem::Alias)
6768
.chain(structs.into_iter().map(RustItem::Struct))
6869
.chain(enums.into_iter().map(RustItem::Enum))
70+
.chain(consts.into_iter().map(RustItem::Const))
6971
.collect::<Vec<_>>();
7072

7173
topsort(&mut items);
@@ -96,6 +98,7 @@ impl Language for Go {
9698
RustItem::Enum(e) => self.write_enum(w, e, &types_mapping_to_struct)?,
9799
RustItem::Struct(s) => self.write_struct(w, s)?,
98100
RustItem::Alias(a) => self.write_type_alias(w, a)?,
101+
RustItem::Const(c) => self.write_const(w, c)?,
99102
}
100103
}
101104

@@ -189,6 +192,23 @@ impl Language for Go {
189192
Ok(())
190193
}
191194

195+
fn write_const(&mut self, w: &mut dyn Write, c: &RustConst) -> std::io::Result<()> {
196+
match c.expr {
197+
RustConstExpr::Int(val) => {
198+
let const_type = self
199+
.format_type(&c.r#type, &[])
200+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
201+
writeln!(
202+
w,
203+
"const {} {} = {}",
204+
c.id.renamed.to_pascal_case(),
205+
const_type,
206+
val
207+
)
208+
}
209+
}
210+
}
211+
192212
fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
193213
write_comments(w, 0, &rs.comments)?;
194214
// TODO: Support generic bounds: https://github.com/1Password/typeshare/issues/222

core/src/language/kotlin.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::parser::{remove_dash_from_identifier, DecoratorKind, ParsedData};
44
use crate::rust_types::{RustTypeFormatError, SpecialRustType};
55
use crate::{
66
rename::RenameExt,
7-
rust_types::{Id, RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
7+
rust_types::{Id, RustConst, RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
88
};
99
use itertools::Itertools;
1010
use joinery::JoinableIterator;
@@ -174,6 +174,10 @@ impl Language for Kotlin {
174174
Ok(())
175175
}
176176

177+
fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
178+
todo!()
179+
}
180+
177181
fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
178182
self.write_comments(w, 0, &rs.comments)?;
179183
writeln!(w, "@Serializable")?;

core/src/language/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
parser::{ParseError, ParsedData},
33
rust_types::{
4-
Id, RustEnum, RustEnumVariant, RustItem, RustStruct, RustType, RustTypeAlias,
4+
Id, RustConst, RustEnum, RustEnumVariant, RustItem, RustStruct, RustType, RustTypeAlias,
55
RustTypeFormatError, SpecialRustType,
66
},
77
topsort::topsort,
@@ -173,6 +173,7 @@ pub trait Language {
173173
structs,
174174
enums,
175175
aliases,
176+
consts,
176177
..
177178
} = data;
178179

@@ -181,7 +182,8 @@ pub trait Language {
181182
.into_iter()
182183
.map(RustItem::Alias)
183184
.chain(structs.into_iter().map(RustItem::Struct))
184-
.chain(enums.into_iter().map(RustItem::Enum)),
185+
.chain(enums.into_iter().map(RustItem::Enum))
186+
.chain(consts.into_iter().map(RustItem::Const)),
185187
);
186188

187189
topsort(&mut items);
@@ -191,6 +193,7 @@ pub trait Language {
191193
RustItem::Enum(e) => self.write_enum(writable, e)?,
192194
RustItem::Struct(s) => self.write_struct(writable, s)?,
193195
RustItem::Alias(a) => self.write_type_alias(writable, a)?,
196+
RustItem::Const(c) => self.write_const(writable, c)?,
194197
}
195198
}
196199

@@ -299,6 +302,15 @@ pub trait Language {
299302
Ok(())
300303
}
301304

305+
/// Write a constant variable.
306+
/// Example of a constant variable:
307+
/// ```
308+
/// const ANSWER_TO_EVERYTHING: u32 = 42;
309+
/// ```
310+
fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
311+
Ok(())
312+
}
313+
302314
/// Write a struct by converting it
303315
/// Example of a struct:
304316
/// ```ignore

core/src/language/python.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use crate::parser::ParsedData;
22
use crate::rust_types::{RustEnumShared, RustItem, RustType, RustTypeFormatError, SpecialRustType};
33
use crate::topsort::topsort;
4+
use crate::RenameExt;
45
use crate::{
56
language::Language,
6-
rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
7+
rust_types::{
8+
RustConst, RustConstExpr, RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias,
9+
},
710
};
811
use std::collections::HashSet;
912
use std::hash::Hash;
@@ -98,6 +101,7 @@ impl Language for Python {
98101
structs,
99102
enums,
100103
aliases,
104+
consts,
101105
..
102106
} = data;
103107

@@ -106,6 +110,7 @@ impl Language for Python {
106110
.map(RustItem::Alias)
107111
.chain(structs.into_iter().map(RustItem::Struct))
108112
.chain(enums.into_iter().map(RustItem::Enum))
113+
.chain(consts.into_iter().map(RustItem::Const))
109114
.collect::<Vec<_>>();
110115

111116
topsort(&mut items);
@@ -116,6 +121,7 @@ impl Language for Python {
116121
RustItem::Enum(e) => self.write_enum(&mut body, &e)?,
117122
RustItem::Struct(rs) => self.write_struct(&mut body, &rs)?,
118123
RustItem::Alias(t) => self.write_type_alias(&mut body, &t)?,
124+
RustItem::Const(c) => self.write_const(&mut body, &c)?,
119125
};
120126
}
121127

@@ -243,6 +249,23 @@ impl Language for Python {
243249
Ok(())
244250
}
245251

252+
fn write_const(&mut self, w: &mut dyn Write, c: &RustConst) -> std::io::Result<()> {
253+
match c.expr {
254+
RustConstExpr::Int(val) => {
255+
let const_type = self
256+
.format_type(&c.r#type, &[])
257+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
258+
writeln!(
259+
w,
260+
"{}: {} = {}",
261+
c.id.renamed.to_snake_case().to_uppercase(),
262+
const_type,
263+
val
264+
)
265+
}
266+
}
267+
}
268+
246269
fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
247270
{
248271
rs.generic_types

core/src/language/scala.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use super::{CrateTypes, Language};
22
use crate::language::SupportedLanguage;
33
use crate::parser::{remove_dash_from_identifier, ParsedData};
4-
use crate::rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias};
5-
use crate::rust_types::{RustType, RustTypeFormatError, SpecialRustType};
4+
use crate::rust_types::{
5+
RustConst, RustEnum, RustEnumVariant, RustField, RustStruct, RustType, RustTypeAlias,
6+
RustTypeFormatError, SpecialRustType,
7+
};
68
use itertools::Itertools;
79
use joinery::JoinableIterator;
810
use lazy_format::lazy_format;
@@ -150,6 +152,10 @@ impl Language for Scala {
150152
Ok(())
151153
}
152154

155+
fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
156+
todo!()
157+
}
158+
153159
fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
154160
self.write_comments(w, 0, &rs.comments)?;
155161

core/src/language/swift.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::{
33
parser::{remove_dash_from_identifier, DecoratorKind, ParsedData},
44
rename::RenameExt,
55
rust_types::{
6-
DecoratorMap, RustEnum, RustEnumVariant, RustStruct, RustTypeAlias, RustTypeFormatError,
7-
SpecialRustType,
6+
DecoratorMap, RustConst, RustEnum, RustEnumVariant, RustStruct, RustTypeAlias,
7+
RustTypeFormatError, SpecialRustType,
88
},
99
GenerationError,
1010
};
@@ -258,6 +258,10 @@ impl Language for Swift {
258258
Ok(())
259259
}
260260

261+
fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
262+
todo!()
263+
}
264+
261265
fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> io::Result<()> {
262266
let mut coding_keys = vec![];
263267
let mut should_write_coding_keys = false;

0 commit comments

Comments
 (0)