pub struct TrackedResource<T> { /* private fields */ }Expand description
A wrapper that tracks changes to a resource.
TrackedResource<T> stores both the current resource data and its
original state (as JSON). This allows detecting which fields have
changed since the resource was loaded or last saved.
§Type Parameters
T- The resource type. Must implementSerialize,DeserializeOwned, andClonefor state tracking to work.
§Deref Pattern
Implements Deref<Target = T> and DerefMut, so you can access
and modify the resource transparently:
use shopify_sdk::rest::TrackedResource;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Product { title: String }
let mut tracked = TrackedResource::new(Product { title: "Test".to_string() });
// Read via Deref
println!("{}", tracked.title);
// Write via DerefMut
tracked.title = "Modified".to_string();Implementations§
Source§impl<T: Serialize + DeserializeOwned + Clone> TrackedResource<T>
impl<T: Serialize + DeserializeOwned + Clone> TrackedResource<T>
Sourcepub const fn new(resource: T) -> Self
pub const fn new(resource: T) -> Self
Creates a new tracked resource for a resource that doesn’t exist yet.
New resources have no original state, so is_dirty() returns true
and changed_fields() returns all fields.
§Example
use shopify_sdk::rest::TrackedResource;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Product { title: String }
let tracked = TrackedResource::new(Product { title: "New".to_string() });
assert!(tracked.is_dirty()); // New resources are always dirtySourcepub fn from_existing(resource: T) -> Self
pub fn from_existing(resource: T) -> Self
Creates a tracked resource from an existing resource.
The current state is captured as the original state, so is_dirty()
returns false until the resource is modified.
§Example
use shopify_sdk::rest::TrackedResource;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Product { title: String }
let tracked = TrackedResource::from_existing(Product { title: "Loaded".to_string() });
assert!(!tracked.is_dirty()); // Existing resources start cleanSourcepub fn is_dirty(&self) -> bool
pub fn is_dirty(&self) -> bool
Returns true if the resource has been modified since loading or last save.
For new resources (no original state), always returns true.
§Example
use shopify_sdk::rest::TrackedResource;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Product { title: String }
let mut tracked = TrackedResource::from_existing(Product { title: "Test".to_string() });
assert!(!tracked.is_dirty());
tracked.title = "Changed".to_string();
assert!(tracked.is_dirty());Sourcepub fn changed_fields(&self) -> Value
pub fn changed_fields(&self) -> Value
Returns only the fields that have changed since loading or last save.
For new resources, returns all fields (since there’s no original state to compare against).
For existing resources, returns only the fields whose values differ from the original state. Nested objects are handled recursively.
§Example
use shopify_sdk::rest::TrackedResource;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Product { title: String, vendor: String }
let mut tracked = TrackedResource::from_existing(Product {
title: "Original".to_string(),
vendor: "Vendor".to_string(),
});
tracked.title = "Changed".to_string();
let changes = tracked.changed_fields();
assert!(changes.get("title").is_some());
assert!(changes.get("vendor").is_none()); // UnchangedSourcepub fn mark_clean(&mut self)
pub fn mark_clean(&mut self)
Marks the resource as clean by capturing the current state as original.
Call this after a successful save operation to reset dirty tracking.
§Example
use shopify_sdk::rest::TrackedResource;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Product { title: String }
let mut tracked = TrackedResource::from_existing(Product { title: "Test".to_string() });
tracked.title = "Changed".to_string();
assert!(tracked.is_dirty());
tracked.mark_clean();
assert!(!tracked.is_dirty());Sourcepub const fn inner(&self) -> &T
pub const fn inner(&self) -> &T
Returns a reference to the inner resource.
In most cases, you can use Deref coercion instead.
Sourcepub fn inner_mut(&mut self) -> &mut T
pub fn inner_mut(&mut self) -> &mut T
Returns a mutable reference to the inner resource.
In most cases, you can use DerefMut coercion instead.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the wrapper and returns the inner resource.
Trait Implementations§
Source§impl<T: Clone> Clone for TrackedResource<T>
impl<T: Clone> Clone for TrackedResource<T>
Source§fn clone(&self) -> TrackedResource<T>
fn clone(&self) -> TrackedResource<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for TrackedResource<T>
impl<T: Debug> Debug for TrackedResource<T>
Source§impl<T> Deref for TrackedResource<T>
Provides transparent read access to the inner resource.
impl<T> Deref for TrackedResource<T>
Provides transparent read access to the inner resource.