Browse Source

Used web-sys to add codons to DOM

pull/1/head
Ravi Shah 5 years ago
parent
commit
a02de2b147
  1. 10
      Cargo.toml
  2. 22
      src/lib.rs

10
Cargo.toml

@ -26,6 +26,16 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. # Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true } wee_alloc = { version = "0.4.5", optional = true }
[dependencies.web-sys]
version = "0.3.50"
features = [
'Document',
'Element',
'HtmlElement',
'Node',
'Window',
]
[dev-dependencies] [dev-dependencies]
wasm-bindgen-test = "0.3.13" wasm-bindgen-test = "0.3.13"

22
src/lib.rs

@ -1,7 +1,6 @@
mod utils; mod utils;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use std::io;
use std::str; use std::str;
use std::process; use std::process;
@ -12,8 +11,9 @@ use std::process;
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen] #[wasm_bindgen]
extern {
fn alert(s: &str);
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
} }
pub fn transcription(dna: String) -> String { pub fn transcription(dna: String) -> String {
@ -160,9 +160,21 @@ pub fn main(mut strand: String) {
println!("{}", stop_index); println!("{}", stop_index);
inter_codons.truncate(stop_index); inter_codons.truncate(stop_index);
let amino_acids_list = translation(inter_codons); let amino_acids_list = translation(inter_codons);
// Use `web_sys`'s global `window` function to get a handle on the global
// window object.
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
let body = document.body().expect("document should have a body");
let val = document.create_element("h2").unwrap();
val.set_text_content(Some("Codons:"));
body.append_child(&val).unwrap();
print!("The translated amino acids are: "); print!("The translated amino acids are: ");
for i in amino_acids_list { for i in amino_acids_list {
print!("{}, ", i);
alert(&i);
let val = document.create_element("p").unwrap();
val.set_text_content(Some(&i));
body.append_child(&val).unwrap();
} }
} }
Loading…
Cancel
Save