use crate::document::DocumentViewId;
use crate::entry::EntrySigned;
use crate::identity::Author;
use crate::operation::{
Operation, OperationAction, OperationEncoded, OperationFields, OperationId, OperationVersion,
};
use crate::schema::SchemaId;
use crate::Validate;
pub trait AsOperation {
fn action(&self) -> OperationAction;
fn schema(&self) -> SchemaId;
fn version(&self) -> OperationVersion;
fn fields(&self) -> Option<OperationFields>;
fn previous_operations(&self) -> Option<DocumentViewId>;
fn has_fields(&self) -> bool {
self.fields().is_some()
}
fn has_previous_operations(&self) -> bool {
self.previous_operations().is_some()
}
fn is_create(&self) -> bool {
self.action() == OperationAction::Create
}
fn is_update(&self) -> bool {
self.action() == OperationAction::Update
}
fn is_delete(&self) -> bool {
self.action() == OperationAction::Delete
}
}
pub trait AsVerifiedOperation:
Sized + Clone + Send + Sync + Validate + PartialEq + std::fmt::Debug
{
type VerifiedOperationError: 'static + std::error::Error + Send + Sync;
fn new(
public_key: &Author,
operation_id: &OperationId,
operation: &Operation,
) -> Result<Self, Self::VerifiedOperationError>;
fn new_from_entry(
entry_encoded: &EntrySigned,
operation_encoded: &OperationEncoded,
) -> Result<Self, Self::VerifiedOperationError>;
fn operation_id(&self) -> &OperationId;
fn public_key(&self) -> &Author;
fn operation(&self) -> &Operation;
}
impl<T: AsVerifiedOperation> AsOperation for T {
fn action(&self) -> OperationAction {
self.operation().action()
}
fn schema(&self) -> SchemaId {
self.operation().schema()
}
fn version(&self) -> OperationVersion {
self.operation().version()
}
fn fields(&self) -> Option<OperationFields> {
self.operation().fields()
}
fn previous_operations(&self) -> Option<DocumentViewId> {
self.operation().previous_operations()
}
}