Sauron é uma estrutura versátil da Web e biblioteca para criar aplicativos da Web do lado do cliente e/ou do servidor, com forte foco em ergonomia, simplicidade e elegância. Isso permite que você escreva o mínimo de código possível e se concentre mais na lógica de negócios do que nos detalhes internos da estrutura.
Sauron é inspirado em Elm-Lang e está seguindo a arquitetura do ELM.
Em um aplicativo Sauron, existe apenas o modelo, a visualização e a atualização. O modelo é o seu estado de aplicação. A visualização descreve como apresentar o modelo ao usuário. A função de atualização descreve como atualizar o modelo, isso usa a mensagem que contém os dados necessários para atualizar o modelo.
Em seu src/lib.rs
use sauron :: {
html :: text , html :: units :: px , jss , node , wasm_bindgen , Application , Cmd , Node , Program ,
} ;
enum Msg {
Increment ,
Decrement ,
Reset ,
}
struct App {
count : i32 ,
}
impl App {
fn new ( ) -> Self {
App { count : 0 }
}
}
impl Application for App {
type MSG = Msg ;
fn view ( & self ) -> Node < Msg > {
node ! {
<main>
<input type = "button"
value= "+"
on_click=|_| {
Msg :: Increment
}
/>
<button class= "count" on_click=|_| { Msg :: Reset } > { text ( self . count ) } </button>
<input type = "button"
value= "-"
on_click=|_| {
Msg :: Decrement
}
/>
</main>
}
}
fn update ( & mut self , msg : Msg ) -> Cmd < Msg > {
match msg {
Msg :: Increment => self . count += 1 ,
Msg :: Decrement => self . count -= 1 ,
Msg :: Reset => self . count = 0 ,
}
Cmd :: none ( )
}
fn stylesheet ( ) -> Vec < String > {
vec ! [ jss! {
"body" : {
font_family : "verdana, arial, monospace" ,
} ,
"main" : {
width : px ( 30 ) ,
height : px ( 100 ) ,
margin : "auto" ,
text_align : "center" ,
} ,
"input, .count" : {
font_size : px ( 40 ) ,
padding : px ( 30 ) ,
margin : px ( 30 ) ,
}
} ]
}
}
# [ wasm_bindgen ( start ) ]
pub fn start ( ) {
Program :: mount_to_body ( App :: new ( ) ) ;
} index.html
<!doctype html >
< html >
< head >
< meta content =" text/html;charset=utf-8 " http-equiv =" Content-Type " />
< title > Counter </ title >
< script type = module >
import init from './pkg/counter.js' ;
await init ( ) . catch ( console . error ) ;
</ script >
</ head >
< body >
</ body >
</ html > Em Cargo.toml , especifique o tipo de caixa para ser cdylib
[ package ]
name = " counter "
version = " 0.1.0 "
edition = " 2021 "
[ lib ]
crate-type = [ " cdylib " ]
[ dependencies ]
sauron = " 0.61.0 " cargo install wasm-pack
cargo install basic-http-serverConstruir usando
wasm-pack build --target web --releaseServir usando
basic-http-server -a 0.0.0.0:4000Em seguida, navegue para http: // localhost: 4000
Vá para o getting-started.md para o tutorial completo.
Para obter mais detalhes sobre os comandos para construir e servir, consulte exemplos neste repositório, cada um tem scripts sobre como construí -los e executá -los.
Licença: MIT