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
//! # Go Away
//!
//! Go Away is a small library for generating go types & marshalling code from Rust type
//! definitions.  It's intended for use when you have existing rust code that is using serde for
//! JSON serialization and you want to allow go services or clients to interact with that code.
//!
//! It may be expanded to other languages at some point but it's mostly been built to service a
//! very specific need and might never evolve past that.
//!
//! Use is fairly simple:
//!
//! ```rust
//! use go_away::{TypeMetadata, TypeRegistry};
//!
//! // First, derive TypeMetadata on some types:
//!
//! #[derive(TypeMetadata)]
//! struct MyType {
//!     my_field: String
//! }
//!
//! // Then you can register this type inside a `TypeRegistry`
//!
//! let mut registry = TypeRegistry::new();
//! MyType::metadata(&mut registry);
//!
//! // And get some go code to write out to a file:
//! let go_code = go_away::registry_to_output::<go_away::GoType>(&registry);
//! ```
//!
//! Note that the output go code does not contain any package definitions or required imports.
//! It's expected that any code that calls `go-away` will add this for itself.
//!

#![warn(missing_docs)]

mod alias;
mod metadata;
mod output;
mod registry;
mod type_id;

pub mod types;

pub use alias::TypeAlias;
pub use metadata::TypeMetadata;
pub use output::{GoType, TypeScriptType};
pub use registry::TypeRegistry;
pub use type_id::TypeId;

pub use go_away_derive::TypeMetadata;

use registry::Type;
use std::fmt::Display;

/// Generates code for all the types in the TypeRegistry
///
/// You should provide `go_away::GoType` or `go_away::TypeScriptType` as a generic
/// parameter with turbofish syntax to decide which format to output.
///
/// Note that this is a WIP API and is likely to be ditched/changed in future releases.
pub fn registry_to_output<'a, Format>(registry: &'a TypeRegistry) -> String
where
    Format: From<&'a Type> + Display,
{
    use std::fmt::Write;

    let mut output = String::new();
    for id in registry.structs.iter().rev() {
        let ty = registry.types.get(id).unwrap();
        write!(&mut output, "{}", Format::from(ty)).unwrap();
    }
    for id in registry.unions.iter().rev() {
        let ty = registry.types.get(id).unwrap();
        write!(&mut output, "{}", Format::from(ty)).unwrap();
    }
    for id in registry.newtypes.iter().rev() {
        let ty = registry.types.get(id).unwrap();
        write!(&mut output, "{}", Format::from(ty)).unwrap();
    }
    for id in registry.enums.iter().rev() {
        let ty = registry.types.get(id).unwrap();
        write!(&mut output, "{}", Format::from(ty)).unwrap();
    }
    for id in registry.aliases.iter().rev() {
        let ty = registry.types.get(id).unwrap();
        write!(&mut output, "{}", Format::from(ty)).unwrap();
    }

    output
}

impl<'a> From<&'a registry::Type> for output::go::GoType<'a> {
    fn from(ty: &'a registry::Type) -> Self {
        match ty {
            registry::Type::Struct(inner) => output::go::GoType::Struct(inner),
            registry::Type::Enum(inner) => output::go::GoType::Enum(inner),
            registry::Type::Union(inner) => output::go::GoType::Union(inner),
            registry::Type::NewType(inner) => output::go::GoType::NewType(inner),
            registry::Type::Alias(inner) => output::go::GoType::Alias(inner),
        }
    }
}

impl<'a> From<&'a registry::Type> for output::typescript::TypeScriptType<'a> {
    fn from(ty: &'a registry::Type) -> Self {
        match ty {
            registry::Type::Struct(inner) => output::typescript::TypeScriptType::Struct(inner),
            registry::Type::Enum(inner) => output::typescript::TypeScriptType::Enum(inner),
            registry::Type::Union(inner) => output::typescript::TypeScriptType::Union(inner),
            registry::Type::NewType(inner) => output::typescript::TypeScriptType::NewType(inner),
            registry::Type::Alias(inner) => output::typescript::TypeScriptType::Alias(inner),
        }
    }
}