Skip to content

Commit

Permalink
feat: pass event object into init
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Nov 25, 2024
1 parent 94548a6 commit 02d0aec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/git_lfs/custom_transfer_agent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Result;

use super::git_lfs_parser::Event;

pub trait CustomTransferAgent {
async fn init(&mut self) -> Result<()>;
async fn init(&mut self, event: &Event) -> Result<()>;
}
52 changes: 48 additions & 4 deletions src/git_lfs/git_lfs_parser.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
use anyhow::Result;
use std::io;

use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};

use super::CustomTransferAgent;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde[rename_all = "snake_case"]]
struct Event {
struct EventJson {
event: String,
operation: String,
remote: String,
concurrent: bool
}

pub struct Event {
event: EventType,
operation: EventOperation,
remote: String,
concurrent: bool
}

pub enum EventType {
Init
}

pub enum EventOperation {
Download,
Upload
}

#[derive(Debug)]
pub struct GitLfsParser<'custom_transfer_agent, T: CustomTransferAgent> {
custom_transfer_agent: &'custom_transfer_agent mut T
Expand All @@ -24,10 +42,36 @@ impl<'custom_transfer_agent, T: CustomTransferAgent> GitLfsParser<'custom_transf
}
}

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

let event_operation = match event.operation.as_str() {
"download" => EventOperation::Download,
"upload" => EventOperation::Upload,
_ => bail!("Event operation was \"{}\". Expected either \"download\" or \"upload\".", event.operation)
};

Ok(Event {
event: event_type,
operation: event_operation,
remote: event.remote.clone(),
concurrent: event.concurrent
})
}

pub async fn listen(&mut self) -> Result<()> {
// todo parse from stdin in a loop.
let mut buffer = String::new();
let stdin = io::stdin();

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

self.custom_transfer_agent.init().await?;
self.custom_transfer_agent.init(&event).await?;

// todo parse from stdin in a loop.

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/git_lfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod custom_transfer_agent;
mod git_lfs_parser;

pub use custom_transfer_agent::CustomTransferAgent;
pub use git_lfs_parser::GitLfsParser;
pub use git_lfs_parser::{Event, GitLfsParser};
4 changes: 2 additions & 2 deletions src/subcommands/main_subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use clap::ArgMatches;

use crate::{credential_manager::CredentialManager, git_lfs::{CustomTransferAgent, GitLfsParser}};
use crate::{credential_manager::CredentialManager, git_lfs::{CustomTransferAgent, Event, GitLfsParser}};

use super::Subcommand;

Expand All @@ -11,7 +11,7 @@ pub struct MainSubcommand {
}

impl CustomTransferAgent for MainSubcommand {
async fn init(&mut self) -> Result<()> {
async fn init(&mut self, _: &Event) -> Result<()> {
self.credential_manager = Some(CredentialManager::new()?);
// Init synology api

Expand Down

0 comments on commit 02d0aec

Please sign in to comment.