Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Support date types #36

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ fn main() {
package: config.kotlin.package,
module_name: config.kotlin.module_name,
type_mappings: config.kotlin.type_mappings,
..Default::default()
}),
Some("typescript") => Box::new(TypeScript {
type_mappings: config.typescript.type_mappings,
..Default::default()
}),
#[cfg(feature = "go")]
Some("go") => Box::new(Go {
Expand Down
9 changes: 8 additions & 1 deletion core/src/language/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl Language for Go {
self.format_type(rtype1, generic_types)?,
self.format_type(rtype2, generic_types)?
),
SpecialRustType::DateTime => "Time".into(),
SpecialRustType::Unit => "struct{}".into(),
SpecialRustType::String => "string".into(),
SpecialRustType::I8
Expand Down Expand Up @@ -100,7 +101,13 @@ impl Language for Go {
)?;
writeln!(w, "package {}", self.package)?;
writeln!(w)?;
writeln!(w, "import \"encoding/json\"")?;
writeln!(
w,
r#"import (
"encoding/json"
"time"
)"#
)?;
writeln!(w)?;
Ok(())
}
Expand Down
27 changes: 26 additions & 1 deletion core/src/language/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
use itertools::Itertools;
use joinery::JoinableIterator;
use lazy_format::lazy_format;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{collections::HashMap, io::Write};

/// All information needed for Kotlin type-code
Expand All @@ -19,6 +20,8 @@ pub struct Kotlin {
pub module_name: String,
/// Conversions from Rust type names to Kotlin type names.
pub type_mappings: HashMap<String, String>,
/// Whether we've outputted a Date (requires custom serializer)
pub has_date: AtomicBool,
}

impl Language for Kotlin {
Expand All @@ -45,6 +48,10 @@ impl Language for Kotlin {
self.format_type(rtype2, generic_types)?
)
}
SpecialRustType::DateTime => {
self.has_date.store(true, Ordering::SeqCst);
"java.time.Instant".into()
}
SpecialRustType::Unit => "Unit".into(),
SpecialRustType::String => "String".into(),
// https://kotlinlang.org/docs/basic-types.html#integer-types
Expand Down Expand Up @@ -74,13 +81,31 @@ impl Language for Kotlin {
writeln!(w, "package {}", self.package)?;
writeln!(w)?;
writeln!(w, "import androidx.compose.runtime.NoLiveLiterals")?;
writeln!(w, "import kotlinx.serialization.*")?;
writeln!(w)?;
}

Ok(())
}

fn end_file(&self, w: &mut dyn Write) -> std::io::Result<()> {
if self.has_date.load(Ordering::SeqCst) {
writeln!(w, "import kotlinx.serialization.*")?;
writeln!(w)?;
writeln!(
w,
r#"object JavaInstantSerializer : KSerializer<java.time.Instant> {{
override val descriptor = PrimitiveDescriptor("Instant", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: java.time.Instant) = encoder.encodeString(value)
override fun deserialize(decoder: Decoder): java.time.Instant = java.time.Instant.parse(decoder.decodeString())
}}
"#
)?;
Ok(())
} else {
Ok(())
}
}

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

Expand Down
1 change: 1 addition & 0 deletions core/src/language/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Language for Swift {
self.format_type(rtype1, generic_types)?,
self.format_type(rtype2, generic_types)?
),
SpecialRustType::DateTime => "Date".into(),
SpecialRustType::Unit => {
self.should_emit_codable_void.store(true, Ordering::SeqCst);
"CodableVoid".into()
Expand Down
15 changes: 15 additions & 0 deletions core/src/language/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use crate::{
language::Language,
rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
};
use std::sync::atomic::{AtomicBool, Ordering};
use std::{collections::HashMap, io::Write};

/// All information needed to generate Typescript type-code
#[derive(Default)]
pub struct TypeScript {
/// Mappings from Rust type names to Typescript type names
pub type_mappings: HashMap<String, String>,
pub has_date: AtomicBool,
}

impl Language for TypeScript {
Expand Down Expand Up @@ -38,6 +40,10 @@ impl Language for TypeScript {
},
self.format_type(rtype2, generic_types)?
)),
SpecialRustType::DateTime => {
self.has_date.store(true, Ordering::SeqCst);
Ok("Date".into())
}
SpecialRustType::Unit => Ok("undefined".into()),
SpecialRustType::String => Ok("string".into()),
SpecialRustType::I8
Expand Down Expand Up @@ -68,6 +74,15 @@ impl Language for TypeScript {
Ok(())
}

fn end_file(&self, w: &mut dyn Write) -> std::io::Result<()> {
if self.has_date.load(Ordering::SeqCst) {
writeln!(w, "export function TypeshareDateReviver(key, value): Date {{ return new Date(value); }}")?;
Ok(())
} else {
Ok(())
}
}

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

Expand Down
10 changes: 8 additions & 2 deletions core/src/rust_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub enum SpecialRustType {
HashMap(Box<RustType>, Box<RustType>),
/// Represents `Option<T>` from the standard library
Option(Box<RustType>),
/// Represents chrono::DateTime<?> from chrono
DateTime,
/// Represents `()`
Unit,
/// Represents `String` from the standard library
Expand Down Expand Up @@ -213,6 +215,7 @@ impl TryFrom<&syn::Type> for RustType {
params.next().unwrap().into(),
))
}
"DateTime" => RustType::Special(SpecialRustType::DateTime),
"str" | "String" => RustType::Special(SpecialRustType::String),
// Since we do not need to box types in other languages, we treat this type
// as its inner type.
Expand Down Expand Up @@ -311,7 +314,8 @@ impl SpecialRustType {
match &self {
Self::Vec(rty) | Self::Option(rty) => rty.contains_type(ty),
Self::HashMap(rty1, rty2) => rty1.contains_type(ty) || rty2.contains_type(ty),
Self::Unit
Self::DateTime
| Self::Unit
| Self::String
| Self::I8
| Self::I16
Expand Down Expand Up @@ -340,6 +344,7 @@ impl SpecialRustType {
Self::Vec(_) => "Vec",
Self::Option(_) => "Option",
Self::HashMap(_, _) => "HashMap",
Self::DateTime => "DateTime",
Self::String => "String",
Self::Bool => "bool",
Self::I8 => "i8",
Expand All @@ -365,7 +370,8 @@ impl SpecialRustType {
Self::HashMap(rtype1, rtype2) => {
Box::new([rtype1.as_ref(), rtype2.as_ref()].into_iter())
}
Self::Unit
Self::DateTime
| Self::Unit
| Self::String
| Self::I8
| Self::I16
Expand Down