init.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use std::path::PathBuf;
  2. use failure::Fail;
  3. use crate::project::{Project, ProjectInitError};
  4. #[derive(Debug, Fail)]
  5. pub enum InitError {
  6. #[fail(
  7. display = "Invalid project kind '{}', valid kinds are 'place' and 'model'",
  8. _0
  9. )]
  10. InvalidKind(String),
  11. #[fail(display = "Project init error: {}", _0)]
  12. ProjectInitError(#[fail(cause)] ProjectInitError),
  13. }
  14. impl_from!(InitError {
  15. ProjectInitError => ProjectInitError,
  16. });
  17. #[derive(Debug)]
  18. pub struct InitOptions<'a> {
  19. pub fuzzy_project_path: PathBuf,
  20. pub kind: Option<&'a str>,
  21. }
  22. pub fn init(options: &InitOptions) -> Result<(), InitError> {
  23. let (project_path, project_kind) = match options.kind {
  24. Some("place") | None => {
  25. let path = Project::init_place(&options.fuzzy_project_path)?;
  26. (path, "place")
  27. }
  28. Some("model") => {
  29. let path = Project::init_model(&options.fuzzy_project_path)?;
  30. (path, "model")
  31. }
  32. Some(invalid) => return Err(InitError::InvalidKind(invalid.to_string())),
  33. };
  34. println!(
  35. "Created new {} project file at {}",
  36. project_kind,
  37. project_path.display()
  38. );
  39. Ok(())
  40. }