-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#define rustutils_file_write_b64decode(text, fname) CALL_LIB(RUST_UTILS, "file_write")(text, fname, "true") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::{ | ||
io, | ||
result, | ||
str::Utf8Error, | ||
}; | ||
use thiserror::Error; | ||
|
||
pub type Result<T> = result::Result<T, Error>; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Illegal null character in string.")] | ||
Null, | ||
#[error("Invalid UTF-8 character at position {position}.")] | ||
Utf8 { source: Utf8Error, position: usize }, | ||
#[error("Invalid or empty filename specified.")] | ||
InvalidFilename, | ||
#[error(transparent)] | ||
Io(#[from] io::Error), | ||
#[error("Invalid algorithm specified.")] | ||
InvalidAlgorithm, | ||
} | ||
|
||
|
||
impl From<Utf8Error> for Error { | ||
fn from(source: Utf8Error) -> Self { | ||
Self::Utf8 { | ||
source, | ||
position: source.valid_up_to(), | ||
} | ||
} | ||
} | ||
|
||
impl From<Error> for String { | ||
fn from(error: Error) -> Self { | ||
error.to_string() | ||
} | ||
} | ||
|
||
impl From<Error> for Vec<u8> { | ||
fn from(error: Error) -> Self { | ||
error.to_string().into_bytes() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::error::Result; | ||
use base64::Engine; | ||
use std::{ | ||
fs::File, | ||
io::{BufWriter, Write}, | ||
}; | ||
byond_fn!(fn file_write(data, path, ...rest) { | ||
let mut should_decode_b64 = false; | ||
if rest.first().map(|x| &**x) == Some("true") { | ||
should_decode_b64 = true; | ||
} | ||
write(data, path, should_decode_b64).err() | ||
}); | ||
|
||
fn write(data: &str, path: &str, base64decode: bool) -> Result<usize> { | ||
let path: &std::path::Path = path.as_ref(); | ||
if let Some(parent) = path.parent() { | ||
std::fs::create_dir_all(parent)?; | ||
} | ||
|
||
let mut file = BufWriter::new(File::create(path)?); | ||
|
||
let written = if base64decode { | ||
file.write( | ||
base64::prelude::BASE64_STANDARD | ||
.decode(data) | ||
.unwrap() | ||
.as_ref(), | ||
)? | ||
} else { | ||
file.write(data.as_bytes())? | ||
}; | ||
|
||
file.flush()?; | ||
file.into_inner() | ||
.map_err(|e| std::io::Error::new(e.error().kind(), e.error().to_string()))? // This is god-awful, but the compiler REFUSES to let me get an owned copy of `e` | ||
.sync_all()?; | ||
|
||
Ok(written) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters