| 1 |
/// Define `Resolver` trait and implement it on some hashmaps and also define the `Resolver` tuple |
| 2 |
/// composition. Provide also some utility functions related to how to create a `Resolver` and |
| 3 |
/// resolving render. |
| 4 |
/// |
| 5 |
use std::borrow::Cow; |
| 6 |
use std::collections::HashMap; |
| 7 |
|
| 8 |
use syn::{parse_quote, Expr, Pat}; |
| 9 |
|
| 10 |
use crate::parse::Fixture; |
| 11 |
|
| 12 |
pub(crate) mod fixtures { |
| 13 |
use quote::format_ident; |
| 14 |
|
| 15 |
use crate::parse::arguments::ArgumentsInfo; |
| 16 |
|
| 17 |
use super::*; |
| 18 |
|
| 19 |
pub(crate) fn get<'a>( |
| 20 |
arguments: &ArgumentsInfo, |
| 21 |
fixtures: impl Iterator<Item = &'a Fixture>, |
| 22 |
) -> impl Resolver + 'a { |
| 23 |
fixtures |
| 24 |
.map(|f| { |
| 25 |
( |
| 26 |
arguments.inner_pat(&f.arg).clone(), |
| 27 |
extract_resolve_expression(f), |
| 28 |
) |
| 29 |
}) |
| 30 |
.collect::<HashMap<_, Expr>>() |
| 31 |
} |
| 32 |
|
| 33 |
fn extract_resolve_expression(fixture: &Fixture) -> syn::Expr { |
| 34 |
let resolve = fixture.resolve.clone(); |
| 35 |
let positional = &fixture.positional.0; |
| 36 |
let f_name = match positional.len() { |
| 37 |
0 => format_ident!("default"), |
| 38 |
l => format_ident!("partial_{}", l), |
| 39 |
}; |
| 40 |
parse_quote! { #resolve::#f_name(#(#positional), *) } |
| 41 |
} |
| 42 |
|
| 43 |
#[cfg(test)] |
| 44 |
mod should { |
| 45 |
use super::*; |
| 46 |
use crate::test::{assert_eq, *}; |
| 47 |
|
| 48 |
#[rstest] |
| 49 |
#[case(&[], "default()")] |
| 50 |
#[case(&["my_expression"], "partial_1(my_expression)")] |
| 51 |
#[case(&["first", "other"], "partial_2(first, other)")] |
| 52 |
fn resolve_by_use_the_given_name( |
| 53 |
#[case] args: &[&str], |
| 54 |
#[case] expected: &str, |
| 55 |
#[values(None, Some("minnie"), Some("__destruct_1"))] inner_pat: Option<&str>, |
| 56 |
) { |
| 57 |
let data = vec![fixture("pippo", args)]; |
| 58 |
let mut arguments: ArgumentsInfo = Default::default(); |
| 59 |
let mut request = pat("pippo"); |
| 60 |
if let Some(inner) = inner_pat { |
| 61 |
arguments.set_inner_pat(pat("pippo"), pat(inner)); |
| 62 |
request = pat(inner); |
| 63 |
} |
| 64 |
|
| 65 |
let resolver = get(&arguments, data.iter()); |
| 66 |
|
| 67 |
let resolved = resolver.resolve(&request).unwrap().into_owned(); |
| 68 |
|
| 69 |
assert_eq!(resolved, format!("pippo::{}", expected).ast()); |
| 70 |
} |
| 71 |
|
| 72 |
#[rstest] |
| 73 |
#[case(&[], "default()")] |
| 74 |
#[case(&["my_expression"], "partial_1(my_expression)")] |
| 75 |
#[case(&["first", "other"], "partial_2(first, other)")] |
| 76 |
fn resolve_by_use_the_resolve_field( |
| 77 |
#[case] args: &[&str], |
| 78 |
#[case] expected: &str, |
| 79 |
#[values("pluto", "minnie::pluto")] resolver_path: &str, |
| 80 |
#[values(None, Some("minnie"), Some("__destruct_1"))] inner_pat: Option<&str>, |
| 81 |
) { |
| 82 |
let data = vec![fixture("pippo", args).with_resolve(resolver_path)]; |
| 83 |
let mut arguments: ArgumentsInfo = Default::default(); |
| 84 |
let mut request = pat("pippo"); |
| 85 |
if let Some(inner) = inner_pat { |
| 86 |
arguments.set_inner_pat(pat("pippo"), pat(inner)); |
| 87 |
request = pat(inner); |
| 88 |
} |
| 89 |
let resolver = get(&arguments, data.iter()); |
| 90 |
|
| 91 |
let resolved = resolver.resolve(&request).unwrap().into_owned(); |
| 92 |
|
| 93 |
assert_eq!(resolved, format!("{}::{}", resolver_path, expected).ast()); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
pub(crate) mod values { |
| 99 |
use super::*; |
| 100 |
use crate::parse::fixture::ArgumentValue; |
| 101 |
|
| 102 |
pub(crate) fn get<'a>(values: impl Iterator<Item = &'a ArgumentValue>) -> impl Resolver + 'a { |
| 103 |
values |
| 104 |
.map(|av| (av.arg.clone(), &av.expr)) |
| 105 |
.collect::<HashMap<_, &'a Expr>>() |
| 106 |
} |
| 107 |
|
| 108 |
#[cfg(test)] |
| 109 |
mod should { |
| 110 |
use super::*; |
| 111 |
use crate::test::{assert_eq, *}; |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn resolve_by_use_the_given_name() { |
| 115 |
let data = vec![ |
| 116 |
arg_value("pippo", "42"), |
| 117 |
arg_value("donaldduck", "vec![1,2]"), |
| 118 |
]; |
| 119 |
let resolver = get(data.iter()); |
| 120 |
|
| 121 |
assert_eq!( |
| 122 |
resolver.resolve(&pat("pippo")).unwrap().into_owned(), |
| 123 |
"42".ast() |
| 124 |
); |
| 125 |
assert_eq!( |
| 126 |
resolver.resolve(&pat("donaldduck")).unwrap().into_owned(), |
| 127 |
"vec![1,2]".ast() |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/// A trait that `resolve` the given ident to expression code to assign the value. |
| 134 |
pub(crate) trait Resolver { |
| 135 |
fn resolve(&self, arg: &Pat) -> Option<Cow<'_, Expr>>; |
| 136 |
} |
| 137 |
|
| 138 |
impl Resolver for HashMap<Pat, &'_ Expr> { |
| 139 |
fn resolve(&self, arg: &Pat) -> Option<Cow<'_, Expr>> { |
| 140 |
self.get(arg) |
| 141 |
.or_else(|| self.get(&pat_invert_mutability(arg))) |
| 142 |
.map(|&c| Cow::Borrowed(c)) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl Resolver for HashMap<Pat, Expr> { |
| 147 |
fn resolve(&self, arg: &Pat) -> Option<Cow<'_, Expr>> { |
| 148 |
self.get(arg).map(Cow::Borrowed) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
impl<R1: Resolver, R2: Resolver> Resolver for (R1, R2) { |
| 153 |
fn resolve(&self, arg: &Pat) -> Option<Cow<'_, Expr>> { |
| 154 |
self.0.resolve(arg).or_else(|| self.1.resolve(arg)) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
impl<R: Resolver + ?Sized> Resolver for &R { |
| 159 |
fn resolve(&self, arg: &Pat) -> Option<Cow<'_, Expr>> { |
| 160 |
(*self).resolve(arg) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl<R: Resolver + ?Sized> Resolver for Box<R> { |
| 165 |
fn resolve(&self, arg: &Pat) -> Option<Cow<'_, Expr>> { |
| 166 |
(**self).resolve(arg) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl Resolver for (Pat, Expr) { |
| 171 |
fn resolve(&self, arg: &Pat) -> Option<Cow<'_, Expr>> { |
| 172 |
if arg == &self.0 { |
| 173 |
Some(Cow::Borrowed(&self.1)) |
| 174 |
} else { |
| 175 |
None |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
pub(crate) fn pat_invert_mutability(p: &Pat) -> Pat { |
| 181 |
match p.clone() { |
| 182 |
Pat::Ident(mut ident) => { |
| 183 |
ident.mutability = match ident.mutability { |
| 184 |
Some(_) => None, |
| 185 |
None => Some(syn::parse_quote! { mut }), |
| 186 |
}; |
| 187 |
syn::Pat::Ident(ident) |
| 188 |
} |
| 189 |
p => p, |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
#[cfg(test)] |
| 194 |
mod should { |
| 195 |
use super::*; |
| 196 |
use crate::test::{assert_eq, *}; |
| 197 |
use syn::parse_str; |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn return_the_given_expression() { |
| 201 |
let ast = parse_str("fn function(mut foo: String) {}").unwrap(); |
| 202 |
let arg = first_arg_pat(&ast); |
| 203 |
let expected = expr("bar()"); |
| 204 |
let mut resolver = HashMap::new(); |
| 205 |
|
| 206 |
resolver.insert(pat("foo").with_mut(), &expected); |
| 207 |
|
| 208 |
assert_eq!(expected, (&resolver).resolve(&arg).unwrap().into_owned()) |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn return_the_given_expression_also_if_not_mut_searched() { |
| 213 |
let ast = parse_str("fn function(foo: String) {}").unwrap(); |
| 214 |
let arg = first_arg_pat(&ast); |
| 215 |
let expected = expr("bar()"); |
| 216 |
let mut resolver = HashMap::new(); |
| 217 |
|
| 218 |
resolver.insert(pat("foo").with_mut(), &expected); |
| 219 |
|
| 220 |
assert_eq!(expected, (&resolver).resolve(&arg).unwrap().into_owned()) |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn return_none_for_unknown_argument() { |
| 225 |
let ast = "fn function(mut fix: String) {}".ast(); |
| 226 |
let arg = first_arg_pat(&ast); |
| 227 |
|
| 228 |
assert!(EmptyResolver.resolve(&arg).is_none()) |
| 229 |
} |
| 230 |
} |