Skip to content

Instantly share code, notes, and snippets.

View MartinKavik's full-sized avatar

Martin Kavík MartinKavik

View GitHub Profile
@MartinKavik
MartinKavik / palindrome.rs
Last active April 14, 2020 19:00
Palindrome checker (a part)
use seed::{*, prelude::*};
fn init(_: Url, _: &mut impl Orders<Msg>) -> Model {
Model::default()
}
#[derive(Default)]
struct Model {
input: String,
}
#![allow(dead_code)]
type CSS = String;
type CSSValue = &'static str;
type HTML = String;
struct S_;
impl S_ {
fn b_color(&self, value: impl ToString) -> CSS {
format!("background-color: {};", value.to_string())
@MartinKavik
MartinKavik / handler_msg_or_unit_compile_validation.rs
Last active March 23, 2020 20:20
Return `Msg`, `Option<Msg>` or `()` in handlers. | Compile-time validation | Nightly Rust
#![feature(optin_builtin_traits)]
//#![feature(negative_impls)]
#[derive(Debug)]
enum Msg {
Increment,
Decrement
}
fn main() {
@MartinKavik
MartinKavik / assert_type_eq_all_problem.rs
Last active March 15, 2020 14:26
uncomment line 53 and run in Rust playground
use std::any::{Any, TypeId};
#[derive(Debug)]
enum Msg {
Increment,
}
// copy-paste from static_assertions crate
#[macro_export]
macro_rules! assert_type_eq_all {
@MartinKavik
MartinKavik / static_handler_return.rs
Created March 15, 2020 11:46
Return `Msg` or `()` in handlers. - compile check
#[derive(Debug)]
enum Msg {
Increment,
}
// can write procedural macro for derviing
impl MsgMarker for Msg {}
fn main() {
let handlers: Vec<Handler<Msg>> = vec![
@MartinKavik
MartinKavik / handler_msg_or_unit_panics.rs
Last active March 15, 2020 17:29
Return `Msg` or `()` in handlers. | Runtime panics | Stable Rust
use std::any::{Any, TypeId};
#[derive(Debug)]
enum Msg {
Increment,
}
fn main() {
let handlers: Vec<Handler<Msg>> = vec![
Handler::new(|| Msg::Increment),
@MartinKavik
MartinKavik / markdown_editor_lib.rs
Created February 26, 2020 07:58
Step 5 - final code
#![feature(track_caller)]
use comp_state::*;
use comp_state_seed_extras::*;
use seed::{prelude::*, *};
use web_sys::{HtmlElement, HtmlTextAreaElement};
#[derive(Default)]
struct Model {}
@MartinKavik
MartinKavik / code-block.js
Created February 22, 2020 21:20
code-block custom element - highlightJS + lit-element
import { LitElement, html, css } from 'https://unpkg.com/lit-element/lit-element.js?module';
import { unsafeHTML } from 'https://unpkg.com/lit-html/directives/unsafe-html.js?module';
class CodeBlockElement extends LitElement {
static get properties() { return {
lang: "",
code: "",
};}
render() {
@MartinKavik
MartinKavik / canvas.rs
Last active November 21, 2019 11:02
Canvas example with ElRef
//! [Web-sys docs](https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.CanvasRenderingContext2d.html)
//! [Web-sys example](https://rustwasm.github.io/wasm-bindgen/examples/2d-canvas.html)
//! [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawWindow)
#[macro_use]
extern crate seed;
use seed::prelude::*;
use web_sys::HtmlCanvasElement;
type Color = &'static str;
@MartinKavik
MartinKavik / lib.rs
Created August 16, 2019 09:58
Toy seed app refactored
// Replace *lib.rs* in the seed-quickstart project with this file,
// then run `cargo make build && cargo make serve`.
#[macro_use]
extern crate seed;
use seed::prelude::*;
// Model