change-user-run 0.1.2

Run commands as other users and create users
Documentation
//! Error handling.

use std::{path::PathBuf, process::ExitStatus, str::Utf8Error};

/// An error that may occur when creating users or running a command as  different user.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Unable to attach to stdin of a command.
    #[error("Unable to attach to stdin of command \"{command}\"")]
    CommandAttachToStdin {
        /// The command for which attaching to stdin failed.
        command: String,
    },

    /// A command exited unsuccessfully.
    #[error("The command \"{command}\" could not be started in the background:\n{source}")]
    CommandBackground {
        /// The command that could not be started in the background.
        command: String,
        /// The source error.
        source: std::io::Error,
    },

    /// A command could not be executed.
    #[error("The command \"{command}\" could not be executed:\n{source}")]
    CommandExec {
        /// The command that could not be executed.
        command: String,
        /// The source error.
        source: std::io::Error,
    },

    /// A command exited unsuccessfully.
    #[error(
        "The command \"{command}\" exited with non-zero status code \"{exit_status}\":\nstderr:\n{stderr}"
    )]
    CommandNonZero {
        /// The command that exited with a non-zero exit code.
        command: String,
        /// The exit status of `command`.
        exit_status: ExitStatus,
        /// The stderr of `command`.
        stderr: String,
    },

    /// Unable to write to stdin of a command.
    #[error("Unable to write to stdin of command \"{command}\"")]
    CommandWriteToStdin {
        /// The command for which writing to stdin failed.
        command: String,
        /// The source error.
        source: std::io::Error,
    },

    /// An executable that is supposed to be called, is not found.
    #[error("Unable to to find executable \"{command}\"")]
    ExecutableNotFound {
        /// The executable that could not be found.
        command: String,
        /// The source error.
        source: which::Error,
    },

    /// An I/O error.
    #[error("I/O error while {context}:\n{source}")]
    Io {
        /// The short description of the operation.
        context: &'static str,

        /// The source error.
        source: std::io::Error,
    },

    /// An I/O error with a specific path.
    #[error("I/O error at {path} while {context}:\n{source}")]
    IoPath {
        /// The file that was being accessed.
        path: PathBuf,

        /// The short description of the operation.
        context: &'static str,

        /// The source error.
        source: std::io::Error,
    },

    /// A password hash error occurred.
    #[error("Password hash error while {context}: {source}")]
    PasswordHash {
        /// The context in which the error occurred.
        ///
        /// This is meant to complete the sentence "Password hash error while ".
        context: String,

        /// The source error.
        source: yescrypt::password_hash::Error,
    },

    /// A UTF-8 error occurred when trying to convert a byte vector to a string.
    #[error("Converting a byte vector to a string failed while {context}:\n{source}")]
    Utf8String {
        /// The context in which the error occurred.
        ///
        /// Should complete the sentence "Converting a byte vector to a string failed while ".
        context: String,
        /// The source error.
        source: Utf8Error,
    },
}