Skip to content

Commit

Permalink
feat: read everything until terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Nov 25, 2024
1 parent 9a711e9 commit 2b22449
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/git_lfs/custom_transfer_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use super::git_lfs_parser::Event;

pub trait CustomTransferAgent {
async fn init(&mut self, event: &Event) -> Result<()>;
async fn terminate(&mut self) -> Result<()>;
}
65 changes: 54 additions & 11 deletions src/git_lfs/git_lfs_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,33 @@ use serde::{Deserialize, Serialize};

use super::CustomTransferAgent;

pub fn error(code: u32, message: &str) -> Result<()> {
let error_json = ErrorJson {
error: ErrorJsonInner {
code,
message: message.to_string()
}
};

println!("{}", serde_json::to_string(&error_json)?);

Ok(())
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct EventJson {
event: String,
operation: String,
remote: String,
concurrent: bool
struct EventJsonPartial {
event: String
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct ErrorJson {
error: ErrorJsonInner
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct ErrorJsonInner {
code: u32,
message: String
}

pub struct Event {
Expand All @@ -21,7 +42,8 @@ pub struct Event {
}

pub enum EventType {
Init
Init,
Terminate
}

// pub enum EventOperation {
Expand All @@ -41,9 +63,10 @@ impl<'custom_transfer_agent, T: CustomTransferAgent> GitLfsParser<'custom_transf
}
}

fn parse(&self, event: &EventJson) -> Result<Event> {
fn parse(&self, event: &EventJsonPartial) -> Result<Event> {
let event_type = match event.event.as_str() {
"init" => EventType::Init,
"terminate" => EventType::Terminate,
_ => bail!("Event type was \"{}\". Value unexpected.", event.event)
};

Expand All @@ -66,13 +89,33 @@ impl<'custom_transfer_agent, T: CustomTransferAgent> GitLfsParser<'custom_transf
let stdin = io::stdin();

stdin.read_line(&mut buffer)?;
let event = self.parse(&serde_json::from_str::<EventJson>(buffer.as_str())?)?;
let event = self.parse(&serde_json::from_str::<EventJsonPartial>(buffer.as_str())?)?;

let init_result = match event.event {
EventType::Init => self.custom_transfer_agent.init(&event).await,
_ => bail!("Event type was not init.")
};

match event.event {
EventType::Init => self.custom_transfer_agent.init(&event).await?
match init_result {
Ok(_) => println!("{{ }}"), // success
Err(err) => error(1, err.to_string().as_str())? // an error occurred
}

// todo parse from stdin in a loop.
loop {
stdin.read_line(&mut buffer)?;
let event = self.parse(&serde_json::from_str::<EventJsonPartial>(buffer.as_str())?)?;

match event.event {
// TODO handle other event types

EventType::Terminate => {
self.custom_transfer_agent.terminate().await?;

break;
},
_ => bail!("Event type not supported in context.")
}
}

Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions src/subcommands/main_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ impl CustomTransferAgent for MainSubcommand {

Ok(())
}

async fn terminate(&mut self) -> Result<()> {
// No cleanup to do.

Ok(())
}
}

impl Subcommand for MainSubcommand {
Expand Down

0 comments on commit 2b22449

Please sign in to comment.