Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New FFI Macro #132

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 71 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ debug = true

[dependencies]
thiserror = "1.0"
byond_fn = "0.5"
flume = { version = "0.10", optional = true }
chrono = { version = "0.4", optional = true }
base64 = { version = "0.13", optional = true }
Expand Down
16 changes: 8 additions & 8 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use byond_fn::byond_fn;
use serde_json::Value;
use std::cmp;

const VALID_JSON_MAX_RECURSION_DEPTH: usize = 8;

byond_fn!(fn json_is_valid(text) {
let value = match serde_json::from_str::<Value>(text) {
Ok(value) => value,
Err(_) => return Some("false".to_owned())
};

Some(get_recursion_level(&value).is_ok().to_string())
});
#[byond_fn]
fn json_is_valid(text: &str) -> bool {
match serde_json::from_str::<Value>(text) {
Ok(value) => get_recursion_level(&value).is_ok(),
Err(_) => false,
}
}

/// Gets the recursion level of the given value
/// If it is above VALID_JSON_MAX_RECURSION_DEPTH, returns Err(())
Expand Down
46 changes: 23 additions & 23 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@ use std::{
path::Path,
};

use byond_fn::byond_fn;

thread_local! {
static FILE_MAP: RefCell<HashMap<OsString, File>> = RefCell::new(HashMap::new());
}

byond_fn!(fn log_write(path, data, ...rest) {
FILE_MAP.with(|cell| -> Result<()> {
#[byond_fn]
fn log_write(path: &Path, data: &str, with_format: Option<bool>) -> Result<()> {
FILE_MAP.with(|cell| {
// open file
let mut map = cell.borrow_mut();
let path = Path::new(path as &str);
let file = match map.entry(path.into()) {
Entry::Occupied(elem) => elem.into_mut(),
Entry::Vacant(elem) => elem.insert(open(path)?),
};

if rest.first().map(|x| &**x) == Some("false") {
// Write the data to the file with no accoutrements.
write!(file, "{}", data)?;
} else {
if with_format.unwrap_or(true) {
// write first line, timestamped
let mut iter = data.split('\n');
if let Some(line) = iter.next() {
writeln!(file, "[{}] {}", Utc::now().format("%F %T%.3f"), line)?;
let time = Utc::now().format("%F %T%.3f");
writeln!(file, "[{time}] {line}")?;
}

// write remaining lines
// write the rest of the lines
for line in iter {
writeln!(file, " - {}", line)?;
writeln!(file, "{line}")?;
}
} else {
// Write the data to the file with no accoutrement's.
write!(file, "{data}")?;
}

Ok(())
}).err()
});

byond_fn!(
fn log_close_all() {
FILE_MAP.with(|cell| {
let mut map = cell.borrow_mut();
map.clear();
});
Some("")
}
);
})
}

#[byond_fn]
fn log_close_all() {
FILE_MAP.with(|cell| {
let mut map = cell.borrow_mut();
map.clear();
});
}

fn open(path: &Path) -> Result<File> {
if let Some(parent) = path.parent() {
Expand Down
52 changes: 25 additions & 27 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
use std::time::{Duration, SystemTime, SystemTimeError};
use std::{
cell::RefCell,
collections::hash_map::{Entry, HashMap},
time::Instant,
};

use byond_fn::byond_fn;

thread_local!( static INSTANTS: RefCell<HashMap<String, Instant>> = RefCell::new(HashMap::new()) );

byond_fn!(fn time_microseconds(instant_id) {
fn time(instant_id: String) -> Duration {
INSTANTS.with(|instants| {
let mut map = instants.borrow_mut();
let instant = match map.entry(instant_id.into()) {
Entry::Occupied(elem) => elem.into_mut(),
Entry::Vacant(elem) => elem.insert(Instant::now()),
};
Some(instant.elapsed().as_micros().to_string())
instant.elapsed()
})
});
}

byond_fn!(fn time_milliseconds(instant_id) {
INSTANTS.with(|instants| {
let mut map = instants.borrow_mut();
let instant = match map.entry(instant_id.into()) {
Entry::Occupied(elem) => elem.into_mut(),
Entry::Vacant(elem) => elem.insert(Instant::now()),
};
Some(instant.elapsed().as_millis().to_string())
})
});
#[byond_fn]
fn time_microseconds(instant_id: String) -> String {
time(instant_id).as_micros().to_string()
}

#[byond_fn]
fn time_milliseconds(instant_id: String) -> String {
time(instant_id).as_millis().to_string()
}

byond_fn!(fn time_reset(instant_id) {
#[byond_fn]
fn time_reset(instant_id: String) {
INSTANTS.with(|instants| {
let mut map = instants.borrow_mut();
map.insert(instant_id.into(), Instant::now());
Some("")
})
});
}

byond_fn!(
fn unix_timestamp() {
Some(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs_f64()
.to_string(),
)
}
);
#[byond_fn]
fn unix_timestamp() -> Result<String, SystemTimeError> {
SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|x| x.as_secs_f64())
.map(|x| x.to_string())
}
Loading