|
| 1 | +use std::collections::BTreeSet; |
| 2 | + |
| 3 | +pub use ::include_dir; |
| 4 | +use common::{ |
| 5 | + input::{IngotKind, Version}, |
| 6 | + InputDb, InputFile, InputIngot, |
| 7 | +}; |
| 8 | +use include_dir::{include_dir, Dir}; |
| 9 | + |
| 10 | +pub const STD: Dir = include_dir!("$CARGO_MANIFEST_DIR/std"); |
| 11 | + |
| 12 | +fn std_src_input_files(db: &mut dyn InputDb, ingot: InputIngot) -> BTreeSet<InputFile> { |
| 13 | + static_dir_files(&STD) |
| 14 | + .into_iter() |
| 15 | + .map(|(path, content)| InputFile::new(db, ingot, path.into(), content.into())) |
| 16 | + .collect() |
| 17 | +} |
| 18 | + |
| 19 | +pub fn std_lib_input_ingot(db: &mut dyn InputDb) -> InputIngot { |
| 20 | + let ingot = InputIngot::new( |
| 21 | + db, |
| 22 | + "std", |
| 23 | + IngotKind::Std, |
| 24 | + Version::new(0, 0, 0), |
| 25 | + BTreeSet::default(), |
| 26 | + ); |
| 27 | + |
| 28 | + let input_files = std_src_input_files(db, ingot); |
| 29 | + let root_file = input_files |
| 30 | + .iter() |
| 31 | + .find(|file| file.path(db).ends_with("lib.fe")) |
| 32 | + .unwrap() |
| 33 | + .to_owned(); |
| 34 | + |
| 35 | + ingot.set_root_file(db, root_file); |
| 36 | + |
| 37 | + ingot.set_files(db, input_files); |
| 38 | + ingot |
| 39 | +} |
| 40 | + |
| 41 | +// pub fn std_src_files() -> Vec<(&'static str, &'static str)> { |
| 42 | +// static_dir_files(STD.get_dir("src").unwrap()) |
| 43 | +// } |
| 44 | + |
| 45 | +pub fn static_dir_files(dir: &'static Dir) -> Vec<(&'static str, &'static str)> { |
| 46 | + fn add_files(dir: &'static Dir, accum: &mut Vec<(&'static str, &'static str)>) { |
| 47 | + accum.extend(dir.files().map(|file| { |
| 48 | + ( |
| 49 | + file.path().to_str().unwrap(), |
| 50 | + file.contents_utf8().expect("non-utf8 static file"), |
| 51 | + ) |
| 52 | + })); |
| 53 | + |
| 54 | + for sub_dir in dir.dirs() { |
| 55 | + add_files(sub_dir, accum) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + let mut files = vec![]; |
| 60 | + add_files(dir, &mut files); |
| 61 | + files |
| 62 | +} |
0 commit comments