You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2KB

  1. use std::path::Path;
  2. use std::time::*;
  3. use errors::{Result, Error};
  4. use site::Site;
  5. use crate::console;
  6. //use crate::console;
  7. pub fn index(
  8. root_dir: &Path,
  9. config_file: &str,
  10. base_url: Option<&str>,
  11. output_dir: &str,
  12. include_drafts: bool,
  13. #[cfg(feature = "tantivy-indexing")]
  14. index_type: &str,
  15. ) -> Result<()> {
  16. let mut site = Site::new(root_dir, config_file)?;
  17. site.set_output_path(output_dir);
  18. // TODO: is base_url even necessary for this command?
  19. if let Some(b) = base_url {
  20. site.set_base_url(b.to_string());
  21. }
  22. if include_drafts {
  23. site.include_drafts();
  24. }
  25. site.load()?;
  26. console::notify_site_size(&site);
  27. console::warn_about_ignored_pages(&site);
  28. let do_elastic_index = || -> Result<()> {
  29. let indexing_start = Instant::now();
  30. let n_indexed = site.build_search_index()
  31. .map_err(|e| Error::from(format!("creating elasticlunr index failed: {}", e)))?;
  32. let indexing_took = Instant::now() - indexing_start;
  33. console::report_n_pages_indexed(n_indexed, indexing_took);
  34. Ok(())
  35. };
  36. #[cfg(feature = "tantivy-indexing")]
  37. {
  38. match index_type {
  39. "elasticlunr" => do_elastic_index()?,
  40. "tantivy" => {
  41. let index_dir = Path::new(output_dir).join("tantivy-index");
  42. if index_dir.exists() {
  43. std::fs::remove_dir_all(&index_dir)?;
  44. }
  45. utils::fs::ensure_directory_exists(&index_dir)?;
  46. let lang = &site.config.default_language;
  47. let library = site.library.read().unwrap(); // unwrap originally in Site::build_search_index, just parroting here, no idea if safe
  48. let indexing_start = Instant::now();
  49. let n_pages_indexed = search::build_tantivy_index(lang, &library, &index_dir)?;
  50. let indexing_took = Instant::now() - indexing_start;
  51. console::report_n_pages_indexed(n_pages_indexed, indexing_took);
  52. }
  53. _ => unreachable!()
  54. }
  55. }
  56. #[cfg(not(feature = "tantivy-indexing"))]
  57. {
  58. do_elastic_index()?;
  59. }
  60. Ok(())
  61. }