Skip to content

Commit

Permalink
rust: Add has_changes for Changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
YannikSc committed Jul 11, 2024
1 parent 0c2caf7 commit 3deef29
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions rust/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ impl Server {
if let AttributeChange::Multi { add, .. } = entry {
add.push(value);
}

Ok(self)
}

Expand Down
24 changes: 24 additions & 0 deletions rust/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ pub trait IntoAttributeValue {
fn into_attribute_value(self) -> AttributeValue;
}

impl Changeset {
pub fn has_changes(&self) -> bool {
self.attributes
.iter()
.filter(|(_, change)| {
if let AttributeChange::Multi { add, remove } = change {
return add.len() + remove.len() > 0;
}

if let AttributeChange::Update { new, old } = change {
return new.ne(old);
}

true
})
.find(|_| true)
.is_some()
}
}

impl Commit {
pub fn new() -> Self {
Self {
Expand All @@ -69,6 +89,10 @@ impl Commit {
return new.ne(old);
}

if let AttributeChange::Multi { add, remove } = change {
return add.len() + remove.len() > 0;
}

true
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion rust/src/new_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl NewObject {
return true;
}

self.server.has_changes()
self.server.has_changes() || self.deferred_changes.has_changes()
}

///
Expand Down
16 changes: 3 additions & 13 deletions rust/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use crate::api::{commit_changes, CommitResponse, query_objects, QueryResponse, Server};
use crate::api::{commit_changes, query_objects, CommitResponse, QueryResponse, Server};
use crate::commit::{AttributeChange, Changeset, Commit};
use crate::filter::{AttributeFilter, IntoFilterValue};

Expand Down Expand Up @@ -58,7 +58,7 @@ impl QueryBuilder {
self
}

pub fn restrict<S: ToString, I: IntoIterator<Item=S>>(mut self, attributes: I) -> Self {
pub fn restrict<S: ToString, I: IntoIterator<Item = S>>(mut self, attributes: I) -> Self {
self.0.restrict = HashSet::from_iter(attributes.into_iter().map(|v| v.to_string()));

self
Expand Down Expand Up @@ -90,16 +90,6 @@ impl Server {
}

pub fn has_changes(&self) -> bool {
self.changes
.attributes
.iter()
.filter(|(_, change)| {
if let AttributeChange::Update { new, old } = change {
return new.ne(old);
}

true
})
.find(|_| true).is_some()
self.changes.has_changes()
}
}

0 comments on commit 3deef29

Please sign in to comment.