go2rs

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2024 License: MIT Imports: 2 Imported by: 0

README

go2rs

PkgGoDev

What is go2rs?

  • go2rs is a Rust struct generator from Go structs
  • Automatically recognizes Go modules in the directory and generates equivalent Rust types
  • Handles Go-to-Rust type conversions with appropriate derives and attributes

Installation

$ go get github.com/drewstone/go2rs

Usage

// ./example/main.go
package main

import (
    "time"
)

type Status string

const (
    StatusOK Status = "OK"
    StatusFailure Status = "Failure"
)

type Param struct {
    Status    Status
    Version   int
    Action    string
    CreatedAt time.Time
}
$ go2rs ./example

Generates:

use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum Status {
    OK,
    Failure,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Param {
    pub status: Status,
    pub version: i32,
    pub action: String,
    #[serde(with = "chrono::serde::ts_seconds")]
    pub created_at: DateTime<Utc>,
}

Features

  • Converts Go types to idiomatic Rust types
  • Handles common Go patterns like string enums
  • Adds appropriate serde derives and attributes
  • Supports time.Time conversion to chrono::DateTime
  • Maintains field visibility and naming conventions
  • Generates documentation from Go comments

TODO

  • Handle custom MarshalJSON/UnmarshalJSON implementations

Acknowledgements

This is entirely built using go2ts by go-generalize as a reference and porting over the same concepts to Rust.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Place this file in the root directory

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any added in v0.1.3

type Any = rstypes.Any

Specific types

func NewAny added in v0.1.3

func NewAny() *Any

Constructor functions

type Array added in v0.1.3

type Array = rstypes.Array

Re-export all types

func NewArray added in v0.1.3

func NewArray(inner Type) *Array

type Boolean added in v0.1.3

type Boolean = rstypes.Boolean

Re-export all types

func NewBoolean added in v0.1.3

func NewBoolean() *Boolean

type Common added in v0.1.3

type Common = rstypes.Common

Re-export all types

type Date added in v0.1.3

type Date = rstypes.Date

Re-export all types

func NewDate added in v0.1.3

func NewDate() *Date

type Enum added in v0.1.3

type Enum = rstypes.Enum

Re-export all types

func NewEnum added in v0.1.3

func NewEnum(name string) *Enum

type EnumVariant added in v0.1.3

type EnumVariant = rstypes.EnumVariant

Re-export all types

type Enumerable added in v0.1.3

type Enumerable = rstypes.Enumerable

Re-export all types

type Function added in v0.1.3

type Function = rstypes.Function

Re-export all types

func NewFunction added in v0.1.3

func NewFunction() *Function

type FunctionParam added in v0.1.3

type FunctionParam = rstypes.FunctionParam

Re-export all types

type Generator

type Generator = generator.Generator

Re-export the Generator type and constructor

func NewGenerator

func NewGenerator(types map[string]rstypes.Type) *Generator

type Map added in v0.1.3

type Map = rstypes.Map

Re-export all types

func NewMap added in v0.1.3

func NewMap(key, value Type) *Map

type NamedType added in v0.1.3

type NamedType = rstypes.NamedType

Re-export all types

type Nullable added in v0.1.3

type Nullable = rstypes.Nullable

Re-export all types

func NewNullable added in v0.1.3

func NewNullable(inner Type) *Nullable

type Number added in v0.1.3

type Number = rstypes.Number

Re-export all types

func NewNumber added in v0.1.3

func NewNumber() *Number

type Primitive added in v0.1.3

type Primitive = rstypes.Primitive

Re-export all types

func NewPrimitive added in v0.1.3

func NewPrimitive(name string) *Primitive

type Result added in v0.1.3

type Result = rstypes.Result

Re-export all types

func NewResult added in v0.1.3

func NewResult(ok, err Type) *Result

type Stream added in v0.1.3

type Stream = rstypes.Stream

Re-export all types

func NewStream added in v0.1.3

func NewStream(inner Type) *Stream

type String added in v0.1.3

type String = rstypes.String

Re-export all types

func NewString added in v0.1.3

func NewString() *String

type Struct added in v0.1.3

type Struct = rstypes.Struct

Re-export all types

func NewStruct added in v0.1.3

func NewStruct(name string) *Struct

type StructField added in v0.1.3

type StructField = rstypes.StructField

Field types

type Trait added in v0.1.3

type Trait = rstypes.Trait

Re-export all types

func NewTrait added in v0.1.3

func NewTrait(name string) *Trait

type Tuple added in v0.1.3

type Tuple = rstypes.Tuple

Re-export all types

func NewTuple added in v0.1.3

func NewTuple() *Tuple

type Type added in v0.1.3

type Type = rstypes.Type

Core types

type Unit added in v0.1.3

type Unit = rstypes.Unit

Re-export all types

func NewUnit added in v0.1.3

func NewUnit() *Unit

type Vec added in v0.1.3

type Vec = rstypes.Vec

Re-export all types

func NewVec added in v0.1.3

func NewVec(inner Type) *Vec

Directories

Path Synopsis
pkg
generator
Package generator is Rust generator from AST
Package generator is Rust generator from AST
parser
Package parser provides a Go module parser for TypeScript AST
Package parser provides a Go module parser for TypeScript AST
parser/testutil
Package testutil offers test utility for parser
Package testutil offers test utility for parser
types
Package types contains structs/interfaces representing Rust types
Package types contains structs/interfaces representing Rust types
util
Package util is a helper library for go2ts
Package util is a helper library for go2ts

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL