Skip to content

Commit

Permalink
Add const support for integer types (#218)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
edif2008 authored Jan 22, 2025
1 parent 0699c46 commit edccd96
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 19 deletions.
2 changes: 2 additions & 0 deletions core/data/tests/can_generate_const/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[typeshare]
pub const MY_VAR: u32 = 12;
5 changes: 5 additions & 0 deletions core/data/tests/can_generate_const/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package proto

import "encoding/json"

const MyVar uint32 = 12
6 changes: 6 additions & 0 deletions core/data/tests/can_generate_const/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from __future__ import annotations




MY_VAR: int = 12
1 change: 1 addition & 0 deletions core/data/tests/can_generate_const/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MY_VAR: number = 12;
22 changes: 21 additions & 1 deletion core/src/language/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Write;
use crate::language::SupportedLanguage;
use crate::parser::ParsedData;
use crate::rename::RenameExt;
use crate::rust_types::{RustItem, RustTypeFormatError, SpecialRustType};
use crate::rust_types::{RustConst, RustConstExpr, RustItem, RustTypeFormatError, SpecialRustType};
use crate::{
language::Language,
rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
Expand Down Expand Up @@ -58,6 +58,7 @@ impl Language for Go {
structs,
enums,
aliases,
consts,
..
} = data;

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

topsort(&mut items);
Expand Down Expand Up @@ -96,6 +98,7 @@ impl Language for Go {
RustItem::Enum(e) => self.write_enum(w, e, &types_mapping_to_struct)?,
RustItem::Struct(s) => self.write_struct(w, s)?,
RustItem::Alias(a) => self.write_type_alias(w, a)?,
RustItem::Const(c) => self.write_const(w, c)?,
}
}

Expand Down Expand Up @@ -189,6 +192,23 @@ impl Language for Go {
Ok(())
}

fn write_const(&mut self, w: &mut dyn Write, c: &RustConst) -> std::io::Result<()> {
match c.expr {
RustConstExpr::Int(val) => {
let const_type = self
.format_type(&c.r#type, &[])
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
writeln!(
w,
"const {} {} = {}",
c.id.renamed.to_pascal_case(),
const_type,
val
)
}
}
}

fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
write_comments(w, 0, &rs.comments)?;
// TODO: Support generic bounds: https://github.com/1Password/typeshare/issues/222
Expand Down
6 changes: 5 additions & 1 deletion core/src/language/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::parser::{remove_dash_from_identifier, DecoratorKind, ParsedData};
use crate::rust_types::{RustTypeFormatError, SpecialRustType};
use crate::{
rename::RenameExt,
rust_types::{Id, RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
rust_types::{Id, RustConst, RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
};
use itertools::Itertools;
use joinery::JoinableIterator;
Expand Down Expand Up @@ -174,6 +174,10 @@ impl Language for Kotlin {
Ok(())
}

fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
todo!()
}

fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
self.write_comments(w, 0, &rs.comments)?;
writeln!(w, "@Serializable")?;
Expand Down
16 changes: 14 additions & 2 deletions core/src/language/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
parser::{ParseError, ParsedData},
rust_types::{
Id, RustEnum, RustEnumVariant, RustItem, RustStruct, RustType, RustTypeAlias,
Id, RustConst, RustEnum, RustEnumVariant, RustItem, RustStruct, RustType, RustTypeAlias,
RustTypeFormatError, SpecialRustType,
},
topsort::topsort,
Expand Down Expand Up @@ -173,6 +173,7 @@ pub trait Language {
structs,
enums,
aliases,
consts,
..
} = data;

Expand All @@ -181,7 +182,8 @@ pub trait Language {
.into_iter()
.map(RustItem::Alias)
.chain(structs.into_iter().map(RustItem::Struct))
.chain(enums.into_iter().map(RustItem::Enum)),
.chain(enums.into_iter().map(RustItem::Enum))
.chain(consts.into_iter().map(RustItem::Const)),
);

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

Expand Down Expand Up @@ -299,6 +302,15 @@ pub trait Language {
Ok(())
}

/// Write a constant variable.
/// Example of a constant variable:
/// ```
/// const ANSWER_TO_EVERYTHING: u32 = 42;
/// ```
fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
Ok(())
}

/// Write a struct by converting it
/// Example of a struct:
/// ```ignore
Expand Down
25 changes: 24 additions & 1 deletion core/src/language/python.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::parser::ParsedData;
use crate::rust_types::{RustEnumShared, RustItem, RustType, RustTypeFormatError, SpecialRustType};
use crate::topsort::topsort;
use crate::RenameExt;
use crate::{
language::Language,
rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
rust_types::{
RustConst, RustConstExpr, RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias,
},
};
use std::collections::HashSet;
use std::hash::Hash;
Expand Down Expand Up @@ -98,6 +101,7 @@ impl Language for Python {
structs,
enums,
aliases,
consts,
..
} = data;

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

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

Expand Down Expand Up @@ -243,6 +249,23 @@ impl Language for Python {
Ok(())
}

fn write_const(&mut self, w: &mut dyn Write, c: &RustConst) -> std::io::Result<()> {
match c.expr {
RustConstExpr::Int(val) => {
let const_type = self
.format_type(&c.r#type, &[])
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
writeln!(
w,
"{}: {} = {}",
c.id.renamed.to_snake_case().to_uppercase(),
const_type,
val
)
}
}
}

fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
{
rs.generic_types
Expand Down
10 changes: 8 additions & 2 deletions core/src/language/scala.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::{CrateTypes, Language};
use crate::language::SupportedLanguage;
use crate::parser::{remove_dash_from_identifier, ParsedData};
use crate::rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias};
use crate::rust_types::{RustType, RustTypeFormatError, SpecialRustType};
use crate::rust_types::{
RustConst, RustEnum, RustEnumVariant, RustField, RustStruct, RustType, RustTypeAlias,
RustTypeFormatError, SpecialRustType,
};
use itertools::Itertools;
use joinery::JoinableIterator;
use lazy_format::lazy_format;
Expand Down Expand Up @@ -150,6 +152,10 @@ impl Language for Scala {
Ok(())
}

fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
todo!()
}

fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> std::io::Result<()> {
self.write_comments(w, 0, &rs.comments)?;

Expand Down
8 changes: 6 additions & 2 deletions core/src/language/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
parser::{remove_dash_from_identifier, DecoratorKind, ParsedData},
rename::RenameExt,
rust_types::{
DecoratorMap, RustEnum, RustEnumVariant, RustStruct, RustTypeAlias, RustTypeFormatError,
SpecialRustType,
DecoratorMap, RustConst, RustEnum, RustEnumVariant, RustStruct, RustTypeAlias,
RustTypeFormatError, SpecialRustType,
},
GenerationError,
};
Expand Down Expand Up @@ -258,6 +258,10 @@ impl Language for Swift {
Ok(())
}

fn write_const(&mut self, _w: &mut dyn Write, _c: &RustConst) -> std::io::Result<()> {
todo!()
}

fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> io::Result<()> {
let mut coding_keys = vec![];
let mut should_write_coding_keys = false;
Expand Down
22 changes: 20 additions & 2 deletions core/src/language/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::RenameExt;
use crate::{
language::{Language, SupportedLanguage},
parser::ParsedData,
rust_types::{
RustEnum, RustEnumVariant, RustField, RustStruct, RustType, RustTypeAlias,
RustTypeFormatError, SpecialRustType,
RustConst, RustConstExpr, RustEnum, RustEnumVariant, RustField, RustStruct, RustType,
RustTypeAlias, RustTypeFormatError, SpecialRustType,
},
};
use itertools::Itertools;
Expand Down Expand Up @@ -120,6 +121,23 @@ impl Language for TypeScript {
Ok(())
}

fn write_const(&mut self, w: &mut dyn Write, c: &RustConst) -> io::Result<()> {
match c.expr {
RustConstExpr::Int(val) => {
let const_type = self
.format_type(&c.r#type, &[])
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
writeln!(
w,
"export const {}: {} = {};",
c.id.renamed.to_snake_case().to_uppercase(),
const_type,
val
)
}
}
}

fn write_struct(&mut self, w: &mut dyn Write, rs: &RustStruct) -> io::Result<()> {
self.write_comments(w, 0, &rs.comments)?;
writeln!(
Expand Down
Loading

0 comments on commit edccd96

Please sign in to comment.