Skip to content

Provides easy communication between Elm and Rust by leveraging serde

License

Notifications You must be signed in to change notification settings

mijnadres/elm_export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

elm_export

Provides easy communication between Elm and Rust by leveraging syn.

Goal

The goal of this project is to provide a bridge between Elm and Rust. We envision a workflow where models are created in Rust, are analyzed by syn and compiled to Elm source files which are serialized by serde.

This would allow easy communication between an Elm front-end and a Rust backend by correctly serializing and deserializing data structures on both ends of the pipeline.

Usage

Notice that at the moment some of this is dreamcode.

Lets say we have some models in Rust.

enum Message {
  FriendRequest(User),
  Message(User, String),
}

struct User {
  name: String
}

We want to generated the corresponding models in Elm. For this we need a dependency on the elm_export crate. Include the following line in your Cargo.toml.

[dependencies]
elm_export = "0.1.0"

Next we need to make our project aware of the crate and the functionality it exposes. Import it in either main.rs or lib.rs. Don't forget to annotate the import with the macro_use annotation.

#[macro_use]
extern crate elm_export;

Now it is time to derive the corresponding models in Elm. Annotate the models with derive(Elm).

#[derive(Elm)]
enum Message {
  FriendRequest(User),
  Message(User, String),
}

#[derive(Elm)]
struct User {
  name: String
}

Now every time your models change and you compile your code the corresponding Elm models will be generated. You can find them in the generated directory in your projects root. The are called after the corresponding Rust definitions. I.e. Message.elm and User.elm in this case.

module Message exposing (..)

type Message =
    FriendRequest User
  | Message User String
module User exposing (..)

type alias User = 
  {
    name: String
  }

Flavor of Elm

Json.Encode and Json.Decode are core Elm packages responsible for converting Elm values into and from JSON. Anyone who creates decoders quickly discovers NoRedInk/elm-decode-pipeline. It is this flavor of decoding that we will targeting with this project.

Roadmap

In order to make the above code happen the following tasks need to be fulfilled.

  • Generate Elm models from Rust models.
  • Generate Elm decoders from Rust models.
  • Generate Elm encoders from Rust models.

Nice to haves

Although not strictly necessary, the following items are nice to have. No promises are made when, if at all, these are implemented.

  • Have proper import statements in the Elm code.

Contributing

Check out the contribution guideline if you want to contribute.

About

Provides easy communication between Elm and Rust by leveraging serde

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published