Skip to main content

specta_kotlin/
lib.rs

1//! [Kotlin](https://kotlinlang.org) language exporter for [Specta](specta).
2//!
3//! <div class="warning">
4//! This crate is still in active development and is not yet ready for general purpose use!
5//! </div>
6//!
7#![cfg_attr(docsrs, feature(doc_cfg))]
8#![doc(
9    html_logo_url = "https://github.com/specta-rs/specta/raw/main/.github/logo-128.png",
10    html_favicon_url = "https://github.com/specta-rs/specta/raw/main/.github/logo-128.png"
11)]
12
13// use specta::{
14//     datatype::{DataType, Primitive},
15//     Type, Types,
16// };
17
18// /// TODO
19// pub fn export<T: Type>() -> Result<String, String> {
20//     datatype(&T::definition(
21//         &mut Types::default(),
22//         Generics::Definition,
23//     ))
24// }
25
26// fn datatype(t: &DataType) -> Result<String, String> {
27//     Ok(match t {
28//         DataType::Primitive(p) => match p {
29//             Primitive::str => "String",
30//             Primitive::char => "Char",
31//             Primitive::i8 => "Byte",
32//             Primitive::i16 => "Short",
33//             Primitive::isize | Primitive::i32 => "Int",
34//             Primitive::i64 => "Long",
35//             Primitive::u8 => "UByte",
36//             Primitive::u16 => "UShort",
37//             Primitive::usize | Primitive::u32 => "UInt",
38//             Primitive::u64 => "ULong",
39//             Primitive::bool => "Boolean",
40//             Primitive::f32 => "Float",
41//             Primitive::f64 => "Double",
42//             Primitive::i128 | Primitive::u128 => {
43//                 return Err("Swift does not support 128 numbers!".to_owned())
44//             }
45//         }
46//         .to_string(),
47//         DataType::List(t) => format!("List<{}>", datatype(t.ty())?),
48//         DataType::Tuple(_) => return Err("Kotlin does not support tuple types".to_owned()),
49//         DataType::Map(t) => format!(
50//             "HashMap<{}, {}>",
51//             datatype(&t.key_ty())?,
52//             datatype(&t.value_ty())?
53//         ),
54//         DataType::Generic(t) => t.to_string(),
55//         DataType::Reference(reference) => {
56//             // let name = reference.name();
57//             let generics = reference.generics();
58
59//             // match &generics[..] {
60//             //     [] => name.to_string(),
61//             //     generics => {
62//             //         let generics = generics
63//             //             .iter()
64//             //             .map(|(_, t)| datatype(t))
65//             //             .collect::<Result<Vec<_>, _>>()?
66//             //             .join(", ");
67
68//             //         format!("{name}<{generics}>")
69//             //     }
70//             // }
71//             todo!();
72//         }
73//         DataType::Nullable(t) => format!("{}?", datatype(&t)?),
74//         DataType::Struct(s) => {
75//             let name = s.name();
76//             let generics = s.generics();
77//             let fields = s.fields();
78//             let tag = s.tag();
79
80//             // let decl = match &fields[..] {
81//             //     [] => "class {name}".to_string(),
82//             //     fields => {
83//             //         let generics = (!generics.is_empty())
84//             //             .then(|| format!("<{}>", generics.join(", ")))
85//             //             .unwrap_or_default();
86
87//             //         let fields = fields
88//             //             .iter()
89//             //             .map(|f| {
90//             //                 let name = &f.name;
91//             //                 let typ = datatype(&f.ty)?;
92//             //                 let optional = matches!(f.ty, DataType::Nullable(_))
93//             //                     .then(|| "= null")
94//             //                     .unwrap_or_default();
95
96//             //                 Ok(format!("\tvar {name}: {typ}{optional}"))
97//             //             })
98//             //             .collect::<Result<Vec<_>, String>>()?
99//             //             .join(", ");
100
101//             //         let tag = tag
102//             //             .clone()
103//             //             .map(|t| format!("var {t}: String"))
104//             //             .unwrap_or_default();
105
106//             //         format!("data class {name}{generics} ({fields}{tag})")
107//             //     }
108//             // };
109//             // format!("@Serializable\n{decl}\n")
110//             todo!();
111//         }
112//         DataType::Literal(_) => return Err("Kotlin does not support literal types!".to_owned()),
113//         _ => todo!(),
114//     })
115// }