prepare 4.0 release

This commit is contained in:
Moritz Bischof
2021-09-27 23:25:47 +02:00
parent 961cc1b53a
commit c1269f1e79
23 changed files with 115 additions and 53 deletions

View File

@@ -39,7 +39,7 @@ We recommend doing this in your tests. [see the example](https://github.com/Alep
## get started
```toml
[dependencies]
ts-rs = "3.1"
ts-rs = "4.0"
```
```rust
@@ -88,4 +88,5 @@ Supported serde attributes:
- [x] documentation
- [x] use typescript types across files
- [x] more enum representations
- [x] generics
- [ ] don't require `'static`

View File

@@ -1,9 +1,9 @@
#![allow(dead_code)]
use std::{collections::BTreeSet, rc::Rc};
use chrono::NaiveDateTime;
use serde::Serialize;
use std::collections::BTreeSet;
use std::rc::Rc;
use ts_rs::{export, TS};
#[derive(Serialize, TS)]

View File

@@ -1,6 +1,6 @@
[package]
name = "ts-rs-macros"
version = "3.1.0"
version = "4.0.0"
authors = ["Moritz Bischof <moritz.bischof1@gmail.com>"]
edition = "2018"
description = "derive macro for ts-rs"

View File

@@ -1,7 +1,9 @@
use syn::{Attribute, Ident, Result};
use crate::attr::{parse_assign_inflection, parse_assign_str, Inflection};
use crate::utils::parse_attrs;
use crate::{
attr::{parse_assign_inflection, parse_assign_str, Inflection},
utils::parse_attrs,
};
#[derive(Default)]
pub struct EnumAttr {

View File

@@ -1,8 +1,7 @@
use syn::{Attribute, Ident, Result};
use crate::utils::parse_attrs;
use super::parse_assign_str;
use crate::utils::parse_attrs;
#[derive(Default)]
pub struct FieldAttr {

View File

@@ -1,10 +1,12 @@
use std::convert::TryFrom;
use syn::{parse::Parse, parse::ParseStream, Error, Lit, Result, Token};
pub use field::*;
pub use r#enum::*;
pub use r#struct::*;
use syn::{
parse::{Parse, ParseStream},
Error, Lit, Result, Token,
};
mod r#enum;
mod field;

View File

@@ -2,8 +2,10 @@ use std::convert::TryFrom;
use syn::{Attribute, Ident, Result};
use crate::attr::{parse_assign_str, Inflection};
use crate::utils::parse_attrs;
use crate::{
attr::{parse_assign_str, Inflection},
utils::parse_attrs,
};
#[derive(Default)]
pub struct StructAttr {

View File

@@ -3,8 +3,7 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::spanned::Spanned;
use syn::{parse_quote, WhereClause, GenericParam, Generics, Item, Result};
use syn::{parse_quote, spanned::Spanned, GenericParam, Generics, Item, Result, WhereClause};
#[macro_use]
mod utils;
@@ -72,11 +71,17 @@ impl DerivedTS {
}
fn add_ts_trait_bound(generics: &Generics) -> Option<WhereClause> {
let generic_types: Vec<_> = generics.params.iter().filter_map(|gp| match gp {
GenericParam::Type(ty) => Some(ty.ident.clone()),
_ => None,
}).collect();
if generic_types.len() == 0 { return generics.where_clause.clone() }
let generic_types: Vec<_> = generics
.params
.iter()
.filter_map(|gp| match gp {
GenericParam::Type(ty) => Some(ty.ident.clone()),
_ => None,
})
.collect();
if generic_types.is_empty() {
return generics.where_clause.clone();
}
match generics.where_clause {
None => Some(parse_quote! { where #( #generic_types : ts_rs::TS ),* }),
Some(ref w) => {

View File

@@ -2,9 +2,11 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::{Fields, Generics, ItemEnum, ItemStruct, Result, Variant};
use crate::attr::{EnumAttr, FieldAttr, Inflection, StructAttr};
use crate::types::generics::format_type;
use crate::DerivedTS;
use crate::{
attr::{EnumAttr, FieldAttr, Inflection, StructAttr},
types::generics::format_type,
DerivedTS,
};
mod generics;
mod named;

View File

@@ -4,9 +4,11 @@ use syn::{
Field, FieldsNamed, GenericArgument, GenericParam, Generics, PathArguments, Result, Type,
};
use crate::attr::{FieldAttr, Inflection};
use crate::types::generics::format_type;
use crate::DerivedTS;
use crate::{
attr::{FieldAttr, Inflection},
types::generics::format_type,
DerivedTS,
};
pub(crate) fn named(
name: &str,

View File

@@ -1,8 +1,10 @@
use quote::quote;
use syn::{FieldsUnnamed, Result};
use crate::attr::{FieldAttr, Inflection};
use crate::DerivedTS;
use crate::{
attr::{FieldAttr, Inflection},
DerivedTS,
};
pub(crate) fn newtype(
name: &str,

View File

@@ -2,8 +2,10 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::{Field, FieldsUnnamed, Result};
use crate::attr::{FieldAttr, Inflection};
use crate::DerivedTS;
use crate::{
attr::{FieldAttr, Inflection},
DerivedTS,
};
pub(crate) fn tuple(
name: &str,

View File

@@ -1,8 +1,7 @@
use quote::quote;
use syn::Result;
use crate::attr::Inflection;
use crate::DerivedTS;
use crate::{attr::Inflection, DerivedTS};
pub(crate) fn unit(name: &str, rename_all: &Option<Inflection>) -> Result<DerivedTS> {
if rename_all.is_some() {

View File

@@ -1,14 +1,13 @@
use std::convert::TryFrom;
use syn::Attribute;
use syn::{Error, Result};
use syn::{Attribute, Error, Result};
macro_rules! syn_err {
($l:literal $(, $a:expr)*) => {
syn_err!(proc_macro2::Span::call_site(); $l $(, $a)*);
syn_err!(proc_macro2::Span::call_site(); $l $(, $a)*)
};
($s:expr; $l:literal $(, $a:expr)*) => {
return Err(syn::Error::new($s, format!($l $(, $a)*)));
return Err(syn::Error::new($s, format!($l $(, $a)*)))
};
}
@@ -86,8 +85,7 @@ pub fn parse_serde_attrs<'a, A: TryFrom<&'a Attribute, Error = Error>>(
#[cfg(feature = "serde-compat")]
mod warning {
use std::fmt::Display;
use std::io::Write;
use std::{fmt::Display, io::Write};
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};

2
rustfmt.toml Normal file
View File

@@ -0,0 +1,2 @@
imports_granularity = "Crate"
group_imports = "StdExternalCrate"

View File

@@ -1,6 +1,6 @@
[package]
name = "ts-rs"
version = "3.1.0"
version = "4.0.0"
authors = ["Moritz Bischof <moritz.bischof1@gmail.com>"]
edition = "2018"
license = "MIT"
@@ -17,7 +17,7 @@ serde-compat = ["ts-rs-macros/serde-compat"]
serde = { version = "1.0", features = ["derive"] }
[dependencies]
ts-rs-macros = { version = "3.1.0", path = "../macros" }
ts-rs-macros = { version = "4.0.0", path = "../macros" }
dprint-plugin-typescript = { version = "0.43" }
chrono = { version = "0.4.19", optional = true }
bigdecimal = {version = "0.1.2", features=["serde"], optional = true}

View File

@@ -1,9 +1,11 @@
use std::any::TypeId;
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Component, Path, PathBuf};
use std::{
any::TypeId,
collections::{BTreeMap, BTreeSet},
fmt::Write,
path::{Component, Path, PathBuf},
};
use crate::TS;
use std::fmt::Write;
/// Expands to a test function which exports typescript bindings to one or multiple files when
/// running `cargo test`.

View File

@@ -18,14 +18,14 @@
//! With the `serde-compat` feature enabled, ts-rs tries parsing serde attributes.
//! Please note that not all serde attributes are supported yet.
use std::path::Path;
use std::{collections::HashMap, fs::OpenOptions};
use std::{
collections::{BTreeMap, BTreeSet, HashSet},
any::TypeId,
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
fs::OpenOptions,
io::{BufWriter, Write},
path::Path,
};
use std::any::TypeId;
pub use ts_rs_macros::TS;
#[doc(hidden)]
@@ -251,8 +251,50 @@ impl_primitives! {
}
#[cfg(feature = "chrono-impl")]
impl_primitives! {
chrono::NaiveDateTime => "string"
mod chrono_impls {
use std::any::TypeId;
use chrono::{Date, DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone};
use super::TS;
impl_primitives! {
NaiveDateTime, NaiveDate, NaiveTime => "string"
}
impl<T: TimeZone + 'static> TS for DateTime<T> {
fn name() -> String {
"string".to_owned()
}
fn inline(_indent: usize) -> String {
"string".to_owned()
}
fn dependencies() -> Vec<(TypeId, String)> {
vec![]
}
fn transparent() -> bool {
false
}
}
impl<T: TimeZone + 'static> TS for Date<T> {
fn name() -> String {
"string".to_owned()
}
fn inline(_indent: usize) -> String {
"string".to_owned()
}
fn dependencies() -> Vec<(TypeId, String)> {
vec![]
}
fn transparent() -> bool {
false
}
}
}
#[cfg(feature = "bigdecimal-impl")]

View File

@@ -1,6 +1,7 @@
#![allow(dead_code)]
use std::collections::HashMap;
use ts_rs::TS;
#[derive(TS)]
@@ -20,7 +21,8 @@ struct GenericAutoBound<T> {
#[derive(TS)]
struct GenericAutoBound2<T>
where T: PartialEq
where
T: PartialEq,
{
value: T,
values: Vec<T>,

View File

@@ -1,7 +1,6 @@
#![allow(dead_code)]
use serde::Serialize;
use ts_rs::TS;
#[derive(Serialize, TS)]

View File

@@ -1,6 +1,7 @@
#![allow(dead_code)]
use std::time::Instant;
use ts_rs::TS;
#[derive(TS)]

View File

@@ -1,7 +1,6 @@
#![allow(dead_code)]
use serde::Deserialize;
use ts_rs::TS;
#[derive(TS, Deserialize)]

View File

@@ -1,7 +1,6 @@
#![allow(dead_code)]
use serde::Serialize;
use ts_rs::TS;
#[derive(Serialize, TS)]