From a02de2b1476a1493c2044511046984a75c4c7b7e Mon Sep 17 00:00:00 2001 From: rav4s Date: Wed, 31 Mar 2021 23:47:22 -0500 Subject: [PATCH] Used web-sys to add codons to DOM --- Cargo.toml | 10 ++++++++++ src/lib.rs | 22 +++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fcfaf08..b2230e4 100644 --- a/Cargo.toml +++ b/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. wee_alloc = { version = "0.4.5", optional = true } +[dependencies.web-sys] +version = "0.3.50" +features = [ + 'Document', + 'Element', + 'HtmlElement', + 'Node', + 'Window', +] + [dev-dependencies] wasm-bindgen-test = "0.3.13" diff --git a/src/lib.rs b/src/lib.rs index fa781a7..23d427a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ mod utils; use wasm_bindgen::prelude::*; -use std::io; use std::str; use std::process; @@ -12,8 +11,9 @@ use std::process; static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; #[wasm_bindgen] -extern { - fn alert(s: &str); +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); } pub fn transcription(dna: String) -> String { @@ -160,9 +160,21 @@ pub fn main(mut strand: String) { println!("{}", stop_index); inter_codons.truncate(stop_index); 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: "); 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(); } }