Skip to content

Commit

Permalink
refact: unified serialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Jun 20, 2024
1 parent 6f27c53 commit e0074f1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 45 deletions.
26 changes: 24 additions & 2 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;

use anyhow::Result;
use colored::Colorize;

use model::{Client, Options};
use serialization::Invocation;
use state::State;
use task::Task;

Expand All @@ -12,6 +13,27 @@ mod serialization;
pub mod state;
pub mod task;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Invocation {
pub action: String,
pub attributes: Option<HashMap<String, String>>,
pub payload: Option<String>,
}

impl Invocation {
pub fn new(
action: String,
attributes: Option<HashMap<String, String>>,
payload: Option<String>,
) -> Self {
Self {
action,
attributes,
payload,
}
}
}

#[derive(Debug, Clone)]
pub struct AgentOptions {
pub max_iterations: usize,
Expand Down Expand Up @@ -123,7 +145,7 @@ impl Agent {
for inv in invocations {
// avoid running the same command twince in a row
if let Some(p) = prev.as_ref() {
if inv.is_same(p) {
if inv.eq(p) {
println!(".");
continue;
}
Expand Down
38 changes: 0 additions & 38 deletions src/agent/serialization/mod.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1 @@
use std::collections::HashMap;

use xml::serialize_invocation;

pub(crate) mod xml;

#[derive(Debug, Default, Clone)]
pub struct Invocation {
pub action: String,
pub attributes: Option<HashMap<String, String>>,
pub payload: Option<String>,

serialized: String,
}

impl Invocation {
pub fn new(
action: String,
attributes: Option<HashMap<String, String>>,
payload: Option<String>,
) -> Self {
let mut zelf = Self {
action,
attributes,
payload,
serialized: "".to_string(),
};
zelf.serialized = serialize_invocation(&zelf);
zelf
}

pub fn as_serialized_str(&self) -> &str {
return self.serialized.as_str();
}

pub fn is_same(&self, other: &Invocation) -> bool {
self.serialized == other.serialized
}
}
3 changes: 1 addition & 2 deletions src/agent/serialization/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use regex::Regex;
use crate::agent::{
namespaces::Action,
state::storage::{Storage, StorageType, CURRENT_TAG, PREVIOUS_TAG},
Invocation,
};

use super::Invocation;

lazy_static! {
pub static ref XML_ATTRIBUTES_PARSER: Regex = Regex::new(r#"(?m)(([^=]+)="([^"]+)")"#).unwrap();
}
Expand Down
6 changes: 4 additions & 2 deletions src/agent/state/history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;

use crate::agent::{model::Message, serialization::Invocation};
use crate::agent::{model::Message, Invocation};

#[derive(Debug, Clone, Default)]
pub struct Execution {
Expand Down Expand Up @@ -47,7 +47,9 @@ impl Execution {
if let Some(response) = self.response.as_ref() {
messages.push(Message::Agent(response.to_string()));
} else if let Some(invocation) = self.invocation.as_ref() {
messages.push(Message::Agent(invocation.as_serialized_str().to_string()));
messages.push(Message::Agent(
crate::agent::serialization::xml::serialize_invocation(invocation),
));
}

messages.push(Message::Feedback(if let Some(err) = &self.error {
Expand Down
3 changes: 2 additions & 1 deletion src/agent/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use colored::Colorize;
use super::{
model::Message,
namespaces::{self, Namespace},
serialization::{self, Invocation},
serialization,
task::Task,
Invocation,
};
use history::{Execution, History};
use storage::Storage;
Expand Down

0 comments on commit e0074f1

Please sign in to comment.