-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split the cache table from the record table (#29)
* Split cached records from configured records * Tested the remote lookup/cache fixed a couple butgs from the TUI changes * Started working on the cached records tui and started using the From trait more * Made it so we can switch between views! * Added a 'from' macro to make From implementations much much much much easier * Finished the database / TUI setup for viewing cached records * Last clean up bits * Last clean up bits FOR REAL * okay... * Kinda actually fixed tcp and fixed all the stuff I broke O.O
- Loading branch information
1 parent
aa0ed2a
commit 7d4bd9b
Showing
18 changed files
with
777 additions
and
408 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "simple-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "simple_macros" | ||
path = "src/lib.rs" | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "*" | ||
syn = { version = "2.0.55", features = ["full", "extra-traits"] } | ||
proc-macro2 = "1.0.78" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
extern crate proc_macro; | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{Attribute, Meta, ReturnType}; | ||
|
||
// TODO this shouldn't require the from type to derive clone | ||
// TODO I want this to take an attr argument to control whether we generate FromIterator or not | ||
#[proc_macro_attribute] | ||
pub fn from(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let ast = syn::parse::<syn::ItemFn>(item).expect("The #[from] macro can only be applied to free-standing functions"); | ||
|
||
if ast.sig.inputs.len() != 1 || ast.sig.output == ReturnType::Default { | ||
panic!("#[from] requires annotated function to have form fn (X) -> Y where X is any type and Y is a non-void type."); | ||
} | ||
|
||
let attributes = ast.attrs.into_iter() | ||
.filter(|attr| match &attr.meta { | ||
Meta::Path(path) if path.is_ident("from") => false, | ||
Meta::Path(path) if path.segments.len() == 1 => match path.segments.last() { | ||
Some(segment) if segment.ident.to_string() == "from" => false, | ||
_ => true | ||
} | ||
Meta::List(list) if list.path.is_ident("from") => false, | ||
Meta::List(list) if list.path.segments.len() == 1 => match list.path.segments.last() { | ||
Some(segment) if segment.ident.to_string() == "from" => false, | ||
_ => true | ||
} | ||
_ => true | ||
}) | ||
.collect::<Vec<Attribute>>(); | ||
|
||
let to_type = match ast.sig.output { | ||
ReturnType::Type(_, return_type) => return_type, | ||
ReturnType::Default => panic!("We need a return type :(") | ||
}; | ||
let (from_arg_name, from_type) = match ast.sig.inputs.get(0) { | ||
Some(syn::FnArg::Typed(base_arg)) => { | ||
(base_arg.pat.clone(), base_arg.ty.clone()) | ||
} | ||
_ => panic!("Bad function argument!! Must be 1 non-receiver argument") | ||
}; | ||
|
||
let function_body = ast.block.stmts; | ||
|
||
let generated = quote! { | ||
#(#attributes)* | ||
impl From<#from_type> for #to_type { | ||
fn from(#from_arg_name: #from_type) -> Self { | ||
#(#function_body)* | ||
} | ||
} | ||
|
||
#(#attributes)* | ||
impl From<&#from_type> for #to_type { | ||
fn from(#from_arg_name: &#from_type) -> Self { | ||
#from_arg_name.clone().into() | ||
} | ||
} | ||
|
||
#(#attributes)* | ||
impl FromIterator<#from_type> for Vec<#to_type> { | ||
fn from_iter<T: IntoIterator<Item = #from_type>>(iter: T) -> Self { | ||
iter.into_iter().collect() | ||
} | ||
} | ||
|
||
#(#attributes)* | ||
impl<'from_iterator_lifetime> FromIterator<&'from_iterator_lifetime #from_type> for Vec<#to_type> { | ||
fn from_iter<T: IntoIterator<Item = &'from_iterator_lifetime #from_type>>(iter: T) -> Self { | ||
iter.into_iter().map(|x| <#from_type as Into<#to_type>>::into(x.clone())).collect() | ||
} | ||
} | ||
}; | ||
generated.into() | ||
} |
Oops, something went wrong.