use crate::common::*;
pub(crate) struct Platform;
#[cfg(unix)]
impl PlatformInterface for Platform {
  fn make_shebang_command(
    path: &Path,
    working_directory: &Path,
    _shebang: Shebang,
  ) -> Result<Command, OutputError> {
        let mut cmd = Command::new(path);
    cmd.current_dir(working_directory);
    Ok(cmd)
  }
  fn set_execute_permission(path: &Path) -> Result<(), io::Error> {
    use std::os::unix::fs::PermissionsExt;
        let mut permissions = fs::metadata(&path)?.permissions();
        let current_mode = permissions.mode();
    permissions.set_mode(current_mode | 0o100);
        fs::set_permissions(&path, permissions)
  }
  fn signal_from_exit_status(exit_status: process::ExitStatus) -> Option<i32> {
    use std::os::unix::process::ExitStatusExt;
    exit_status.signal()
  }
  fn convert_native_path(_working_directory: &Path, path: &Path) -> Result<String, String> {
    path
      .to_str()
      .map(str::to_string)
      .ok_or_else(|| String::from("Error getting current directory: unicode decode error"))
  }
}
#[cfg(windows)]
impl PlatformInterface for Platform {
  fn make_shebang_command(
    path: &Path,
    working_directory: &Path,
    shebang: Shebang,
  ) -> Result<Command, OutputError> {
    use std::borrow::Cow;
        let command = if shebang.interpreter.contains('/') {
            let mut cygpath = Command::new("cygpath");
      cygpath.current_dir(working_directory);
      cygpath.arg("--windows");
      cygpath.arg(shebang.interpreter);
      Cow::Owned(output(cygpath)?)
    } else {
            Cow::Borrowed(shebang.interpreter)
    };
    let mut cmd = Command::new(command.as_ref());
    cmd.current_dir(working_directory);
    if let Some(argument) = shebang.argument {
      cmd.arg(argument);
    }
    cmd.arg(path);
    Ok(cmd)
  }
  fn set_execute_permission(_path: &Path) -> Result<(), io::Error> {
            Ok(())
  }
  fn signal_from_exit_status(_exit_status: process::ExitStatus) -> Option<i32> {
            None
  }
  fn convert_native_path(working_directory: &Path, path: &Path) -> Result<String, String> {
        let mut cygpath = Command::new("cygpath");
    cygpath.current_dir(working_directory);
    cygpath.arg("--unix");
    cygpath.arg(path);
    match output(cygpath) {
      Ok(shell_path) => Ok(shell_path),
      Err(_) => path
        .to_str()
        .map(str::to_string)
        .ok_or_else(|| String::from("Error getting current directory: unicode decode error")),
    }
  }
}