fix: conditionally enable nix error conversions

This commit is contained in:
Orhun Parmaksız 2022-10-22 21:34:45 +03:00
parent c19c19e4db
commit 81ea9521ce
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 12 additions and 5 deletions

View file

@ -16,7 +16,9 @@ use nix::fcntl::{open, OFlag};
use nix::sys::stat::Mode; use nix::sys::stat::Mode;
use std::path::Path; use std::path::Path;
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError}; #[cfg(any(target_os = "linux", target_os = "android"))]
use uucore::error::FromIo;
use uucore::error::{UResult, USimpleError};
use uucore::format_usage; use uucore::format_usage;
static ABOUT: &str = "Synchronize cached writes to persistent storage"; static ABOUT: &str = "Synchronize cached writes to persistent storage";

View file

@ -522,6 +522,7 @@ impl From<std::io::Error> for Box<dyn UError> {
/// // prints "fix me please!: Permission denied" /// // prints "fix me please!: Permission denied"
/// println!("{}", uio_result.unwrap_err()); /// println!("{}", uio_result.unwrap_err());
/// ``` /// ```
#[cfg(any(target_os = "linux", target_os = "android"))]
impl<T> FromIo<UResult<T>> for Result<T, nix::Error> { impl<T> FromIo<UResult<T>> for Result<T, nix::Error> {
fn map_err_context(self, context: impl FnOnce() -> String) -> UResult<T> { fn map_err_context(self, context: impl FnOnce() -> String) -> UResult<T> {
self.map_err(|e| { self.map_err(|e| {
@ -533,6 +534,7 @@ impl<T> FromIo<UResult<T>> for Result<T, nix::Error> {
} }
} }
#[cfg(any(target_os = "linux", target_os = "android"))]
impl<T> FromIo<UResult<T>> for nix::Error { impl<T> FromIo<UResult<T>> for nix::Error {
fn map_err_context(self, context: impl FnOnce() -> String) -> UResult<T> { fn map_err_context(self, context: impl FnOnce() -> String) -> UResult<T> {
Err(Box::new(UIoError { Err(Box::new(UIoError {
@ -542,6 +544,7 @@ impl<T> FromIo<UResult<T>> for nix::Error {
} }
} }
#[cfg(any(target_os = "linux", target_os = "android"))]
impl From<nix::Error> for UIoError { impl From<nix::Error> for UIoError {
fn from(f: nix::Error) -> Self { fn from(f: nix::Error) -> Self {
Self { Self {
@ -551,6 +554,7 @@ impl From<nix::Error> for UIoError {
} }
} }
#[cfg(any(target_os = "linux", target_os = "android"))]
impl From<nix::Error> for Box<dyn UError> { impl From<nix::Error> for Box<dyn UError> {
fn from(f: nix::Error) -> Self { fn from(f: nix::Error) -> Self {
let u_error: UIoError = f.into(); let u_error: UIoError = f.into();
@ -746,12 +750,13 @@ impl Display for ClapErrorWrapper {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use nix::errno::Errno;
use std::io::ErrorKind;
#[test] #[test]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn test_nix_error_conversion() { fn test_nix_error_conversion() {
use super::{FromIo, UIoError};
use nix::errno::Errno;
use std::io::ErrorKind;
for (nix_error, expected_error_kind) in [ for (nix_error, expected_error_kind) in [
(Errno::EACCES, ErrorKind::PermissionDenied), (Errno::EACCES, ErrorKind::PermissionDenied),
(Errno::ENOENT, ErrorKind::NotFound), (Errno::ENOENT, ErrorKind::NotFound),