Skip to main content

TrackedResource

Struct TrackedResource 

Source
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 implement Serialize, DeserializeOwned, and Clone for 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>

Source

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 dirty
Source

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 clean
Source

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());
Source

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()); // Unchanged
Source

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());
Source

pub const fn inner(&self) -> &T

Returns a reference to the inner resource.

In most cases, you can use Deref coercion instead.

Source

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.

Source

pub fn into_inner(self) -> T

Consumes the wrapper and returns the inner resource.

Source

pub const fn is_new(&self) -> bool

Returns true if this is a new resource (no original state).

Trait Implementations§

Source§

impl<T: Clone> Clone for TrackedResource<T>

Source§

fn clone(&self) -> TrackedResource<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for TrackedResource<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for TrackedResource<T>

Provides transparent read access to the inner resource.

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> DerefMut for TrackedResource<T>

Provides transparent mutable access to the inner resource.

Modifications via DerefMut will be detected by is_dirty().

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for TrackedResource<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for TrackedResource<T>
where T: RefUnwindSafe,

§

impl<T> Send for TrackedResource<T>
where T: Send,

§

impl<T> Sync for TrackedResource<T>
where T: Sync,

§

impl<T> Unpin for TrackedResource<T>
where T: Unpin,

§

impl<T> UnwindSafe for TrackedResource<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Formattable for T
where T: Deref, <T as Deref>::Target: Formattable,

Source§

impl<T> Parsable for T
where T: Deref, <T as Deref>::Target: Parsable,