add todomvc crate and fix clippy stuff

This commit is contained in:
Michael Zhang 2020-02-19 17:46:04 -06:00
parent 1453885ed2
commit 3422ae6423
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
11 changed files with 41 additions and 61 deletions

4
Cargo.lock generated
View file

@ -370,6 +370,10 @@ dependencies = [
"syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "todomvc"
version = "0.1.0"
[[package]]
name = "unicode-xid"
version = "0.2.0"

View file

@ -13,6 +13,7 @@ members = [
"syn-serde",
"examples/helloworld",
"examples/todomvc",
]
[features]

View file

@ -1,25 +1,22 @@
#[macro_use]
extern crate quote;
#[macro_use]
extern crate maplit;
#[macro_use]
extern crate serde_derive;
pub mod model;
mod visitor;
mod tuple_map;
mod visitor;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use crate::model::{Component, Elem, Rsx, TagLhs};
use crate::model::Component;
use crate::visitor::Visitor;
use proc_macro2::TokenStream;
use symbol::Symbol;
use syn::Expr;
pub fn build(
// name: impl AsRef<str>,
@ -94,37 +91,5 @@ pub fn process(mod_name: impl AsRef<str>, code: impl AsRef<str>) {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut out_file = File::create(out_dir.join(format!("{}.rs", mod_name.as_ref()))).unwrap();
let tokens = build(&component);
write!(out_file, "{}", tokens);
write!(out_file, "{}", tokens).unwrap();
}
// #[proc_macro]
// pub fn example(_input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
// let helloworld_datamodel: HashMap<String, String> = hashmap! {
// "name".into() => "String".into(),
// };
// let helloworld_datainit: HashMap<String, String> = hashmap! {
// "name".into() => "\"world\".into()".into(),
// };
// let helloworld_dom = vec![
// Rsx::Elem(Elem {
// tag: "input".into(),
// attrs: hashmap! {
// TagLhs::Bind("value".into()) => "name".into(),
// },
// inner: vec![],
// }),
// Rsx::Text("Hello, ".into()),
// Rsx::Code(Box::new(syn::parse_str::<Expr>("name").unwrap())),
// Rsx::Text("!".into()),
// ];
// process(
// "HelloWorld",
// &helloworld_datamodel,
// &helloworld_datainit,
// &helloworld_dom,
// )
// .into()
// }

View file

@ -1,6 +1,6 @@
use std::collections::HashMap;
use std::hash::{BuildHasher, Hash};
use proc_macro2::TokenStream;
use symbol::Symbol;
use syn_serde::{Expr, Syn, Type};
@ -8,12 +8,14 @@ pub type Id = Symbol;
pub type ModelMap = HashMap<Symbol, (Type, Expr)>;
pub fn convert_map(map: HashMap<Symbol, (syn::Type, syn::Expr)>) -> ModelMap {
pub fn convert_map<T: Hash + Eq, S: BuildHasher>(
map: HashMap<T, (syn::Type, syn::Expr), S>,
) -> HashMap<T, (Type, Expr)> {
map.into_iter()
.map(|(name, (ty, expr))| {
.map(|(left, (ty, expr))| {
let ty = ty.to_adapter();
let expr = expr.to_adapter();
(name, (ty, expr))
(left, (ty, expr))
})
.collect()
}
@ -47,7 +49,7 @@ impl Clone for TagRhs {
match self {
TagRhs::Code(expr) => {
let expr: syn::Expr = Syn::from_adapter(&*expr);
TagRhs::Code(expr.clone().to_adapter())
TagRhs::Code(expr.to_adapter())
}
TagRhs::Text(string) => TagRhs::Text(string.clone()),
}

View file

@ -1,12 +1,15 @@
// https://github.com/daboross/serde-tuple-vec-map/blob/master/src/lib.rs
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
use std::fmt;
use std::cmp;
use std::collections::HashMap;
use serde::{de::{Visitor, Deserialize, Deserializer, SeqAccess}, ser::{Serialize, Serializer}};
use serde::{
de::{Deserialize, Deserializer, SeqAccess, Visitor},
ser::{Serialize, Serializer},
};
struct TupleVecMapVisitor<K, V> {
marker: PhantomData<HashMap<K, V>>,

View file

@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::collections::HashSet;
use bimap::BiHashMap;
use petgraph::graphmap::DiGraphMap;
use petgraph::visit::Dfs;
use proc_macro2::{TokenStream, TokenTree};
@ -83,8 +82,6 @@ impl Visitor {
pub fn load_model(&mut self, model: &ModelMap) {
for (key, (ty, init)) in model {
let id = Symbol::gensym();
// self.model_bimap.insert(id, key.clone());
let ty = Syn::from_adapter(&*ty);
let init = Syn::from_adapter(&*init);
self.model.insert(key.clone(), (ty, init));

View file

@ -8,17 +8,16 @@ use std::collections::HashMap;
use std::iter::FromIterator;
use std::iter::Peekable;
use quote::ToTokens;
use enterprise_compiler::model::{Component, Elem, ModelMap, Rsx};
use syn_serde::Syn;
use proc_macro2::{
token_stream::IntoIter, Delimiter, Group, Ident, Punct, Spacing, TokenStream, TokenTree,
};
use symbol::Symbol;
use syn::{
parse::{Parse, ParseStream},
Error as SynError, Expr, Lit, Result as SynResult, Token, Type,
Error as SynError, Expr, Result as SynResult, Token, Type,
};
use syn_serde::Syn;
use crate::rsx::{RsxParser, RsxToken};
@ -35,7 +34,7 @@ enum ParseError {
UnexpectedKeyword,
MissingModel,
MissingView,
InvalidRsx(TokenTree),
// InvalidRsx(TokenTree),
UnmatchedOpenTag(TokenTree),
}
@ -305,7 +304,7 @@ pub fn component(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStre
// TODO: allow importing and stuff
let mut output = TokenStream::new();
for component in visitor {
println!("- {:#?}", component);
// println!("- {:#?}", component);
let component = component.expect("holy shiet");
let name = format_ident!("{}", component.name);
let serialized = serde_json::to_string(&component).expect("fucking json");

View file

@ -3,7 +3,7 @@ use std::iter::FromIterator;
use std::iter::Peekable;
use enterprise_compiler::model::{TagLhs, TagRhs};
use proc_macro2::{token_stream::IntoIter, Delimiter, Ident, Spacing, TokenStream, TokenTree};
use proc_macro2::{token_stream::IntoIter, Delimiter, Ident, TokenStream, TokenTree};
use symbol::Symbol;
use syn::{Expr, Lit};
use syn_serde::Syn;
@ -106,12 +106,12 @@ impl RsxParser {
} else {
unimplemented!("these are wrong states")
};
consume_punct(&mut iter, Some('='));
consume_punct(&mut iter, Some('='))?;
let next_token = iter.next();
let rhs = match next_token {
Some(TokenTree::Literal(lit)) => {
let mut stream = TokenStream::from(TokenTree::Literal(lit));
let stream = TokenStream::from(TokenTree::Literal(lit));
let lit = syn::parse2::<Lit>(stream)?;
if let Lit::Str(string) = lit {
TagRhs::Text(string.value())
@ -137,7 +137,7 @@ impl RsxParser {
return Ok(Some(variant(Symbol::from(name.to_string()), attrs)));
}
TokenTree::Literal(lit) => {
let mut stream = TokenStream::from(TokenTree::Literal(lit));
let stream = TokenStream::from(TokenTree::Literal(lit));
let lit = syn::parse2::<Lit>(stream)?;
if let Lit::Str(string) = lit {

View file

@ -0,0 +1,9 @@
[package]
name = "todomvc"
version = "0.1.0"
authors = ["Michael Zhang <iptq@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View file

@ -1,8 +1,5 @@
// cribbed from https://github.com/remexre/symbol-rs
#[macro_use]
extern crate serde_derive;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::fmt::{self, Debug, Display, Formatter, Result as FmtResult};