use std::fmt;
use std::str::FromStr;
use crate::error::Error;
use crate::types::{TargetType, ValueType};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Operation {
Set,
Remove,
Push,
Pop,
ListRemove,
SetAdd,
SetRemove,
}
impl fmt::Display for Operation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Operation {
pub fn as_str(&self) -> &str {
match self {
Operation::Set => "set",
Operation::Remove => "rm",
Operation::Push => "push",
Operation::Pop => "pop",
Operation::ListRemove => "list_rm",
Operation::SetAdd => "set_add",
Operation::SetRemove => "set_rm",
}
}
}
impl FromStr for Operation {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"set" => Ok(Operation::Set),
"rm" => Ok(Operation::Remove),
"push" => Ok(Operation::Push),
"pop" => Ok(Operation::Pop),
"list_rm" | "list:rm" => Ok(Operation::ListRemove),
"set_add" | "set:add" => Ok(Operation::SetAdd),
"set_rm" | "set:rm" => Ok(Operation::SetRemove),
_ => Err(Error::Other(format!("unknown operation: {s}"))),
}
}
}
#[cfg(test)]
mod tests {
use super::Operation;
#[test]
fn parses_canonical_operations() {
assert_eq!(
"list_rm".parse::<Operation>().unwrap(),
Operation::ListRemove
);
assert_eq!("set_add".parse::<Operation>().unwrap(), Operation::SetAdd);
assert_eq!("set_rm".parse::<Operation>().unwrap(), Operation::SetRemove);
}
#[test]
fn parses_legacy_cli_style_operations() {
assert_eq!(
"list:rm".parse::<Operation>().unwrap(),
Operation::ListRemove
);
assert_eq!("set:add".parse::<Operation>().unwrap(), Operation::SetAdd);
assert_eq!("set:rm".parse::<Operation>().unwrap(), Operation::SetRemove);
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetadataValue {
pub value: String,
pub value_type: ValueType,
pub is_git_ref: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetadataEntry {
pub key: String,
pub value: String,
pub value_type: ValueType,
pub is_git_ref: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetadataRecord {
pub target_value: String,
pub key: String,
pub value: String,
pub value_type: ValueType,
pub is_git_ref: bool,
pub is_promised: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Authorship {
pub email: String,
pub timestamp: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TombstoneRecord {
pub target_type: TargetType,
pub target_value: String,
pub key: String,
pub timestamp: i64,
pub email: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetTombstoneRecord {
pub target_type: TargetType,
pub target_value: String,
pub key: String,
pub member_id: String,
pub value: String,
pub timestamp: i64,
pub email: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListTombstoneRecord {
pub target_type: TargetType,
pub target_value: String,
pub key: String,
pub entry_name: String,
pub timestamp: i64,
pub email: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModifiedEntry {
pub target_type: TargetType,
pub target_value: String,
pub key: String,
pub operation: Operation,
pub value: String,
pub value_type: Option<ValueType>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SerializableEntry {
pub target_type: TargetType,
pub target_value: String,
pub key: String,
pub value: String,
pub value_type: ValueType,
pub last_timestamp: i64,
pub is_git_ref: bool,
}