Skip to content

Commit

Permalink
Merge pull request #6 from ayazhafiz/refactor/mac2
Browse files Browse the repository at this point in the history
refactor(mac): port mac implementation to work with v2 (tests fail)
  • Loading branch information
ArturKovacs committed Nov 11, 2019
2 parents fd597fc + 576fad7 commit adf0ea4
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use std::ffi::OsString;
use std::path::Path;
use std::process::Command;

use crate::Error;

pub fn is_implemented() -> bool {
true
}
use crate::{Error, ErrorKind, TrashItem};

pub fn remove_all<I, T>(paths: I) -> Result<(), Error>
where
Expand All @@ -17,20 +13,35 @@ where
.into_iter()
// Convert paths into canonical, absolute forms and collect errors
.map(|path| {
path.as_ref()
.canonicalize()
.map_err(|e| Error::CanonicalizePath {
code: e.raw_os_error(),
})
path.as_ref().canonicalize().map_err(|e| {
Error::new(
ErrorKind::CanonicalizePath {
original: path.as_ref().into(),
},
Box::new(e),
)
})
})
// Convert paths into &strs and collect errors
.map(|path| path.and_then(|p| p.to_str().ok_or(Error::Unknown).map(|s| s.to_owned())))
.map(|path| {
path.and_then(|p| {
match p.to_str() {
Some(s) => Ok(s.to_owned()),
None => Err(Error::kind_only(ErrorKind::ConvertOsString {
// `PathBuf`s are stored as `OsString`s internally, so failure to convert
// to a slice reduces appropriately.
original: p.into(),
})),
}
})
})
.collect::<Result<Vec<String>, Error>>()?;

// AppleScript command to move files (or directories) to Trash looks like
// osascript -e 'tell application "Finder" to delete { POSIX file "file1", POSIX "file2" }'
// The `-e` flag is used to execute only one line of AppleScript.
let mut command = Command::new("osascript");
const APPLESCRIPT: &str = "osascript";
let mut command = Command::new(APPLESCRIPT);
let posix_files = full_paths
.into_iter()
.map(|p| format!("POSIX file \"{}\"", p))
Expand All @@ -45,14 +56,21 @@ where
command.args(argv);

// Execute command
let result = command.output().map_err(|e| Error::Remove {
code: e.raw_os_error(),
let result = command.output().map_err(|e| {
Error::new(
ErrorKind::PlatformApi {
function_name: APPLESCRIPT,
code: e.raw_os_error(),
},
Box::new(e),
)
})?;

if !result.status.success() {
return Err(Error::Remove {
return Err(Error::kind_only(ErrorKind::PlatformApi {
function_name: APPLESCRIPT,
code: result.status.code(),
});
}));
}

Ok(())
Expand All @@ -61,3 +79,18 @@ where
pub fn remove<T: AsRef<Path>>(path: T) -> Result<(), Error> {
remove_all(&[path])
}

pub fn list() -> Result<Vec<TrashItem>, Error> {
unimplemented!();
}

pub fn purge_all<I>(_items: I) -> Result<(), Error>
where
I: IntoIterator<Item = TrashItem>,
{
unimplemented!();
}

pub fn restore_all<I>(_items: I) -> Result<(), Error> {
unimplemented!();
}

0 comments on commit adf0ea4

Please sign in to comment.