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

Remove Logger #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions pkl-bind/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Pkl Bind

Exposing the rust bindings for Apple's [Pkl language](https://pkl-lang.org).
5 changes: 3 additions & 2 deletions pkl-bind/src/evaluator/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/// Derive trait for unmarshalling data from the PKL
/// binary format as described
/// [here](https://pkl-lang.org/main/current/bindings-specification/binary-encoding.html).
Expand All @@ -14,6 +13,8 @@ pub trait Pkl {
mod tests {
use pkl_derive::Pkl;

use crate::log;

use super::*;

#[test]
Expand All @@ -37,6 +38,6 @@ mod tests {
let test = Test::unmarshal(data).unwrap();
assert_eq!(test.foo, 1);
assert_eq!(test.bar, 2);
println!("Unmarshalled: {:?}", test);
log!(1, "Unmarshalled: {:?}", test);
}
}
25 changes: 12 additions & 13 deletions pkl-bind/src/evaluator/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{sync::mpsc::{Sender, Receiver, channel}, any::Any, collections::HashMap, rc::Rc};
use std::{sync::mpsc::{Sender, channel}, any::Any, collections::HashMap};

use crate::evaluator::msg_api::incoming::IncomingMessage;

use super::{msg_api::{outgoing::{ResourceReader, ModuleReader, Evaluate}, incoming::EvaluateResponse}, module_source::ModuleSource, logger::Logger, evaluator_options::EvaluatorOptions, evaluator_manager::EvaluatorManager};
use super::{msg_api::{outgoing::{ResourceReader, ModuleReader}, incoming::EvaluateResponse},
module_source::ModuleSource, evaluator_options::EvaluatorOptions};

// Interface for evaluating pkl modules
pub struct Evaluator { // NOTE the lifetime allows us to ignore close() since at the end of the lifetime the Evaluator is killed automatically
pub evaluator_id: i64,
pub logger: Logger,
// pub manager: Option<Rc<&EvaluatorManager>>, //TODO fix the bidirectional reference
pub pending_requests: HashMap<i64, Sender<EvaluateResponse>>,
pub closed: bool,
Expand All @@ -20,7 +20,6 @@ impl Default for Evaluator {
fn default() -> Self {
Self {
evaluator_id: rand::random(),
logger: Default::default(),
// manager: Default::default(),
pending_requests: Default::default(),
closed: Default::default(),
Expand Down Expand Up @@ -55,16 +54,16 @@ impl EvaluatorMethods for Evaluator {
}

fn evaluate_expression_raw<T>(&self, source: &ModuleSource, expr: Option<String>) -> Result<T, &'static str> {
let request_id: i64 = rand::random::<i64>();
let (send, recv) = channel::<IncomingMessage>();
// let request_id: i64 = rand::random::<i64>();
// let (send, recv) = channel::<IncomingMessage>();

let msg = Evaluate {
request_id,
evaluator_id: self.evaluator_id,
module_uri: source.uri().to_string(),
module_text: source.contents().clone(), //FIXME badness
expr,
};
// let msg = Evaluate {
// request_id,
// evaluator_id: self.evaluator_id,
// module_uri: source.uri().to_string(),
// module_text: source.contents().clone(), //FIXME badness
// expr,
// };
todo!()
}

Expand Down
32 changes: 13 additions & 19 deletions pkl-bind/src/evaluator/evaluator_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use crate::evaluator::decoder::Pkl;
use crate::{evaluator::decoder::Pkl, log, plog};

use super::{evaluator::Evaluator, evaluator_options::EvaluatorOptions, msg_api::{incoming::IncomingMessage, outgoing::{OutgoingMessage, CreateEvaluator, CloseEvaluator, Evaluate, ListModulesResponse, PathElement}}};
use super::executor::Executor;
Expand Down Expand Up @@ -54,7 +54,6 @@ impl EvaluatorManager {

let evaluator = Evaluator {
evaluator_id: eval_resp.evaluator_id.unwrap(), // if we did not error, then this is guaranteed
logger: Default::default(),
// manager: Some(Rc::new(self)), // FIXME see Evaluator.rs
pending_requests: Default::default(),
closed: false,
Expand Down Expand Up @@ -97,23 +96,23 @@ impl EvaluatorManager {
self.exec.send(OutgoingMessage::CloseEvaluator(close_msg));
//TODO get the data and decode

println!("Data: {:?}", x);
log!(1, "Data: {:?}", x);
let data = x.clone().result.expect("failed to get result");

// FIXME fails to decode, need to unmarshal data
print!("Data: ");
log!(1, "Data: ");
for d in &data {
print!("{:#04X}, ", d);
plog!(1, "{:#04X}, ", d);
}
println!();
log!(1, "");

let res = T::unmarshal(data);
println!("Res: {:?}", res);
log!(1, "Res: {:?}", res);
return res;
},
IncomingMessage::ReadResource(x) => todo!(),
IncomingMessage::ReadModule(x) => todo!(),
IncomingMessage::ListResources(x) => todo!(),
IncomingMessage::ReadResource(_x) => todo!(),
IncomingMessage::ReadModule(_x) => todo!(),
IncomingMessage::ListResources(_x) => todo!(),
IncomingMessage::ListModules(x) => {
// get all the files in the module:
let path = PathBuf::from(file.clone());
Expand All @@ -122,7 +121,7 @@ impl EvaluatorManager {
// files = std::fs::read_dir(path); // TODO
}

let mut modules: Vec<PathElement> = vec![];
let /*mut*/ modules: Vec<PathElement> = vec![];
// for file in files {
// // TODO make module
// }
Expand All @@ -134,7 +133,7 @@ impl EvaluatorManager {
error: None,
};

let resp = self.exec.senrec(OutgoingMessage::ListModulesResponse(list_resp)).expect("Failed to send/receive data");
let _resp = self.exec.senrec(OutgoingMessage::ListModulesResponse(list_resp)).expect("Failed to send/receive data");

},
IncomingMessage::Log(_) => todo!(),
Expand All @@ -146,10 +145,6 @@ impl EvaluatorManager {
// send any read_module_response
// send the close evaluator
}

pub fn create_evaluator(&self, none: Option<()>) -> i64 {
todo!()
}
}

impl Drop for EvaluatorManager {
Expand All @@ -170,7 +165,7 @@ impl Drop for EvaluatorManager {
#[cfg(test)]
mod tests {
use pkl_derive::Pkl;
use crate::evaluator::decoder::Pkl;
use crate::evaluator::{decoder::Pkl, module_source::file_source};

use super::*;

Expand All @@ -194,8 +189,7 @@ mod tests {

let evaluator = eval.new_evaluator(None).expect("Failed to create a new evaluator");

//TODO remove my paths
let test: Test = eval.evaluate_module::<Test>("file:///home/stormblessed/Code/pkl-rust/pkl-bind/src/evaluator/tests/test.pkl".into(), evaluator).expect("Failed to obtain result");
let test: Test = eval.evaluate_module::<Test>(file_source("src/evaluator/tests/test.pkl".into()).uri().to_string(), evaluator).expect("Failed to obtain result");

assert_eq!(test.foo, 1);
assert_eq!(test.bar, 2);
Expand Down
16 changes: 6 additions & 10 deletions pkl-bind/src/evaluator/evaluator_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use std::{collections::HashMap, path::PathBuf};
use dirs::home_dir;
use std::env;

use super::{msg_api::outgoing::{ResourceReader, ModuleReader}, logger::Logger};
use super::msg_api::outgoing::{ResourceReader, ModuleReader};

//TODO documentation
//TODO this should be taken from a pkl file in the future
pub struct EvaluatorOptions {
pub properties: HashMap<String, String>,
pub env: HashMap<String, String>,
pub module_paths: Vec<String>,
pub logger: Logger,
pub output_format: String,
pub allowed_modules: Vec<String>,
pub allowed_resources: Vec<String>,
Expand All @@ -19,7 +18,7 @@ pub struct EvaluatorOptions {
pub cache_dir: PathBuf,
pub root_dir: String, //TODO this should also be a path
pub project_dir: String, //TODO this should be a path
pub declared_project_dependency: ProjectDependencies
pub declared_project_dependency: ProjectDependencies,
}

macro_rules! vec_of_strings {
Expand Down Expand Up @@ -48,7 +47,6 @@ impl Default for EvaluatorOptions {
properties: Default::default(),
env: os_env,
module_paths: Default::default(),
logger: Default::default(),
output_format: Default::default(),
allowed_modules,
allowed_resources,
Expand All @@ -63,20 +61,20 @@ impl Default for EvaluatorOptions {
}

#[derive(Default)]
struct ProjectRemoteDependency {
pub struct ProjectRemoteDependency {
package_uri: String, // TODO this should be a path
checksums: String, //TODO should this be unified with the msg_api::Checksums type?
}

#[derive(Default)]
struct ProjectLocalDependency {
pub struct ProjectLocalDependency {
package_uri: String,
project_file_uri: String,
dependencies: ProjectDependencies
}

#[derive(Default)]
struct ProjectDependencies {
pub struct ProjectDependencies {
local_dependencies: HashMap<String, ProjectLocalDependency>,
remote_dependencies: HashMap<String, ProjectRemoteDependency>,
}
Expand All @@ -87,8 +85,6 @@ mod tests {

#[test]
fn default_options_test() {
let defaults: EvaluatorOptions = Default::default();

defaults.logger.trace("hello, ".into(), "world".into());
let _defaults: EvaluatorOptions = Default::default();
}
}
36 changes: 18 additions & 18 deletions pkl-bind/src/evaluator/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{process::{Command, Child, Stdio, ChildStdin, ChildStdout}, sync::{mpsc::{Sender, Receiver, channel, RecvError}, atomic::AtomicBool}, os::unix::process::CommandExt, io::{Write, BufReader, BufWriter, Read}, cmp::Reverse, thread::{self, JoinHandle}, time::{Duration, Instant}};
use std::{process::{Command, Child, Stdio, ChildStdin, ChildStdout}, sync::mpsc::RecvError, io::{Write, Read}};

use serde::Serialize;
use crate::log;

use super::msg_api::{incoming::*, outgoing::*, code::*};

Expand Down Expand Up @@ -102,7 +102,7 @@ impl Executor {
let mut sender = self.child_in.take().expect("Failed to take");

sender.write_all(&message).expect("Failed to send message");
// println!("Sent message: {:?}", msg);
// log!(1,"Sent message: {:?}", msg);

self.child_in = Some(sender);
}
Expand All @@ -122,45 +122,45 @@ impl Executor {
// TODO not very DRY, but this might be the most idiomatic way to use serde
match prefix {
MessageCode::NewEvaluatorResponse => {
println!("Matched new evaluator, Code: {:02X?}", prefix); //TODO Switch to logging
log!(1,"Matched new evaluator, Code: {:02X?}", prefix); //TODO Switch to logging
match rmp_serde::from_read::<_, CreateEvaluatorResponse>(&mut out) {
Ok(msg) => value = Some(IncomingMessage::CreateEvaluatorResponse(msg)),
Err(err) => eprintln!("Error decoding the message: {}", err),
Err(err) => log!(1,"Error decoding the message: {}", err),
}
},
MessageCode::EvaluateResponse => {
println!("Matched new evaluator, Code: {:02X?}", prefix);
log!(1,"Matched new evaluator, Code: {:02X?}", prefix);
match rmp_serde::from_read::<_, EvaluateResponse>(&mut out) {
Ok(msg) => value = Some(IncomingMessage::EvaluateResponse(msg)),
Err(err) => eprintln!("Error decoding the message: {}", err),
Err(err) => log!(1,"Error decoding the message: {}", err),
}
},
MessageCode::EvaluateReadModule => {
println!("Matched new evaluator, Code: {:02X?}", prefix);
log!(1,"Matched new evaluator, Code: {:02X?}", prefix);
match rmp_serde::from_read::<_, ReadModule>(&mut out) {
Ok(msg) => value = Some(IncomingMessage::ReadModule(msg)),
Err(err) => eprintln!("Error decoding the message: {}", err),
Err(err) => log!(1,"Error decoding the message: {}", err),
}
},
MessageCode::ListResourcesRequest => {
println!("Matched new evaluator, Code: {:02X?}", prefix);
log!(1,"Matched new evaluator, Code: {:02X?}", prefix);
match rmp_serde::from_read::<_, ListResources>(&mut out) {
Ok(msg) => value = Some(IncomingMessage::ListResources(msg)),
Err(err) => eprintln!("Error decoding the message: {}", err),
Err(err) => log!(1,"Error decoding the message: {}", err),
}
},
MessageCode::ListModulesRequest => {
println!("Matched new evaluator, Code: {:02X?}", prefix);
log!(1,"Matched new evaluator, Code: {:02X?}", prefix);
match rmp_serde::from_read::<_, ListModules>(&mut out) {
Ok(msg) => value = Some(IncomingMessage::ListModules(msg)),
Err(err) => eprintln!("Error decoding the message: {}", err),
Err(err) => log!(1,"Error decoding the message: {}", err),
}
},
MessageCode::EvaluateLog => {
println!("Matched new evaluator, Code: {:02X?}", prefix);
log!(1,"Matched new evaluator, Code: {:02X?}", prefix);
match rmp_serde::from_read::<_, Log>(&mut out) {
Ok(msg) => value = Some(IncomingMessage::Log(msg)),
Err(err) => eprintln!("Error decoding the message: {}", err),
Err(err) => log!(1,"Error decoding the message: {}", err),
}
},
_ => {
Expand Down Expand Up @@ -200,7 +200,7 @@ mod tests {
for i in vec {
print!("0x{i:02X}, ");
}
println!("]");
log!(1,"]");

}

Expand Down Expand Up @@ -237,7 +237,7 @@ mod tests {
let test1 = pack_message(OutgoingMessage::CreateEvaluator(create_eval)).expect("Failed to pack");

let _ = eval.child_in.take().unwrap().write(&test1.to_vec());
// println!("Wrote message: {:?}", &test1.to_vec());
// log!(1,"Wrote message: {:?}", &test1.to_vec());
// print_binary(&test1.to_vec());
let a = eval.child_out.take().unwrap().read_exact(&mut r);
// print_binary(&r);
Expand Down Expand Up @@ -276,7 +276,7 @@ mod tests {
// print_binary(&test1);

let result = eval.senrec(OutgoingMessage::CreateEvaluator(create_eval)).expect("Failed to accept");
println!("Received evaluator response: {:?}", result);
log!(1,"Received evaluator response: {:?}", result);
}

}
Loading