cargo format

This commit is contained in:
Yuriy Dupyn 2024-01-28 22:40:41 +01:00
parent 4d45da0cd1
commit 845db102c2
33 changed files with 885 additions and 530 deletions

View file

@ -24,10 +24,10 @@ use crate::config::Configuration;
use crate::persistence::state_to_file;
use crate::proto_wrapper::{CompleteStatus, ServerProto};
mod config;
mod proto_wrapper;
mod cancellation;
mod config;
mod persistence;
mod proto_wrapper;
type TokenStore = Arc<Mutex<HashMap<(i32, i32), ResetCancelToken>>>;
type SharedDbState = Arc<RwLock<State>>;
@ -65,16 +65,17 @@ async fn get_state(config: &Configuration) -> anyhow::Result<State> {
println!("WARNING: No DB state file found, creating new one");
Ok(State::new())
}
Err(e) => {
Err(e)?
}
Ok(state) => {
Ok(state)
}
Err(e) => Err(e)?,
Ok(state) => Ok(state),
}
}
async fn handle_stream(mut stream: TcpStream, state: SharedDbState, tokens: TokenStore, config: Arc<Configuration>) -> anyhow::Result<()> {
async fn handle_stream(
mut stream: TcpStream,
state: SharedDbState,
tokens: TokenStore,
config: Arc<Configuration>,
) -> anyhow::Result<()> {
let (reader, writer) = stream.split();
let mut writer = ProtoWriter::new(BufWriter::new(writer));
let mut reader = ProtoReader::new(BufReader::new(reader), 1024);
@ -88,7 +89,9 @@ async fn handle_stream(mut stream: TcpStream, state: SharedDbState, tokens: Toke
let result = match request {
Ok(req) => handle_connection(&mut reader, &mut writer, req, state, token, config).await,
Err(ServerHandshakeError::IsCancelRequest(cancel)) => handle_cancellation(cancel.pid, cancel.secret, &tokens).await,
Err(ServerHandshakeError::IsCancelRequest(cancel)) => {
handle_cancellation(cancel.pid, cancel.secret, &tokens).await
}
Err(e) => Err(anyhow::anyhow!("Error during handshake: {:?}", e)),
};
@ -134,10 +137,17 @@ async fn handle_cancellation(pid: i32, key: i32, tokens: &TokenStore) -> anyhow:
Ok(())
}
async fn handle_connection<R, W>(reader: &mut R, writer: &mut W, request: HandshakeRequest, state: SharedDbState, token: ResetCancelToken, config: Arc<Configuration>) -> anyhow::Result<()>
where
R: FrontendProtoReader + Send,
W: BackendProtoWriter + ProtoFlush + Send,
async fn handle_connection<R, W>(
reader: &mut R,
writer: &mut W,
request: HandshakeRequest,
state: SharedDbState,
token: ResetCancelToken,
config: Arc<Configuration>,
) -> anyhow::Result<()>
where
R: FrontendProtoReader + Send,
W: BackendProtoWriter + ProtoFlush + Send,
{
println!("Client connected: {:?}", request);
@ -152,9 +162,7 @@ async fn handle_connection<R, W>(reader: &mut R, writer: &mut W, request: Handsh
let result = handle_query(writer, &state, data.query.into(), &token, &config).await;
match result {
Ok(_) => {}
Err(e) => {
writer.write_error_message(&e.to_string()).await?
}
Err(e) => writer.write_error_message(&e.to_string()).await?,
}
writer.write_ready_for_query().await?;
}
@ -165,9 +173,15 @@ async fn handle_connection<R, W>(reader: &mut R, writer: &mut W, request: Handsh
Ok(())
}
async fn handle_query<W>(writer: &mut W, state: &SharedDbState, query: String, token: &ResetCancelToken, config: &Arc<Configuration>) -> anyhow::Result<()>
where
W: BackendProtoWriter + ProtoFlush + Send,
async fn handle_query<W>(
writer: &mut W,
state: &SharedDbState,
query: String,
token: &ResetCancelToken,
config: &Arc<Configuration>,
) -> anyhow::Result<()>
where
W: BackendProtoWriter + ProtoFlush + Send,
{
// Make sure token is reset before next query
token.reset();
@ -184,11 +198,15 @@ async fn handle_query<W>(writer: &mut W, state: &SharedDbState, query: String, t
match response {
Response::Deleted(i) => {
writer.write_command_complete(CompleteStatus::Delete(i)).await?;
writer
.write_command_complete(CompleteStatus::Delete(i))
.await?;
true
}
Response::Inserted => {
writer.write_command_complete(CompleteStatus::Insert { oid: 0, rows: 1 }).await?;
writer
.write_command_complete(CompleteStatus::Insert { oid: 0, rows: 1 })
.await?;
true
}
Response::Selected(schema, columns, mut rows) => {
@ -207,22 +225,30 @@ async fn handle_query<W>(writer: &mut W, state: &SharedDbState, query: String, t
}
}
writer.write_command_complete(CompleteStatus::Select(sent_rows)).await?;
writer
.write_command_complete(CompleteStatus::Select(sent_rows))
.await?;
}
_ => {
writer.write_command_complete(CompleteStatus::Select(0)).await?;
writer
.write_command_complete(CompleteStatus::Select(0))
.await?;
}
}
false
},
}
Response::TableCreated => {
writer.write_command_complete(CompleteStatus::CreateTable).await?;
writer
.write_command_complete(CompleteStatus::CreateTable)
.await?;
true
},
}
Response::IndexCreated => {
writer.write_command_complete(CompleteStatus::CreateIndex).await?;
writer
.write_command_complete(CompleteStatus::CreateIndex)
.await?;
true
},
}
}
};