Skip to content

Commit

Permalink
fix: Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Jan 29, 2024
1 parent 5374f95 commit a6451b9
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 51 deletions.
15 changes: 9 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fs::File, io::{BufReader, Read}};
use std::{
fs::File,
io::{BufReader, Read},
};

use clap::Parser;
use liblykia::protocol::connection::{CommunicationError, Connection, Message, Request};
Expand Down Expand Up @@ -38,15 +41,14 @@ fn run_repl() {
}*/
}


pub struct ClientSession {
conn: Connection,
}

impl ClientSession {
pub fn new(stream: TcpStream) -> Self {
ClientSession {
conn: Connection::new(stream)
conn: Connection::new(stream),
}
}

Expand All @@ -65,7 +67,6 @@ async fn run_file(filename: &str, print_ast: bool) {
let file = File::open(filename).expect("File couldn't be opened.");

let mut content: String = String::new();


BufReader::new(file)
.read_to_string(&mut content)
Expand All @@ -77,8 +78,10 @@ async fn run_file(filename: &str, print_ast: bool) {
// perform redis protocol frame parsing.
let mut session = ClientSession::new(socket);

session.send(Message::Request(Request::Run(content))).await.unwrap();

session
.send(Message::Request(Request::Run(content)))
.await
.unwrap();
}

#[tokio::main]
Expand Down
2 changes: 1 addition & 1 deletion liblykia/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod protocol;
pub mod protocol;
15 changes: 10 additions & 5 deletions liblykia/src/protocol/connection.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use bson::Bson;
use bytes::BytesMut;
use serde::{Deserialize, Serialize};
use tokio::{io::{copy, AsyncReadExt, AsyncWriteExt, BufWriter}, net::TcpStream};
use tokio::{
io::{copy, AsyncReadExt, AsyncWriteExt, BufWriter},
net::TcpStream,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Request {
Run(String)
Run(String),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Response {
Value(Bson)
Value(Bson),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Message {
Request(Request),
Response(Response)
Response(Response),
}

#[derive(Debug)]
Expand Down Expand Up @@ -62,7 +65,9 @@ impl Connection {
if self.read_buffer.is_empty() {
return Ok(None);
} else {
return Err(CommunicationError::GenericError("Connection reset by peer".to_owned()));
return Err(CommunicationError::GenericError(
"Connection reset by peer".to_owned(),
));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion liblykia/src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod connection;
pub mod connection;
2 changes: 1 addition & 1 deletion server/benches/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lykiadb_server::runtime::{RuntimeMode, Runtime};
use lykiadb_server::runtime::{Runtime, RuntimeMode};

fn runtime(filename: &str) {
let file = File::open(filename).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion server/src/lang/tokens/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::lang::tokens::token::*;
use crate::lang::Literal::*;
use crate::sym;
use std::iter::{Enumerate, Peekable};
use std::sync::Arc;
use std::str::Chars;
use std::sync::Arc;

pub struct Scanner<'a> {
chars: Peekable<Enumerate<Chars<'a>>>,
Expand Down
26 changes: 12 additions & 14 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio_stream::wrappers::TcpListenerStream;
use tokio_stream::StreamExt as _;

struct Server {
listener: Option<TcpListener>
listener: Option<TcpListener>,
}

pub struct ServerSession {
Expand All @@ -35,10 +35,15 @@ impl ServerSession {
let bson_response = bson::to_bson(&execution.ok().or_else(|| Some(RV::Undefined)));
self.conn.write(Message::Response(Response::Value(bson_response.unwrap()))).await;
}*/
self.conn.write(Message::Response(Response::Value(bson::to_bson(&1515).unwrap()))).await.unwrap();
},
self.conn
.write(Message::Response(Response::Value(
bson::to_bson(&1515).unwrap(),
)))
.await
.unwrap();
}
},
_ => panic!("Unsupported message type")
_ => panic!("Unsupported message type"),
}
}
}
Expand All @@ -50,9 +55,7 @@ impl ServerSession {

impl Server {
pub fn new() -> Result<Self, Error> {
Ok(Server {
listener: None,
})
Ok(Server { listener: None })
}

pub async fn listen(mut self, addr: &str) -> Result<Self, Error> {
Expand All @@ -77,13 +80,8 @@ impl Server {
}
Ok(())
}

}
#[tokio::main]
async fn main() -> Result<(), Error> {
Server::new()?
.listen("0.0.0.0:19191")
.await?
.serve()
.await
}
Server::new()?.listen("0.0.0.0:19191").await?.serve().await
}
24 changes: 15 additions & 9 deletions server/src/runtime/environment.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::runtime::interpreter::HaltReason;
use crate::runtime::types::RV;
use core::panic;
use std::borrow::{Borrow, BorrowMut};
use rustc_hash::FxHashMap;
use std::borrow::{Borrow, BorrowMut};

use super::interpreter::InterpretError;

Expand All @@ -23,9 +23,7 @@ pub struct EnvironmentArena {

impl EnvironmentArena {
pub fn new() -> Self {
let mut arena = EnvironmentArena {
envs: vec![]
};
let mut arena = EnvironmentArena { envs: vec![] };
arena.push(None);
arena
}
Expand Down Expand Up @@ -76,9 +74,15 @@ impl EnvironmentArena {
let ancestor = self.ancestor(env_id, distance);

if let Some(unwrapped) = ancestor {
self.envs[unwrapped.0].borrow_mut().map.insert(name.to_string(), value);
self.envs[unwrapped.0]
.borrow_mut()
.map
.insert(name.to_string(), value);
} else {
self.envs[env_id.0].borrow_mut().map.insert(name.to_string(), value);
self.envs[env_id.0]
.borrow_mut()
.map
.insert(name.to_string(), value);
}

Ok(true)
Expand Down Expand Up @@ -128,7 +132,6 @@ impl EnvironmentArena {
}
}


#[cfg(test)]
mod test {
use crate::runtime::types::RV;
Expand Down Expand Up @@ -156,7 +159,8 @@ mod test {
let parent = env_arena.top();
env_arena.declare(parent, "five".to_string(), RV::Num(5.0));
let child = env_arena.push(Some(parent));
env_arena.assign(child, "five".to_string(), RV::Num(5.1))
env_arena
.assign(child, "five".to_string(), RV::Num(5.1))
.unwrap();
assert_eq!(env_arena.read(parent, "five").unwrap(), RV::Num(5.1));
assert_eq!(env_arena.read(child, "five").unwrap(), RV::Num(5.1));
Expand All @@ -173,6 +177,8 @@ mod test {
fn test_assign_to_undefined_variable() {
let mut env_arena = super::EnvironmentArena::new();
let env = env_arena.top();
assert!(env_arena.assign(env, "five".to_string(), RV::Num(5.0)).is_err());
assert!(env_arena
.assign(env, "five".to_string(), RV::Num(5.0))
.is_err());
}
}
14 changes: 8 additions & 6 deletions server/src/runtime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Interpreter {
statements: &Vec<StmtId>,
closure: EnvId,
parameters: &Vec<String>,
arguments: &[RV]
arguments: &[RV],
) -> Result<RV, HaltReason> {
let fn_env = self.env_arena.push(Some(closure));

Expand Down Expand Up @@ -293,10 +293,10 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
let evaluated = self.visit_expr(*expr)?;
let result = if let Some(distance_unv) = distance {
self.env_arena
.assign_at( self.env, distance_unv, &dst.name, evaluated.clone())
.assign_at(self.env, distance_unv, &dst.name, evaluated.clone())
} else {
self.env_arena
.assign( self.env, dst.name.clone(), evaluated.clone())
.assign(self.env, dst.name.clone(), evaluated.clone())
};
if result.is_err() {
return Err(result.err().unwrap());
Expand Down Expand Up @@ -374,8 +374,11 @@ impl VisitorMut<RV, HaltReason> for Interpreter {

if name.is_some() {
// TODO(vck): Callable shouldn't be cloned here
self.env_arena
.declare(self.env, name.as_ref().unwrap().name.to_string(), callable.clone());
self.env_arena.declare(
self.env,
name.as_ref().unwrap().name.to_string(),
callable.clone(),
);
}

Ok(callable)
Expand Down Expand Up @@ -513,7 +516,6 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
}
}


#[derive(Clone)]
pub struct Output {
out: Vec<RV>,
Expand Down
2 changes: 1 addition & 1 deletion server/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod types;

pub struct Runtime {
mode: RuntimeMode,
out: Option<Shared<Output>>
out: Option<Shared<Output>>,
}

#[derive(Eq, PartialEq)]
Expand Down
5 changes: 4 additions & 1 deletion server/src/runtime/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use self::{
time::nt_clock,
};

use super::{interpreter::Output, types::{Function, RV}};
use super::{
interpreter::Output,
types::{Function, RV},
};

pub mod fib;
pub mod json;
Expand Down
4 changes: 1 addition & 3 deletions server/src/runtime/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ impl Function {
parameters,
closure,
body,
} => {
interpreter.user_fn_call(body, *closure, parameters, arguments)
}
} => interpreter.user_fn_call(body, *closure, parameters, arguments),
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions server/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{cell::RefCell, sync::Arc};



pub type Shared<T> = Arc<RefCell<T>>;

pub fn alloc_shared<T>(obj: T) -> Shared<T> {
Expand Down

0 comments on commit a6451b9

Please sign in to comment.