From 3422ae6423dd3acd7f102f07ea1b9fc0f86188d2 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 19 Feb 2020 17:46:04 -0600 Subject: [PATCH] add todomvc crate and fix clippy stuff --- Cargo.lock | 4 +++ Cargo.toml | 1 + enterprise-compiler/src/lib.rs | 41 ++-------------------------- enterprise-compiler/src/model.rs | 12 ++++---- enterprise-compiler/src/tuple_map.rs | 9 ++++-- enterprise-compiler/src/visitor.rs | 3 -- enterprise-macros/src/lib.rs | 9 +++--- enterprise-macros/src/rsx.rs | 8 +++--- examples/todomvc/Cargo.toml | 9 ++++++ examples/todomvc/src/main.rs | 3 ++ symbol/src/lib.rs | 3 -- 11 files changed, 41 insertions(+), 61 deletions(-) create mode 100644 examples/todomvc/Cargo.toml create mode 100644 examples/todomvc/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 8d0d08f..bf34af5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index d1645d5..88e6fb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "syn-serde", "examples/helloworld", + "examples/todomvc", ] [features] diff --git a/enterprise-compiler/src/lib.rs b/enterprise-compiler/src/lib.rs index 363a058..b8403b7 100644 --- a/enterprise-compiler/src/lib.rs +++ b/enterprise-compiler/src/lib.rs @@ -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, @@ -94,37 +91,5 @@ pub fn process(mod_name: impl AsRef, code: impl AsRef) { 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 = hashmap! { -// "name".into() => "String".into(), -// }; - -// let helloworld_datainit: HashMap = 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::("name").unwrap())), -// Rsx::Text("!".into()), -// ]; - -// process( -// "HelloWorld", -// &helloworld_datamodel, -// &helloworld_datainit, -// &helloworld_dom, -// ) -// .into() -// } diff --git a/enterprise-compiler/src/model.rs b/enterprise-compiler/src/model.rs index 8cbb6e5..b0ce36f 100644 --- a/enterprise-compiler/src/model.rs +++ b/enterprise-compiler/src/model.rs @@ -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; -pub fn convert_map(map: HashMap) -> ModelMap { +pub fn convert_map( + map: HashMap, +) -> HashMap { 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()), } diff --git a/enterprise-compiler/src/tuple_map.rs b/enterprise-compiler/src/tuple_map.rs index f401831..8099090 100644 --- a/enterprise-compiler/src/tuple_map.rs +++ b/enterprise-compiler/src/tuple_map.rs @@ -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 { marker: PhantomData>, diff --git a/enterprise-compiler/src/visitor.rs b/enterprise-compiler/src/visitor.rs index eb9887c..f19bde7 100644 --- a/enterprise-compiler/src/visitor.rs +++ b/enterprise-compiler/src/visitor.rs @@ -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)); diff --git a/enterprise-macros/src/lib.rs b/enterprise-macros/src/lib.rs index b0b2dfd..942771f 100644 --- a/enterprise-macros/src/lib.rs +++ b/enterprise-macros/src/lib.rs @@ -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"); diff --git a/enterprise-macros/src/rsx.rs b/enterprise-macros/src/rsx.rs index e3932f6..012ad51 100644 --- a/enterprise-macros/src/rsx.rs +++ b/enterprise-macros/src/rsx.rs @@ -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::(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::(stream)?; if let Lit::Str(string) = lit { diff --git a/examples/todomvc/Cargo.toml b/examples/todomvc/Cargo.toml new file mode 100644 index 0000000..b9533e8 --- /dev/null +++ b/examples/todomvc/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "todomvc" +version = "0.1.0" +authors = ["Michael Zhang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/examples/todomvc/src/main.rs b/examples/todomvc/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/examples/todomvc/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/symbol/src/lib.rs b/symbol/src/lib.rs index 925c5ba..5d3c7e6 100644 --- a/symbol/src/lib.rs +++ b/symbol/src/lib.rs @@ -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};