|
| 1 | +use super::*; |
| 2 | +use std::iter::{IntoIterator, once}; |
| 3 | +use std::vec::IntoIter; |
| 4 | + |
| 5 | +pub fn expr<'a>( |
| 6 | + ast: Vec<Expr<'a>>, |
| 7 | +) -> impl Iterator<Item = &'a str> { |
| 8 | + ast.into_iter().flat_map(expr_one) |
| 9 | +} |
| 10 | + |
| 11 | +fn expr_one<'a>(ast: Expr<'a>) -> IntoIter<&'a str> { |
| 12 | + match ast { |
| 13 | + Expr::Use(x) => expr_use(true, x) |
| 14 | + .chain(vec![";"].into_iter()) |
| 15 | + .collect::<Vec<&'a str>>() |
| 16 | + .into_iter(), |
| 17 | + Expr::Other(x) => vec![x].into_iter(), |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +fn expr_use<'a>( |
| 22 | + top: bool, |
| 23 | + ast: ExprUse<'a>, |
| 24 | +) -> IntoIter<&'a str> { |
| 25 | + let x0 = if top { vec!["use"] } else { vec![] }; |
| 26 | + let x1 = match ast { |
| 27 | + ExprUse::Item { |
| 28 | + module, |
| 29 | + rename, |
| 30 | + nested, |
| 31 | + } => expr_use_item(module, rename, nested), |
| 32 | + ExprUse::Many(xs) => expr_use_many(xs), |
| 33 | + ExprUse::Glob => vec!["*"].into_iter(), |
| 34 | + }; |
| 35 | + x0.into_iter() |
| 36 | + .chain(x1.into_iter()) |
| 37 | + .collect::<Vec<_>>() |
| 38 | + .into_iter() |
| 39 | +} |
| 40 | + |
| 41 | +fn expr_use_item<'a>( |
| 42 | + module: &'a str, |
| 43 | + rename: Option<&'a str>, |
| 44 | + nested: Option<Box<ExprUse<'a>>>, |
| 45 | +) -> IntoIter<&'a str> { |
| 46 | + let module_iter = once(module); |
| 47 | + |
| 48 | + let rename_iter = rename |
| 49 | + .map(|x| once("as").chain(once(x))) |
| 50 | + .into_iter() |
| 51 | + .flatten(); |
| 52 | + |
| 53 | + let nested_iter = nested |
| 54 | + .map(|x| once("::").chain(expr_use(false, *x))) |
| 55 | + .into_iter() |
| 56 | + .flatten(); |
| 57 | + |
| 58 | + module_iter |
| 59 | + .chain(rename_iter) |
| 60 | + .chain(nested_iter) |
| 61 | + .collect::<Vec<_>>() |
| 62 | + .into_iter() |
| 63 | +} |
| 64 | + |
| 65 | +fn expr_use_many<'a>( |
| 66 | + xs: Vec<ExprUse<'a>>, |
| 67 | +) -> IntoIter<&'a str> { |
| 68 | + xs.into_iter() |
| 69 | + .flat_map(|x| expr_use(false, x)) |
| 70 | + .collect::<Vec<_>>() |
| 71 | + .into_iter() |
| 72 | +} |
0 commit comments