-
Notifications
You must be signed in to change notification settings - Fork 0
Rust documentation #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Rust documentation #181
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b15ae4
Fix Rust codegen using 'lbf-prelude-rust'
szg251 1ff2a4f
Add Rust documentation
szg251 c9d9d6b
Merge branch 'main' into szg251/add-docs
szg251 bbe3dbf
Add Rust doc to summary
szg251 c305fd8
Fix Rust doc
szg251 8d74c91
Merge branch 'main' into szg251/add-docs
szg251 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "crate": [ | ||
| "Document" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # LambdaBuffers to Rust | ||
|
|
||
| Let's take a look at how LambdaBuffers modules map into Rust modules and how | ||
| LambdaBuffers type definitions map into Rust type definitions. | ||
|
|
||
| We'll use the `lbf-prelude-to-rust` CLI tool which is just a convenient wrapper over | ||
| the raw `lbf` CLI. We can get this tool by either loading the LambdaBuffers Nix | ||
| environment that comes packaged with all the CLI tools: | ||
|
|
||
| ```shell | ||
| $ nix develop github:mlabs-haskell/lambda-buffers#lb | ||
| $ lbf<tab> | ||
| lbf lbf-plutus-to-purescript lbf-prelude-to-haskell lbf-prelude-to-rust | ||
| lbf-plutus-to-haskell lbf-plutus-to-rust lbf-prelude-to-purescript | ||
| ``` | ||
|
|
||
| Or we can simply just refer directly to the `lbf-prelude-to-rust` CLI by `nix run | ||
| github:mlabs-haskell/lambda-buffers#lbf-prelude-to-rust`. | ||
|
|
||
| In this chapter, we're going to use the latter option. | ||
|
|
||
| Let's now use `lbf-prelude-to-rust` to process the [Document.lbf](https://github.com/mlabs-haskell/lambda-buffers/tree/main/docs/examples/Document.lbf) schema. | ||
|
|
||
| ```purescript | ||
| module Document | ||
|
|
||
| -- Importing types | ||
| import Prelude (Text, List, Set, Bytes) | ||
|
|
||
| -- Author | ||
| sum Author = Ivan | Jovan | Savo | ||
|
|
||
| -- Reviewer | ||
| sum Reviewer = Bob | Alice | ||
|
|
||
| -- Document | ||
| record Document a = { | ||
| author : Author, | ||
| reviewers : Set Reviewer, | ||
| content : Chapter a | ||
| } | ||
|
|
||
| -- Chapter | ||
| record Chapter a = { | ||
| content : a, | ||
| subChapters : List (Chapter a) | ||
| } | ||
|
|
||
| -- Some actual content | ||
| sum RichContent = Image Bytes | Gif Bytes | Text Text | ||
|
|
||
| -- Rich document | ||
| prod RichDocument = (Document RichContent) | ||
| ``` | ||
|
|
||
| ```shell | ||
| $ nix run github:mlabs-haskell/lambda-buffers#lbf-prelude-to-rust -- Document.lbf | ||
| $ find autogen/ | ||
| autogen/ | ||
| autogen/LambdaBuffers | ||
| autogen/LambdaBuffers/Document.hs | ||
| autogen/build.json | ||
| ``` | ||
|
|
||
| As we can see the `autogen` directory has been created that contains the generated Rust modules. | ||
| Note the `autogen/build.json` file as it contains all the necessary Cargo dependencies the generated module needs in order to be properly compiled by Rust. | ||
|
|
||
| The outputted Rust module in `autogen/document.rs`: | ||
|
|
||
| ```rust | ||
| #![no_implicit_prelude] | ||
| #![allow(warnings)] | ||
| extern crate lbf_prelude; | ||
| extern crate std; | ||
|
|
||
|
|
||
| #[derive(std::fmt::Debug, std::clone::Clone)] | ||
| pub enum Author{Ivan, Jovan, Savo} | ||
|
|
||
| #[derive(std::fmt::Debug, std::clone::Clone)] | ||
| pub struct Chapter<A>{pub content: A, | ||
| pub sub_chapters: std::boxed::Box<lbf_prelude::prelude::List<Chapter<A>>>} | ||
|
|
||
| #[derive(std::fmt::Debug, std::clone::Clone)] | ||
| pub struct Document<A>{pub author: Author, | ||
| pub reviewers: lbf_prelude::prelude::Set<Reviewer>, | ||
| pub content: Chapter<A>} | ||
|
|
||
| #[derive(std::fmt::Debug, std::clone::Clone)] | ||
| pub enum Reviewer{Bob, Alice} | ||
|
|
||
| #[derive(std::fmt::Debug, std::clone::Clone)] | ||
| pub enum RichContent{Image(lbf_prelude::prelude::Bytes), | ||
| Gif(lbf_prelude::prelude::Bytes), | ||
| Text(lbf_prelude::prelude::Text)} | ||
|
|
||
| #[derive(std::fmt::Debug, std::clone::Clone)] | ||
| pub struct RichDocument(pub Document<RichContent>); | ||
|
|
||
| ``` | ||
|
|
||
| ## Sum types | ||
|
|
||
| The types `Author`, `Reviewer`, and `RichContent` have been declared as sum types in the LambdaBuffers schema using the `sum` keyword. | ||
|
|
||
| As we can see, nothing too surprising here, all the `sum` types become `enum` | ||
| in Rust. | ||
|
|
||
| ## Product types | ||
|
|
||
| The type `RichDocument` have been declared as a product type in the | ||
| LambdaBuffers schema using the `prod` keyword. | ||
|
|
||
| They become Rust tuple `struct` (or named tuple) | ||
|
|
||
| ## Record types | ||
|
|
||
| The types `Document` and `Chapter` have been declared as record types in the | ||
| LambdaBuffers schema using the `record` keyword. | ||
|
|
||
| Like with product types, they become Rust `struct` with named fields. | ||
|
|
||
| All types and their fields are public, allowing to manipulate them without accessors. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.