1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
//! Generate TypeScript interface/type declarations from rust structs. //! //! ## why? //! When building a web application in rust, data structures have to be shared between backend and frontend. //! Using this library, you can easily generate TypeScript bindings to your rust structs & enums, so that you can keep your //! types in one place. //! //! ts-rs might also come in handy when working with webassembly. //! //! ## how? //! ts-rs exposes a single trait, `TS`. //! Using a derive macro, you can implement this trait for //! your types. //! Then, you can use this trait to obtain the TypeScript bindings. //! We recommend doing this in your tests. [see the example](https://github.com/Aleph-Alpha/ts-rs/blob/main/example/src/lib.rs) //! //! ## serde compatibility layer //! With the `serde-compat` feature enabled, ts-rs tries parsing serde attributes. //! Please note that not all serde attributes are supported yet. use std::fs::OpenOptions; use std::io::{BufWriter, Write}; use std::path::Path; pub use ts_rs_macros::TS; /// A type which can be represented in TypeScript. /// Most of the time, you'd want to derive this trait instead of implementing it manually. /// ts-rs comes with implementations for all numeric types, `String`, `Vec`, `Option` and tuples. /// /// ## get started /// [TS](ts_rs::TS) can easily be derived for structs and enums: /// ```rust /// use ts_rs::TS; /// /// #[derive(TS)] /// struct User { /// first_name: String, /// last_name: String, /// } /// ``` /// To actually obtain the bindings, you can call `User::dump` to write the bindings to a file: /// ```rust /// # use ts_rs::TS; /// /// # #[derive(TS)] /// # struct User { /// # first_name: String, /// # last_name: String, /// # } /// std::fs::remove_file("bindings.ts").ok(); /// User::dump("bindings.ts").unwrap(); /// ``` /// /// ### struct attributes /// /// - `#[ts(rename = "..")]`: /// Set the name of the generated interface /// /// - `#[ts(rename_all = "..")]`: /// Rename all fields of this struct. /// Valid values are `lowercase`, `UPPERCASE`, `camelCase`, `snake_case`, `PascalCase`, `SCREAMING_SNAKE_CASE` /// /// ### struct field attributes /// /// - `#[ts(type = "..")]`: /// Overrides the type used in TypeScript /// /// - `#[ts(rename = "..")]`: /// Renames this field /// /// - `#[ts(inline)]`: /// Inlines the type of this field /// /// - `#[ts(skip)]`: /// Skip this field /// /// - `#[ts(flatten)]`: /// Flatten this field (only works if the field is a struct) /// /// ### enum attributes /// /// - `#[ts(rename = "..")]`: /// Set the name of the generated type /// /// - `#[ts(rename_all = "..")]`: /// Rename all variants of this enum. /// Valid values are `lowercase`, `UPPERCASE`, `camelCase`, `snake_case`, `PascalCase`, `SCREAMING_SNAKE_CASE` /// /// ### enum variant attributes /// /// - `#[ts(rename = "..")]`: /// Renames this variant /// /// - `#[ts(skip)]`: /// Skip this variant pub trait TS { /// Declaration of this type, e.g. `interface User { user_id: number, ... }`. /// This function will panic if the type has no declaration. fn decl() -> String { panic!("{} cannot be declared", Self::name()); } /// Name of this type in TypeScript. fn name() -> String; /// Formats this types definition in TypeScript, e.g `{ user_id: number }`. /// This function will panic if the type cannot be inlined. fn inline(#[allow(unused_variables)] indent: usize) -> String { panic!("{} cannot be inlined", Self::name()); } /// Flatten an type declaration. /// This function will panic if the type cannot be flattened. fn inline_flattened(#[allow(unused_variables)] indent: usize) -> String { panic!("{} cannot be flattened", Self::name()) } /// Dumps the declaration of this type to a file. /// If the file does not exist, it will be created. /// If it does, the declaration will be appended. /// /// This function will panicked when called on a type which does not have a declaration. fn dump(out: impl AsRef<Path>) -> std::io::Result<()> { let out = out.as_ref(); let file = OpenOptions::new() .append(true) .create(true) .truncate(false) .open(out)?; let mut writer = BufWriter::new(file); writer.write_all(Self::decl().as_bytes())?; writer.write_all(b"\n\n")?; writer.flush()?; Ok(()) } } /// Expands to a test function which exports typescript bindings to one or multiple files. /// If a file already exists, it will be overriden. /// Missing parent directories of the file(s) will be created. /// Paths are interpreted as being relative to the project root. /// ```rust /// # use ts_rs::{export, TS}; /// #[derive(TS)] struct A; /// #[derive(TS)] struct B; /// #[derive(TS)] struct C; /// /// export! { /// A, B => "bindings/a.ts", /// C => "bindings/b.ts" /// } /// ``` /// When running `cargo test`, bindings for `A`, `B` and `C` will be exported to `bindings/a.ts` /// and `bindings/b.ts`. #[macro_export] macro_rules! export { ($($($p:path),+ => $l:literal),* $(,)?) => { #[cfg(test)] #[test] fn export_typescript() -> std::io::Result<()> { use std::io::Write; let manifest_var = std::env::var("CARGO_MANIFEST_DIR").unwrap(); let manifest_dir = std::path::Path::new(&manifest_var); $({ let out = manifest_dir.join($l); std::fs::create_dir_all(out.parent().unwrap())?; let out_file = std::fs::File::create(&out)?; let mut writer = std::io::BufWriter::new(out_file); $( writer.write_all(<$p as ts_rs::TS>::decl().as_bytes())?; writer.write_all(b"\n\n")?; )* writer.flush()?; })* Ok(()) } }; } macro_rules! impl_primitives { ($($($ty:ty),* => $l:literal),*) => { $($( impl TS for $ty { fn name() -> String { $l.to_owned() } fn inline(_: usize) -> String { $l.to_owned() } } )*)* }; } macro_rules! impl_tuples { ( impl $($i:ident),* ) => { impl<$($i: TS),*> TS for ($($i,)*) { fn name() -> String { format!( "[{}]", vec![$($i::name()),*].join(", ") ) } fn inline(indent: usize) -> String { format!( "[{}]", vec![ $($i::inline(indent)),* ].join(", ") ) } } }; ( $i2:ident $(, $i:ident)* ) => { impl_tuples!(impl $i2 $(, $i)* ); impl_tuples!($($i),*); }; () => {}; } macro_rules! impl_proxy { ($($t:tt)*) => { $($t)* { fn name() -> String { T::name() } fn inline(indent: usize) -> String { T::inline(indent) } fn inline_flattened(indent: usize) -> String { T::inline_flattened(indent) } } }; } impl_primitives! { u8, i8, u16, i16, u32, i32, u64, i64, f32, f64 => "number", u128, i128 => "bigint", bool => "boolean", String, &str => "string", () => "null" } impl_tuples!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10); impl_proxy!(impl<T: TS> TS for &T); impl_proxy!(impl<T: TS> TS for Box<T>); impl_proxy!(impl<T: TS> TS for std::sync::Arc<T>); impl_proxy!(impl<T: TS> TS for std::rc::Rc<T>); impl_proxy!(impl<'a, T: TS + ToOwned> TS for std::borrow::Cow<'a, T>); impl_proxy!(impl<T: TS> TS for std::cell::Cell<T>); impl_proxy!(impl<T: TS> TS for std::cell::RefCell<T>); impl<T: TS> TS for Option<T> { fn name() -> String { format!("{} | null", T::name()) } fn inline(indent: usize) -> String { format!("{} | null", T::inline(indent)) } } impl<T: TS> TS for Vec<T> { fn name() -> String { format!("{}[]", T::name()) } fn inline(indent: usize) -> String { format!("{}[]", T::inline(indent)) } }