6.0.1-alpha.15 • Published 24 days ago

@rainlanguage/dotrain v6.0.1-alpha.15

Weekly downloads
-
License
CAL-1.0
Repository
github
Last release
24 days ago

npm.io

Rain Language Composer and LSP

The Rain language server protocol (LSP) services implementation (language services) and .rain composer written in rust and made available for NodeJs and broswers through wasm-bindgen in Typescript/Javascript which makes it well suited for editors and IDEs (as it is used in Rainlang vscode and codemirror language extension).

  • Dotrain specs can be found here
  • Rainlang specs can be found here
  • Dotrain has been implemented for vscode and codemirror, see rainlang-vscode and rainlang-codemirror repositories for more details.
  • Dotrain vscode extension can be found here.

The primary goal of the Rain language is to make smart contract development accessible for as many people as possible. This is fundamentally grounded in our belief that accessibility is the difference between theoretical and practical decentralisation. There are many people who would like to participate in authoring and auditing crypto code but currently cannot. When someone wants/needs to do something but cannot, then they delegate to someone who can, this is by definition centralisation.

For more info and details, please read this article

If you find an issue or you want to propose an improvement, please feel free to post it on: issues


Tutorial

Javascript/Typescript

To get started, install the package:

npm install @rainlanguage/dotrain

or

yarn add @rainlanguage/dotrain
// imports
import { RainLanguageServices, MetaStore, TextDocumentItem } from "@rainlanguage/dotrain";

// instantiate a MetaStore which is a in-memory CAS for Rain metadata
const metaStore = new MetaStore();

// some text document
const textDocument = TextDocumentItem.create(
  "file:///file-name.rain",
  "rainlang",
  0,
  "some dotrain text"
);

// initiating the services (metaStore is optional)
const langServices = new RainLanguageServices(metaStore);

// getting validation results (lsp Diagnostics)
const diagnostics = await langServices.doValidate(textDocument);

// instantiate a new RainDocument
const rainDocument = await langServices.newRainDocument(textDocument)

// composing a RainDocument to get rainlang string
const rainlangText = await rainDocument.compose(["entrypoint-1" , "entrypoint-2"]);

Rust

dotrain crate

To get started, install the package:

cargo add dotrain

Features

  • cli: A clap based module (CLI app) for functionalities of this library, this features is required for building the binary
  • js-api: includes wrappers around main structs and functionalities to provide an API through wasm-bindgen (this feature is always enabled when building for wasm family targets)
use std::sync::{Arc, RwLock};
use dotrain::{Store, RainDocument};

let text = "some text".to_string();

// instantiate arc locked Store
let meta_store = Arc::new(RwLock::new(Store::default()));

// instantiate
let rain_document = RainDocument::new(text, Some(meta_store));

// compose this instance of RainDocument to get rainlang string
let rainlang_text = rain_document.compose(&vec!["entrypoint1", "entrypoint2"], None)?;

dotrain_lsp crate

To get started, install the package:

cargo add dotrain-lsp

Features

  • js-api: includes wrappers around main structs and functionalities to provide an API through wasm-bindgen (this feature is always enabled when building for wasm family targets)
use std::sync::{Arc, RwLock};
use dotrain::{Store, RainLanguageServices, TextDocumentItem, LanguageServiceParams, RainDocument};

// instantiate arc locked Store
let meta_store = Arc::new(RwLock::new(Store::default()));

// the following needs 'lsp' feature to be enabled
let lang_params = LanguageServiceParams {
  meta_store: Some(meta_store)
}

// a LSP TextdocumentItem
let text_document = TextDocumentItem {
  uri,
  text,
  version: 0,
  language_id: "rainlang".to_string()
}

// instantiate RainLanguageServices
let lang_services = RainLanguageServices::new(lang_params);

// get LSP diagnostics of the given text document
let diagnostics = lang_services.do_validate(&text_document, true);

CLI

The CLI app can be built using nix (requires nix package manager to be installed):

nix build github:rainlanguage/dotrain

or installed with the following command from cargo:

cargo install dotrain

this will install the dotrain binary in your path which then can be used to compose .rain files and generate outputs.

Examples

composes a .rain file with specified entrypoints

dotrain compose --input path/to/some.rain --entrypoints first --entrypoints second

optionally, path to rainconfig.json can be provided:

dotrain -c path/to/rainconfig.json --input path/to/some.rain --entrypoints first --entrypoints second

Dotrain cli is also available in Rain CLI app which can be easily run with nix:

nix run github:rainlanguage/rain.cli -- --help

rainconfig

Configuration details for .rain composer (source files and subgraphs). Following command will print info about rainconfig and its fields:

dotrain rainconfig <COMMAND>

Here is an example of a rainconfig.json:

{
  "include": ["./folder1", "./folder2"],
  "subgraphs": [
    "https://subgraph1-url",
    "https://subgraph2-url",
    "https://subgraph3-url"
  ]
}

Building JS/TS Bindings

From the root of this repo, simply run the following to build the js bindings:

nix run .#build-js-bindings

This will build the rust library with wasm32-unknown-unknown target in release mode with js-api feature enabled and then generates bindings using wasm-bindgen-cli into ./dist directory by encoding the wasm binary into a json as importing json is native in js/ts and eliminates the need for using fetch/fs operations when loading the wasm module.

To generate js/ts documents:

nix run .#js-bindings-docs

To run tests:

nix run .#test-js-bindings
6.0.1-alpha.15

24 days ago

6.0.1-alpha.14

2 months ago

6.0.1-alpha.13

2 months ago

6.0.1-alpha.11

2 months ago

6.0.1-alpha.10

3 months ago

6.0.1-alpha.9

3 months ago

6.0.1-alpha.8

3 months ago

6.0.1-alpha.6

3 months ago