Skip to content

Commit 6490228

Browse files
committed
first release
1 parent 03338d2 commit 6490228

File tree

6 files changed

+137
-40
lines changed

6 files changed

+137
-40
lines changed

Cargo.toml

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
[package]
2+
authors = ["Garrett Berg <[email protected]>"]
3+
description = "Types for making working with the filesystem ergonomic, therefore fun."
4+
documentation = "https://docs.rs/ergo_fs"
5+
keywords = [
6+
"filesystem",
7+
"path",
8+
"file",
9+
"types",
10+
"ergonomic",
11+
]
12+
license = "MIT OR Apache-2.0"
213
name = "ergo_fs"
14+
readme = "README.md"
15+
repository = "https://github.com/vitiral/ergo_fs"
316
version = "0.1.0"
4-
authors = ["Garrett Berg <[email protected]>"]
517

618
[dependencies]
19+
path_abs = "^0.3.0"
20+
std_prelude = "^0.2.9"
721
tar = "^0.4.14"
822
tempdir = "^0.3.5"
923
walkdir = "^2.0.1"
10-
path_Abs = "^0.3.0"
11-
lockfile = "*"

LICENSE-APACHE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2018 Garrett Berg, [email protected]
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

LICENSE-MIT

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Garrett Berg, [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# ergo_fs: types for making working with the filesystem ergonomic, therefore fun.
2+
[![Build Status](https://travis-ci.org/vitiral/ergo_fs.svg?branch=windows)](https://travis-ci.org/vitiral/ergo_fs)
3+
[![Build status](https://ci.appveyor.com/api/projects/status/vgis54solhygre0n?svg=true)](https://ci.appveyor.com/project/vitiral/path-abs)
4+
[![Docs](https://docs.rs/ergo_fs/badge.svg)](https://docs.rs/ergo_fs)
5+
6+
This library aims to make working with the filesystem in rust more ergonomic.
7+
8+
**See the [library docs](https://docs.rs/ergo_fs) for more information**
9+
10+
# LICENSE
11+
The source code in this repository is Licensed under either of
12+
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
13+
http://www.apache.org/licenses/LICENSE-2.0)
14+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or
15+
http://opensource.org/licenses/MIT)
16+
17+
at your option.
18+
19+
Unless you explicitly state otherwise, any contribution intentionally submitted
20+
for inclusion in the work by you, as defined in the Apache-2.0 license, shall
21+
be dual licensed as above, without any additional terms or conditions.

src/lib.rs

+39-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
/* Copyright (c) 2018 Garrett Berg, [email protected]
2+
*
3+
* Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4+
* http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5+
* http://opensource.org/licenses/MIT>, at your option. This file may not be
6+
* copied, modified, or distributed except according to those terms.
7+
*/
18
//! ergo_fs: types for making working with the filesystem ergonomic, therefore fun.
29
//!
10+
//! ## Purpose
11+
//!
312
//! This crate provides a minimal set of common types and methods for working with the filesystem.
413
//! These types aim to provide:
514
//!
@@ -16,9 +25,26 @@
1625
//! of files.
1726
//! - [`walkdir`](https://github.com/BurntSushi/walkdir): Provides an efficient and cross platform
1827
//! implementation of recursive directory traversal.
28+
//! - [`std_prelude`]: prelude that the rust stdlib should have always had.
1929
//!
2030
//! Consider supporting their development individually and starring them on github.
2131
//!
32+
//! ## How to Use
33+
//! ergo_fs is intended to be a "standard library" of filesystem types. Therefore you should
34+
//! use like so:
35+
//!
36+
//! ```
37+
//! extern crate ergo_fs;
38+
//! use ergo_fs::*;
39+
//! # fn try_main() -> ::std::io::Result<()> {
40+
//! # Ok(()) } fn main() { try_main().unwrap() }
41+
//! ```
42+
//!
43+
//! This will also export the [`std_prelude`] crate into your namespace, giving you automatic
44+
//! access to the `io::{Read, Write}` traits, `path::{Path, PathBuf}` types and more.
45+
//!
46+
//! [`std_prelude`]: http://github.com/vitiral/std_prelude
47+
//!
2248
//! # Types
2349
//! This library provides several kinds of types which improve and expand on `std::fs` and
2450
//! `std::path`, as well as provide new functionality like temporary files and tar archives.
@@ -70,17 +96,18 @@
7096
//!
7197
//! ### Examples
7298
//! ```rust
99+
//! # extern crate ergo_fs;
73100
//! use ergo_fs::{WalkDir, PathType};
74101
//!
75-
//! # fn try_main() -> Result<(), ::std::io::Error> {
102+
//! # fn try_main() -> ::std::io::Result<()> {
103+
//! // note: this will error
76104
//! for entry in WalkDir::new("foo").min_depth(1) {
77-
//! match PathType::new(entry?)? {
105+
//! match PathType::new(entry?.path())? {
78106
//! PathType::File(file) => println!("got file {}", file.display()),
79107
//! PathType::Dir(dir) => println!("got dir {}", dir.display()),
80108
//! }
81109
//! }
82-
//! # Ok(())
83-
//! # }
110+
//! # Ok(()) } fn main() { try_main().unwrap_err(); }
84111
//! ```
85112
//!
86113
//! ## Tar Files
@@ -98,25 +125,24 @@
98125
//! > would be interested in using `path_abs` under the hood?
99126
//!
100127
//! ```rust
101-
//! use ergo_fs::{PathTmp, FileWrite};
128+
//! # extern crate ergo_fs;
129+
//! use ergo_fs::*;
102130
//! use ergo_fs::tar::Builder;
103131
//!
104-
//! # fn try_main() -> Result<(), ::std::io::Error> {
132+
//! # fn try_main() -> ::std::io::Result<()> {
105133
//! // We are going to tar the source code of this library
106134
//!
107135
//! let tmp = PathTmp::create("tmp")?;
108-
//! let mut tarfile = PathFile::create(tmp.join("src.tar"))?;
136+
//! let mut tarfile = FileWrite::create(tmp.join("src.tar"))?;
109137
//!
110138
//! // tar the source directory
111-
//! let mut tar = Builder::new(tarfile)
139+
//! let mut tar = Builder::new(tarfile);
112140
//! tar.append_dir_all("src", ".")?;
113141
//! tar.finish();
114-
//! let tarfile = tar.into_inner();
115-
//! tarfile.flush();
142+
//! let tarfile = tar.into_inner()?;
116143
//!
117144
//! // A tarfile now exists, do whatever you would like with it.
118-
//! # Ok(())
119-
//! # }
145+
//! # Ok(()) } fn main() { try_main().unwrap() }
120146
//! ```
121147
122148
extern crate std_prelude;
@@ -128,6 +154,7 @@ extern crate path_abs;
128154
pub extern crate tar;
129155
pub extern crate walkdir;
130156

157+
pub use std_prelude::*;
131158
pub use path_abs::{PathAbs, PathArc, PathFile, PathDir, PathType, FileRead, FileWrite, FileEdit};
132159
pub use walkdir::{WalkDir, Error as WalkError};
133160

src/tmp.rs

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Almost all of this code is copy/pasted from the tempdir crate.
1+
// Copyright (c) 2018 Garrett Berg, [email protected]
22
//
3-
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
3+
// Almost all of the docs in this module are copy/pasted from the tempdir crate.
4+
// Copyright (c) 2015 The Rust Project Developers. See the COPYRIGHT
45
// file at the top-level directory of this distribution and at
56
// http://rust-lang.org/COPYRIGHT.
67
//
@@ -11,12 +12,11 @@
1112
// except according to those terms.
1213

1314
use std::env;
14-
use std::fs;
15-
use std::io;
15+
use std::fmt;
1616

1717
use std_prelude::*;
1818
use tempdir;
19-
use path_abs::{PathArc, PathAbs, PathDir};
19+
use path_abs::{PathArc, PathAbs, PathDir, Error, Result};
2020

2121
/// A `PathDir` that is automatically deleted when it goes out of scope.
2222
///
@@ -77,18 +77,18 @@ impl PathTmp {
7777
/// ```
7878
/// use ergo_fs::{PathFile, PathTmp};
7979
///
80-
/// let tmp_dir = PathTmp::create("example").unwrap()
80+
/// let tmp_dir = PathTmp::create("example").unwrap();
8181
/// let file = PathFile::create(tmp_dir.join("temporary-note.txt")).unwrap();
8282
/// let message = "This file existed, but only for a moment.";
8383
/// file.write_str(message).unwrap();
84-
/// assert_eq!(file.read_string(), message);
84+
/// assert_eq!(file.read_string().unwrap(), message);
8585
///
8686
/// // Close the tmp_dir manually (would automatically happen when dropped).
8787
/// // All contents are automatically deleted.
88-
/// drop(tmp_dir.close().unwrap();
88+
/// tmp_dir.close().unwrap();
8989
/// assert!(!file.exists());
9090
/// ```
91-
pub fn create(prefix: &str) -> io::Result<PathTmp> {
91+
pub fn create(prefix: &str) -> Result<PathTmp> {
9292
PathTmp::create_in(&env::temp_dir(), prefix)
9393
}
9494

@@ -105,22 +105,23 @@ impl PathTmp {
105105
/// ```
106106
/// use ergo_fs::{PathFile, PathTmp};
107107
///
108-
/// let tmp_dir = PathTmp::create_in(".", "example").unwrap()
108+
/// let tmp_dir = PathTmp::create_in(".", "example").unwrap();
109109
/// let file = PathFile::create(tmp_dir.join("temporary-note.txt")).unwrap();
110110
/// let message = "This file existed, but only for a moment.";
111111
/// file.write_str(message).unwrap();
112-
/// assert_eq!(file.read_string(), message);
112+
/// assert_eq!(file.read_string().unwrap(), message);
113113
///
114114
/// // Close the tmp_dir manually (would automatically happen when dropped).
115115
/// // All contents are automatically deleted.
116-
/// drop(tmp_dir.close().unwrap();
116+
/// tmp_dir.close().unwrap();
117117
/// assert!(!file.exists());
118118
/// ```
119-
pub fn create_in<P: AsRef<Path>>(base: P, prefix: &str) -> io::Result<PathTmp> {
120-
let tmp = TempDir::new_in(&base, prefix).map_err(|err| {
121-
io::Error::new(
122-
err.kind(),
123-
format!("{} when creating tmpdir in {}", err, base),
119+
pub fn create_in<P: AsRef<Path>>(base: P, prefix: &str) -> Result<PathTmp> {
120+
let tmp = tempdir::TempDir::new_in(&base, prefix).map_err(|err| {
121+
Error::new(
122+
err,
123+
"creating tmpdir",
124+
PathArc::new(&base),
124125
)
125126
})?;
126127

@@ -140,16 +141,16 @@ impl PathTmp {
140141
/// ```
141142
/// use ergo_fs::PathTmp;
142143
///
143-
/// let tmp_dir = PathTmp::create_in(".", "persist").unwrap()
144-
/// let dir = tmp_dir.persisit();
144+
/// let tmp_dir = PathTmp::create_in(".", "persist").unwrap();
145+
/// let dir = tmp_dir.persist();
145146
///
146147
/// // The directory is now persisted to disk
147148
/// assert!(dir.exists());
148149
///
149150
/// // It can still be manually removed though.
150151
/// dir.remove().unwrap();
151152
/// ```
152-
pub fn persist(mut self) -> PathDir {
153+
pub fn persist(self) -> PathDir {
153154
self.tmp.into_path();
154155
self.dir
155156
}
@@ -168,11 +169,13 @@ impl PathTmp {
168169
/// may be platform specific.
169170
///
170171
/// [`std::io::Error`]: http://doc.rust-lang.org/std/io/struct.Error.html
171-
pub fn close(mut self) -> io::Result<()> {
172+
pub fn close(self) -> Result<()> {
173+
let dir = self.dir;
172174
self.tmp.close().map_err(|err| {
173-
io::Error::new(
174-
err.kind(),
175-
format!("{} when removing {}", err, self.dir),
175+
Error::new(
176+
err,
177+
"removing",
178+
dir.into(),
176179
)
177180
})
178181
}
@@ -196,7 +199,7 @@ impl Hash for PathTmp {
196199
}
197200

198201
impl AsRef<PathDir> for PathTmp {
199-
fn as_ref(&self) -> &PathAbs {
202+
fn as_ref(&self) -> &PathDir {
200203
&self.dir
201204
}
202205
}

0 commit comments

Comments
 (0)