kea-lifecycle 0.1.0

buildpack lifecycle
Documentation
use std::path::{Path, PathBuf};

use super::error::{BuildpackError, Result};

pub struct ExecutablePath(pub PathBuf);

impl ExecutablePath {
    pub fn from_execuing_path() -> Result<Self> {
        let executable_path = std::env::current_exe().map_err(|_| {
            BuildpackError::InvalidExecutable("can't get current executable path".into())
        })?;
        Self::from_path(executable_path)
    }

    pub fn from_path(executable_path: impl AsRef<Path>) -> Result<Self> {
        Ok(Self(executable_path.as_ref().to_owned()))
    }

    pub fn executable_name(&self) -> Result<String> {
        let executable_filename = self
            .0
            .file_name()
            .ok_or_else(|| {
                BuildpackError::InvalidExecutable(format!(
                    "cant get file name from '{}'",
                    self.0.to_string_lossy()
                ))
            })?
            .to_string_lossy();

        let executable = executable_filename
            .strip_suffix(std::env::consts::EXE_SUFFIX)
            .unwrap_or(executable_filename.as_ref());

        Ok(executable.into())
    }
}