1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
use codeviz_common::ElementFormatter; use std::collections::BTreeSet; use super::*; #[derive(Debug, Clone)] pub struct FileSpec { pub elements: Elements, } impl FileSpec { pub fn new() -> FileSpec { FileSpec { elements: Elements::new() } } pub fn push<E>(&mut self, element: E) where E: Into<Element>, { self.elements.push(element); } fn imports(&self) -> Option<Elements> { let mut imports = BTreeSet::new(); self.elements.imports(&mut imports); let modules: BTreeSet<(String, Option<String>)> = imports .into_iter() .map(|imported| (imported.module, imported.alias)) .collect(); if modules.is_empty() { return None; } let mut elements = Elements::new(); for (module, alias) in modules { let mut s = Statement::new(); s.push("use "); s.push(&module); if let Some(ref alias) = alias { s.push(" as "); s.push(alias); } s.push(";"); elements.push(s); } Some(elements) } pub fn format<W>(&self, out: &mut W) -> Result<()> where W: ::std::fmt::Write, { let mut elements = Elements::new(); if let Some(imports) = self.imports() { elements.push(imports); } elements.push(self.elements.clone().join(Spacing)); let elements: Element = elements.clone().join(Spacing).into(); let mut extra = (); elements.format(&mut ElementFormatter::new(out), &mut extra)?; out.write_char('\n')?; Ok(()) } } impl ImportReceiver for BTreeSet<ImportedName> { fn receive(&mut self, name: &ImportedName) { self.insert(name.clone()); } } impl ToString for FileSpec { fn to_string(&self) -> String { let mut s = String::new(); self.format(&mut s).unwrap(); s } }