specta-elm 0.0.1

Yey! now your Rust types in Elm ;)
Documentation
use specta::{
    Types,
    datatype::{DataType, Primitive},
};

use crate::{
    Error, Exporter,
    elm::{ELM_ENUM_SYMBOL, ELM_STRUCT_SYMBOL},
    module::ModuleImports,
    types::NDT,
};

pub const INTEGER: &str = "Int";
pub const FLOAT: &str = "Float";
pub const BOOL: &str = "Bool";
pub const STRING: &str = "String";
pub const NULL: &str = "Null";
pub const INDENT: &'static str = "    ";

pub fn render(p: &Primitive, location: String) -> Result<&'static str, Error> {
    use specta::datatype::Primitive::*;

    Ok(match p {
        i8 | i16 | i32 | u8 | u16 | u32 => INTEGER,
        // `null` comes from `NaN`, `Infinity` and `-Infinity`. Is done by JS APIs and Serde JSON.
        f16 | f32 | f64 /* this looks wrong: (having | null which I deleted) but `f64` is the direct equivalent of `number` */ => FLOAT ,
        usize | isize | i64 | u64 | i128 | u128 | f128 => {
            return Err(Error::bigint_forbidden(location));
        }
        Primitive::bool => BOOL,
        str | char => STRING,
    })
}

// pub(crate) fn export_internal<'a, E: Exporter>(
//     s: &mut String,
//     imports: &'a mut ModuleImports,
//     exporter: &E,
//     types: &Types,                                     // Original
//     ndts: impl IntoIterator<Item = &'a NamedDataType>, // Module
// ) -> Result<&'a mut ModuleImports, Error> {
//     let ndts = ndts.into_iter().filter(|ndt| ndt.ty.is_some());
//
//     for ndt in ndts {
//         export_single_internal(s, imports, exporter, types, &ndt.into())?;
//     }
//
//     Ok(imports)
// }

pub(crate) fn export_internal<'a, E: Exporter>(
    s: &mut String,
    imports: &'a mut ModuleImports,
    exporter: &E,
    types: &Types,
    ndt: &NDT,
) -> Result<&'a mut ModuleImports, Error> {
    let name = ndt
        .sanitise_type_name()
        .map_err(|err| err.with_named_datatype(ndt))?;

    if let Some(dt) = ndt.inner().ty.as_ref() {
        let type_def = match dt {
            DataType::Enum(_) => ELM_ENUM_SYMBOL.to_string() + &name + "\n" + INDENT + "= ",
            _ => ELM_STRUCT_SYMBOL.to_string() + &name + " = ",
        };

        s.push_str(&type_def)
    }

    let body_dt = ndt.inner().ty.as_ref().expect("ups no body man");
    ndt.render(
        s,
        imports,
        exporter,
        types,
        &body_dt,
        Some(ndt.inner().name.as_ref()),
    )
    .map_err(|err| err.with_named_datatype(ndt))?;
    s.push_str("\n\n");

    Ok(imports)
}