Repository: vitiral/path_abs (language: rust)

Issue:
File* Constructor cleanup
Before the next version I want to cleanup the constructors.

- `open_*` instead of `new` or just `read`. I.e. `open_read`, `open_append`, etc. I think I'm going to keep these methods on `PathFile`, but rename accordingly.
- `open_abs` instead of `open_path`.



Discussion / hints:

Fix patch:
diff --git a/src/edit.rs b/src/edit.rs
index 7740c53..853e328 100644
--- a/src/edit.rs
+++ b/src/edit.rs
@@ -13,8 +13,7 @@ use std::io;
 use std_prelude::*;
 
 use super::open::FileOpen;
-use super::PathFile;
-use super::{Error, PathInfo, Result};
+use super::{Error, PathFile, PathAbs, PathInfo, Result};
 
 /// A read/write file handle with `path()` attached and improved error messages. Contains methods
 /// and trait implements for both readable _and_ writeable files.
@@ -53,10 +52,10 @@ impl FileEdit {
     }
 
     /// Shortcut to open the file if the path is already absolute.
-    pub(crate) fn open_path(path: PathFile, mut options: fs::OpenOptions) -> Result<FileEdit> {
+    pub(crate) fn open_abs<P: Into<PathAbs>>(path: P, mut options: fs::OpenOptions) -> Result<FileEdit> {
         options.write(true);
         options.read(true);
-        Ok(FileEdit(FileOpen::open_path(path, options)?))
+        Ok(FileEdit(FileOpen::open_abs(path, options)?))
     }
 
     /// Open the file in editing mode, truncating it first if it exists and creating it
diff --git a/src/file.rs b/src/file.rs
index 7197c7a..761752b 100644
--- a/src/file.rs
+++ b/src/file.rs
@@ -152,7 +152,7 @@ impl PathFile {
     /// # Ok(()) } fn main() { try_main().unwrap() }
     /// ```
     pub fn read_string(&self) -> Result<String> {
-        let mut f = self.read()?;
+        let mut f = self.open_read()?;
         f.read_string()
     }
 
@@ -179,7 +179,7 @@ impl PathFile {
         let mut options = fs::OpenOptions::new();
         options.create(true);
         options.truncate(true);
-        let mut f = FileWrite::open_path(self.clone(), options)?;
+        let mut f = FileWrite::open_abs(self.clone(), options)?;
         if s.is_empty() {
             return Ok(());
         }
@@ -208,7 +208,7 @@ impl PathFile {
     /// # Ok(()) } fn main() { try_main().unwrap() }
     /// ```
     pub fn append_str(&self, s: &str) -> Result<()> {
-        let mut f = self.append()?;
+        let mut f = self.open_append()?;
         if s.is_empty() {
             return Ok(());
         }
@@ -234,14 +234,14 @@ impl PathFile {
     /// let expected = "foo\nbar";
     /// file.write_str(expected)?;
     ///
-    /// let mut read = file.read()?;
+    /// let mut read = file.open_read()?;
     /// let mut s = String::new();
     /// read.read_to_string(&mut s)?;
     /// assert_eq!(expected, s);
     /// # Ok(()) } fn main() { try_main().unwrap() }
     /// ```
-    pub fn read(&self) -> Result<FileRead> {
-        FileRead::read_path(self.clone())
+    pub fn open_read(&self) -> Result<FileRead> {
+        FileRead::open_abs(self.clone())
     }
 
     /// Open the file as write-only in append mode.
@@ -262,16 +262,16 @@ impl PathFile {
     /// let expected = "foo\nbar\n";
     /// file.write_str("foo\n")?;
     ///
-    /// let mut append = file.append()?;
+    /// let mut append = file.open_append()?;
     /// append.write_all(b"bar\n")?;
     /// append.flush();
     /// assert_eq!(expected, file.read_string()?);
     /// # Ok(()) } fn main() { try_main().unwrap() }
     /// ```
-    pub fn append(&self) -> Result<FileWrite> {
+    pub fn open_append(&self) -> Result<FileWrite> {
         let mut options = fs::OpenOptions::new();
         options.append(true);
-        FileWrite::open_path(self.clone(), options)
+        FileWrite::open_abs(self.clone(), options)
     }
 
     /// Open the file for editing (reading and writing).
@@ -291,7 +291,7 @@ impl PathFile {
     ///
     /// let expected = "foo\nbar";
     ///
-    /// let mut edit = file.edit()?;
+    /// let mut edit = file.open_edit()?;
     /// let mut s = String::new();
     ///
     /// edit.write_all(expected.as_bytes())?;
@@ -300,8 +300,8 @@ impl PathFile {
     /// assert_eq!(expected, s);
     /// # Ok(()) } fn main() { try_main().unwrap() }
     /// ```
-    pub fn edit(&self) -> Result<FileEdit> {
-        FileEdit::open_path(self.clone(), fs::OpenOptions::new())
+    pub fn open_edit(&self) -> Result<FileEdit> {
+        FileEdit::open_abs(self.clone(), fs::OpenOptions::new())
     }
 
     /// Copy the file to another location, including permission bits
diff --git a/src/lib.rs b/src/lib.rs
index c13d620..b3c4198 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -188,13 +188,13 @@
 //! // Opening a File
 //!
 //! // open read-only using the PathFile method
-//! let read = file.read()?;
+//! let read = file.open_read()?;
 //!
 //! // Or use the type directly: open for appending
-//! let write = FileWrite::append(&file)?;
+//! let write = FileWrite::open_append(&file)?;
 //!
 //! // Open for read/write editing.
-//! let edit = file.edit()?;
+//! let edit = file.open_edit()?;
 //! # Ok(()) } fn main() { try_main().unwrap() }
 //! ```
 
@@ -978,7 +978,7 @@ mod tests {
                     escape(&foo)
                 )
             };
-            assert_match!(pat, foo.edit().unwrap_err())
+            assert_match!(pat, foo.open_edit().unwrap_err())
         }
     }
 
@@ -1157,11 +1157,6 @@ mod tests {
         }
     }
 
-    #[cfg(not(windows))]
-    mod unix {
-        // FIXME: is anything really unix specific?
-    }
-
     mod any {
         use super::*;
 
diff --git a/src/open.rs b/src/open.rs
index f17155b..7b46c01 100644
--- a/src/open.rs
+++ b/src/open.rs
@@ -11,8 +11,7 @@ use std::fmt;
 use std::fs;
 use std_prelude::*;
 
-use super::PathFile;
-use super::{Error, Result};
+use super::{Error, Result, PathAbs, PathFile};
 
 /// **INTERNAL TYPE: do not use directly.**
 ///
@@ -40,13 +39,14 @@ impl FileOpen {
     ///
     /// Typically you should use `PathFile::open` instead (i.e. `file.open(options)` or
     /// `file.read()`).
-    pub fn open_path(path: PathFile, options: fs::OpenOptions) -> Result<FileOpen> {
+    pub fn open_abs<P: Into<PathAbs>>(path: P, options: fs::OpenOptions) -> Result<FileOpen> {
+        let path = path.into();
         let file = options
             .open(&path)
             .map_err(|err| Error::new(err, "opening", path.clone().into()))?;
 
         Ok(FileOpen {
-            path: path,
+            path: PathFile::new_unchecked(path),
             file: file,
         })
     }
diff --git a/src/read.rs b/src/read.rs
index 40f56b2..de9a54e 100644
--- a/src/read.rs
+++ b/src/read.rs
@@ -13,8 +13,7 @@ use std::io;
 use std_prelude::*;
 
 use super::open::FileOpen;
-use super::PathFile;
-use super::{Error, PathInfo, Result};
+use super::{Error, PathInfo, PathFile, PathAbs, Result};
 
 /// A read-only file handle with `path()` attached and improved error messages. Contains only the
 /// methods and trait implementations which are allowed by a read-only file.
@@ -35,7 +34,7 @@ use super::{Error, PathInfo, Result};
 /// let expected = "foo\nbar";
 /// file.write_str(expected)?;
 ///
-/// let mut read = FileRead::read(example)?;
+/// let mut read = FileRead::open(example)?;
 /// let mut s = String::new();
 /// read.read_to_string(&mut s)?;
 /// assert_eq!(expected, s);
@@ -45,17 +44,17 @@ pub struct FileRead(pub(crate) FileOpen);
 
 impl FileRead {
     /// Open the file as read-only.
-    pub fn read<P: AsRef<Path>>(path: P) -> Result<FileRead> {
+    pub fn open<P: AsRef<Path>>(path: P) -> Result<FileRead> {
         let mut options = fs::OpenOptions::new();
         options.read(true);
         Ok(FileRead(FileOpen::open(path, options)?))
     }
 
     /// Shortcut to open the file if the path is already absolute.
-    pub(crate) fn read_path(path: PathFile) -> Result<FileRead> {
+    pub(crate) fn open_abs<P: Into<PathAbs>>(path: P) -> Result<FileRead> {
         let mut options = fs::OpenOptions::new();
         options.read(true);
-        Ok(FileRead(FileOpen::open_path(path, options)?))
+        Ok(FileRead(FileOpen::open_abs(path, options)?))
     }
 
     /// Read what remains of the file to a `String`.
diff --git a/src/write.rs b/src/write.rs
index 5915880..9f16eaa 100644
--- a/src/write.rs
+++ b/src/write.rs
@@ -13,8 +13,7 @@ use std::io;
 use std_prelude::*;
 
 use super::open::FileOpen;
-use super::PathFile;
-use super::{Error, PathInfo, Result};
+use super::{Error, PathAbs, PathFile, PathInfo, Result};
 
 /// A write-only file handle with `path()` attached and improved error messages. Contains only the
 /// methods and trait implementations which are allowed by a write-only file.
@@ -50,12 +49,12 @@ impl FileWrite {
     }
 
     /// Shortcut to open the file if the path is already absolute.
-    pub(crate) fn open_path(
-        path_file: PathFile,
+    pub(crate) fn open_abs<P: Into<PathAbs>>(
+        path: P,
         mut options: fs::OpenOptions,
     ) -> Result<FileWrite> {
         options.write(true);
-        Ok(FileWrite(FileOpen::open_path(path_file, options)?))
+        Ok(FileWrite(FileOpen::open_abs(path, options)?))
     }
 
     /// Open the file in write-only mode, truncating it first if it exists and creating it
@@ -68,7 +67,7 @@ impl FileWrite {
     }
 
     /// Open the file for appending, creating it if it doesn't exist.
-    pub fn append<P: AsRef<Path>>(path: P) -> Result<FileWrite> {
+    pub fn open_append<P: AsRef<Path>>(path: P) -> Result<FileWrite> {
         let mut options = fs::OpenOptions::new();
         options.append(true);
         options.create(true);
@@ -77,7 +76,7 @@ impl FileWrite {
 
     /// Open the file for editing (reading and writing) but do not create it
     /// if it doesn't exist.
-    pub fn edit<P: AsRef<Path>>(path: P) -> Result<FileWrite> {
+    pub fn open_edit<P: AsRef<Path>>(path: P) -> Result<FileWrite> {
         let mut options = fs::OpenOptions::new();
         options.read(true);
         FileWrite::open(path, options)


Test patch: