[cargo-cljs "0.1.0"]
cargo-cljs {:mvn/version "0.1.0"}
Install the wasm toolchain
$ rustup default nightly
$ rustup update
$ rustup component add wasm32-unknown-unknown --toolchain nightly
$ cargo install --git https://github.com/alexcrichton/wasm-gc
Run $rustup update
periodically to update the toolchain.
(ns your-app.core
(:require [cljs.core.async :refer [take!]]
[cargo.api :as cargo]))
(def cfg
{:project-name "your-project"
:dir "path/to/your-project" ;; must contain Cargo.toml
:target :wasm ;; thats it!
:verbose true ;; toggle status messages
:release? true})
(defn build []
(take! (api/build-wasm cfg) ;<-- all build fns return promise chans
(fn [[err {:keys [buffer]}]] ;<-- channels yield [?err ?ok]
(if err
(api/report-error err) ;<---just a logging helper
(send-module-somewhere buffer))))
This library works by converting your configuration map into an appropriate cargo shell command+env, spawning out a cargo process, and simplifying the output into a map. If the command fails, the map will yield to your promise-chan as:
;; To understand output maps, please read the docstring for `cargo/report.cljs`
[{
:type :some-error
:warnings [{}..] ;<-- you are failing lint checks
:errors [{}..] ;<-- compilation failed
:stdout [...] ;<-- everything not recognized as a compiler message (ie from your bin if any)
:stderr [...] ;<-- human readable status from cargo + from your bin if any
}]
Each build command is asynchronous. and there may be several async commands chained together before they reach the user. If any one of them fails, the point of failure will include a :type :description
entry and will short circuit back to you as [{:type ...}]
.
If the command succeeds, the result will resemble:
[nil {:warnings [...]
:stdout [...]}]
Notice the warnings key! Even if your build succeeded, there may still be warnings. Don't forget to fix them.
(cargo.api/cargo-build cfg)
(cargo.api/cargo-run cfg)
:bin-args ""
(cargo.api/cargo-test cfg)
(cargo.api/build-wasm cfg)
(cargo.api/build-wasm-local cfg ?importOptions)
(cargo.api/report-error err)
(cargo.api/last-result)
(cargo.api/get-stdout)
(cargo.api/get-stderr)
(cargo.api/get-warnings)
(cargo.api/get-errors)
(cargo.api/render-warnings ?warnings)
(get-warnings)
(cargo.api/render-errors ?errors)
(get-errors)
The default logging scheme found in cargo.util
is intended to work with chrome devtools rigged up to your nodejs process. It is worthwhile to take the time to set this up, or even use electron. There are many chrome extensions for doing this automatically: be wary of them! A little convenience is not worth the risk.
Each function just adds some simple coloring cues. You can override every log fn with (cargo.util/override-all! f)
or individually with (cargo.util/override-map! {...})
if you dislike them.
{
:target :wasm ;; if omitted uses system native default. TODO other targets
:release true ;; TODO there are configurable opt levels too
:cargo-verbose false ;; not terribly useful
:bin-args "a string that is passed to your binary ie give it json"
:features ["passed" "to" "--features" "flag"]
:env {} ;; merged with env created by rest of cfg, will override clashes
:rustflags {:cfg ["some_cfg_opt"]
:allow [:dead_code
:unused_imports
:non_snake_case
:unused_parens
:unused_variables
:non_camel_case_types]}}
Writing rust is hard enough without wrangling the angry elder god that is rustc. Let's make it as easy as possible!
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close