Skip to content

Commit

Permalink
Adds file_exists function (#43)
Browse files Browse the repository at this point in the history
* Adds file_exists

* Updates the README
  • Loading branch information
ZeWaka authored Oct 13, 2020
1 parent 1088c53 commit dbe0c07
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The default features are:

Additional features are:
* url: Faster replacements for `url_encode` and `url_decode`.
* file: Faster replacements for `file2text` and `text2file`.
* file: Faster replacements for `file2text` and `text2file`, as well as reading or checking if files exist.
* hash: Faster replacement for `md5`, support for SHA-1, SHA-256, and SHA-512. Requires OpenSSL on Linux.

## Installing
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn main() {
f,
r#"
#define rustg_file_read(fname) call(RUST_G, "file_read")(fname)
#define rustg_file_exists(fname) call(RUST_G, "file_exists")(fname)
#define rustg_file_write(text, fname) call(RUST_G, "file_write")(text, fname)
#define rustg_file_append(text, fname) call(RUST_G, "file_append")(text, fname)
Expand Down
9 changes: 9 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ byond_fn! { file_read(path) {
read(path).ok()
} }

byond_fn! { file_exists(path) {
Some(exists(path))
} }

byond_fn! { file_write(data, path) {
write(data, path).err()
} }
Expand All @@ -27,6 +31,11 @@ fn read(path: &str) -> Result<String> {
Ok(content)
}

fn exists(path: &str) -> String {
let path = std::path::Path::new(path);
path.exists().to_string()
}

fn write(data: &str, path: &str) -> Result<usize> {
let path: &std::path::Path = path.as_ref();
if let Some(parent) = path.parent() {
Expand Down

0 comments on commit dbe0c07

Please sign in to comment.