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

Feature/time functions #91

Draft
wants to merge 6 commits 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
4 changes: 3 additions & 1 deletion elprop/elprop
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/sh
#!/usr/bin/env bash

cd "$(dirname "$0")"

cargo build --workspace
cargo run -- "$@"
3 changes: 3 additions & 0 deletions elprop/src/bin/code/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ impl Function {
"StringOrSymbol" => {
Ok(Type::Object(vec![ObjectType::String, ObjectType::Symbol]))
}
"LispTime" => {
Ok(Type::Object(vec![ObjectType::Cons]))
}
"Symbol" => Ok(Type::Object(vec![ObjectType::Symbol])),
"Number" | "NumberValue" => {
Ok(Type::Object(vec![ObjectType::Integer, ObjectType::Float]))
Expand Down
10 changes: 10 additions & 0 deletions elprop/src/bin/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use code::output::{Output, Status};
use proptest::prelude::TestCaseError;
use proptest::test_runner::{Config, TestError, TestRunner};
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Write};
use std::process::Stdio;
use std::{fs, path::PathBuf};
Expand Down Expand Up @@ -42,14 +43,19 @@ fn main() {
let rune_panicked = RefCell::new(false);
let master_count = RefCell::new(0);

let arguments = RefCell::new(HashMap::<String, Vec<String>>::new());
let outputs = RefCell::new(Vec::new());
for func in config.functions {
let name = func.name.clone();
arguments.borrow_mut().entry(name.clone()).or_default();

let result = runner.run(&func.strategy(), |input| {
if *rune_panicked.borrow() {
return Err(TestCaseError::Reject("Rune panicked".into()));
}
let body = code::data::print_args(&input);
arguments.borrow_mut().entry(name.clone()).and_modify(|v| v.push(body.clone()));

// send to emacs
println!(";; sending to Emacs");
let test_str = format!(";; ELPROP_START\n({name} {body})\n;; ELPROP_END");
Expand Down Expand Up @@ -106,6 +112,10 @@ fn main() {
outputs.borrow_mut().push(output);
}

let args_file = target.join("arguments.json");
let json = serde_json::to_string(&*arguments.borrow()).expect("Malformed Arguments JSON");
fs::write(args_file, json).unwrap();

let _ = child.kill();
let json = serde_json::to_string(&*outputs.borrow()).expect("Malformed Output JSON");
let output_file = target.join("output.json");
Expand Down
65 changes: 64 additions & 1 deletion src/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,16 @@ fn disable_debug() -> bool {
#[elprop("[\x00-\x7F]*", _)]
fn base64_encode_string(string: &str, line_break: OptionalFlag) -> Result<String> {
if string.is_ascii() {
Ok(base64_encode(string, line_break.is_some(), true, true))
Ok(base64_encode(string, line_break.is_some(), true, false))
} else {
Err(anyhow!("Multibyte character in data for base64 encoding"))
}
}

#[defun]
fn base64url_encode_string(string: &str, no_pad: OptionalFlag) -> Result<String> {
if string.is_ascii() {
Ok(base64_encode(string, false, no_pad.is_none(), true))
} else {
Err(anyhow!("Multibyte character in data for base64 encoding"))
}
Expand All @@ -991,14 +1000,68 @@ fn base64_encode(string: &str, _line_break: bool, pad: bool, base64url: bool) ->
engine.encode(string)
}

#[defun]
fn base64_decode_string(
string: &str,
base64url: OptionalFlag,
ignore_invalid: OptionalFlag,
) -> Result<Vec<u8>> {
let error_msg = "Invalid base64 data";
base64_decode(string, base64url.is_some(), ignore_invalid.is_some())
.map_err(|_| anyhow!(error_msg))
}

fn base64_decode(
string: &str,
base64url: bool,
ignore_invalid: bool,
) -> Result<Vec<u8>, base64::DecodeError> {
let config = base64::engine::GeneralPurposeConfig::new()
.with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent)
.with_decode_allow_trailing_bits(true);
let alphabets = if base64url { base64::alphabet::URL_SAFE } else { base64::alphabet::STANDARD };
let engine = base64::engine::GeneralPurpose::new(&alphabets, config);
if ignore_invalid {
let santizied_string: String =
string.chars().filter(|c| alphabets.as_str().contains(*c)).collect();
engine.decode(santizied_string)
} else {
engine.decode(string)
}
}

#[cfg(test)]
mod test {
use crate::{fns::levenshtein_distance, interpreter::assert_lisp};

#[test]
fn test_base64_encode_string() {
assert_lisp("(base64-encode-string \"hello\")", "\"aGVsbG8=\"");
assert_lisp("(base64-encode-string \"aa>\")", "\"YWE+\"");
assert_lisp("(base64-encode-string \" a>\")", "\"IGE+\"");
}

#[test]
fn test_base64url_encode_string() {
assert_lisp("(base64url-encode-string \" \")", "\"IA==\"");
assert_lisp("(base64url-encode-string \"aa>\")", "\"YWE-\"");
assert_lisp("(base64url-encode-string \" a>\")", "\"IGE-\"");
}

// Need a way to convert to ByteString instead of String
#[test]
#[ignore]
fn test_base64_decode_string() {
assert_lisp("(base64-decode-string \"aa\" nil t)", "\"i\"");
// assert_lisp("(base64-decode-string \"0+\" nil t)", r#"\323"#);
// assert_lisp("(base64-decode-string \"Wj1Yse54𐩃-N\" t t)", "\"Z=X\261\356x\370\"");
// assert_lisp("(base64-encode-string \"aa>\")", "\"YWE+\"");
// assert_lisp("(base64-encode-string \" a>\")", "\"IGE+\"");
}
#[test]
#[ignore]
fn test_base64_url_decode_string() {
assert_lisp("(base64-decode-string \"Wj1Yse54𐩃-N\" t t)", "\"Z=X\\261\\356x\\370\"");
}

#[test]
Expand Down
Loading
Loading