Skip to main content

Cache

Struct Cache 

Source
pub struct Cache {
Show 14 fields pub sync_token: String, pub full_sync_date_utc: Option<DateTime<Utc>>, pub last_sync: Option<DateTime<Utc>>, pub items: Vec<Item>, pub projects: Vec<Project>, pub labels: Vec<Label>, pub sections: Vec<Section>, pub notes: Vec<Note>, pub project_notes: Vec<ProjectNote>, pub reminders: Vec<Reminder>, pub filters: Vec<Filter>, pub collaborators: Vec<Collaborator>, pub collaborator_states: Vec<CollaboratorState>, pub user: Option<User>, /* private fields */
}
Expand description

Local cache for Todoist data.

The cache structure mirrors the Sync API response for easy updates from sync operations. It stores all relevant resources and metadata about the last sync.

§Thread Safety

Cache is Send and Sync, but it has no internal synchronization. Concurrent reads are safe, but concurrent writes or read-modify-write patterns require external synchronization.

For multi-threaded access, wrap in Arc<RwLock<Cache>>:

use std::sync::{Arc, RwLock};
use todoist_cache_rs::Cache;

let cache = Arc::new(RwLock::new(Cache::new()));

// Read access
let items_count = cache.read().unwrap().items.len();

// Write access
cache.write().unwrap().sync_token = "new_token".to_string();

In typical CLI usage, the cache is owned by a single-threaded runtime and external synchronization is not needed.

Fields§

§sync_token: String

The sync token for incremental syncs. Use “*” for a full sync or the stored token for incremental updates.

§full_sync_date_utc: Option<DateTime<Utc>>

UTC timestamp when the last full sync was performed. This is set when a full sync completes successfully.

§last_sync: Option<DateTime<Utc>>

UTC timestamp of the last successful sync (full or incremental).

§items: Vec<Item>

Cached tasks (called “items” in the Sync API).

§projects: Vec<Project>

Cached projects.

§labels: Vec<Label>

Cached personal labels.

§sections: Vec<Section>

Cached sections.

§notes: Vec<Note>

Cached task comments (called “notes” in the Sync API).

§project_notes: Vec<ProjectNote>

Cached project comments.

§reminders: Vec<Reminder>

Cached reminders.

§filters: Vec<Filter>

Cached saved filters.

§collaborators: Vec<Collaborator>

Cached collaborators for shared projects.

§collaborator_states: Vec<CollaboratorState>

Cached collaborator membership states by project.

§user: Option<User>

Cached user information.

Implementations§

Source§

impl Cache

Source

pub fn new() -> Self

Creates a new empty cache with sync_token set to “*” for initial full sync.

Source

pub fn with_data( sync_token: String, full_sync_date_utc: Option<DateTime<Utc>>, last_sync: Option<DateTime<Utc>>, items: Vec<Item>, projects: Vec<Project>, labels: Vec<Label>, sections: Vec<Section>, notes: Vec<Note>, project_notes: Vec<ProjectNote>, reminders: Vec<Reminder>, filters: Vec<Filter>, user: Option<User>, ) -> Self

Creates a new cache with provided data and rebuilds indexes.

This is primarily useful for testing. The indexes are automatically rebuilt after construction.

Source

pub fn rebuild_indexes(&mut self)

Rebuilds all lookup indexes from current cache data.

This is called automatically after applying sync responses and should be called after loading the cache from disk.

Source

pub fn find_project(&self, name_or_id: &str) -> Option<&Project>

Find a project by ID or name (case-insensitive). O(1) lookup.

Source

pub fn find_section( &self, name_or_id: &str, project_id: Option<&str>, ) -> Option<&Section>

Find a section by ID or name (case-insensitive) within a project. O(1) lookup.

If project_id is provided, returns the section only if it belongs to that project. If project_id is None and there’s exactly one match, returns it.

Source

pub fn find_label(&self, name_or_id: &str) -> Option<&Label>

Find a label by ID or name (case-insensitive). O(1) lookup.

Source

pub fn find_item(&self, id: &str) -> Option<&Item>

Find an item by ID. O(1) lookup.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache has never been synced (sync_token is “*”).

Source

pub fn needs_full_sync(&self) -> bool

Returns true if the cache requires a full sync. This is true when the sync_token is “*”.

Source

pub fn apply_sync_response(&mut self, response: &SyncResponse)

Applies a sync response to the cache, merging in changes.

This method handles both full and incremental sync responses:

  • Updates the sync token and timestamps
  • For full sync: replaces all resources with the response data
  • For incremental sync: merges changes (add/update/delete by ID)

Resources with is_deleted: true are removed from the cache.

§Arguments
  • response - The sync response from the Todoist API
Source

pub fn apply_mutation_response(&mut self, response: &SyncResponse)

Applies a mutation response to the cache.

This method is similar to apply_sync_response() but is specifically designed for write operation (mutation) responses. It:

  • Updates the sync_token from the response
  • Updates the last_sync timestamp
  • Merges any resources returned in the response (add/update/delete by ID)

Unlike full sync responses, mutation responses always use incremental merge logic since they only contain affected resources.

Note: The temp_id_mapping from the response should be used by the caller to resolve temporary IDs before calling this method, or the caller can use the returned response’s temp_id_mapping to look up real IDs.

§Arguments
  • response - The sync response from a mutation (write) operation

Trait Implementations§

Source§

impl Clone for Cache

Source§

fn clone(&self) -> Cache

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 Debug for Cache

Source§

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

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

impl Default for Cache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Cache

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Cache

Source§

fn eq(&self, other: &Cache) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Cache

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Cache

Auto Trait Implementations§

§

impl Freeze for Cache

§

impl RefUnwindSafe for Cache

§

impl Send for Cache

§

impl Sync for Cache

§

impl Unpin for Cache

§

impl UnsafeUnpin for Cache

§

impl UnwindSafe for Cache

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<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,