Struct alfred_rs::data::Data [−][src]
pub struct Data { /* fields omitted */ }
Workflow data that will be persisted to disk
Methods
impl Data
[src]
impl Data
pub fn load() -> Result<Self, Error>
[src]
pub fn load() -> Result<Self, Error>
Loads the workflow data or creates a new one.
Reads the content of workflow's standard data file WORKFLOW_UID-persistent-data.json
.
If the file is missing or corrupt a new (empty) data will be produced.
Errors:
This method can fail if any disk/IO error happens.
pub fn set<K, V>(&mut self, k: K, v: &V) -> Result<(), Error> where
K: Into<String>,
V: Serialize,
[src]
pub fn set<K, V>(&mut self, k: K, v: &V) -> Result<(), Error> where
K: Into<String>,
V: Serialize,
Set the value of key k
to v
and persist it to disk
k
is a type that implements Into<String>
. v
can be any type as long as it
implements Serialize
.
This method overwrites values of any existing keys, otherwise adds the key/value pair to the workflow's standard data file
Example
use alfred_rs::data::Data; let mut workflow_data = Data::load().unwrap(); workflow_data.set("user_id", &0xFF); workflow_data.set("last_log_date", &Utc::now());
Errors:
If v
cannot be serialized or there are file IO issues an error is returned.
pub fn get<K, V>(&self, k: K) -> Option<V> where
K: AsRef<str>,
V: for<'d> Deserialize<'d>,
[src]
pub fn get<K, V>(&self, k: K) -> Option<V> where
K: AsRef<str>,
V: for<'d> Deserialize<'d>,
Get (possible) value of key k
from workflow's data file
If key k
has not been set before None
will be returned.
Since the data can be of arbitrary type, you should annotate the type you are expecting
to get back from data file.
If the stored value cannot be deserialized back to the desired type None
is returned.
Example
use alfred_rs::data::Data; let wf_data = Data::load().unwrap(); let id: i32 = wf_data.get("user_id").expect("user id was not set"); let last_log: DateTime<Utc> = wf_data.get("last_log_date").expect("log date was not set");
pub fn save_to_file<P, V>(p: P, data: &V) -> Result<(), Error> where
P: AsRef<Path>,
V: Serialize,
[src]
pub fn save_to_file<P, V>(p: P, data: &V) -> Result<(), Error> where
P: AsRef<Path>,
V: Serialize,
Function to save (temporary) data
to file named p
in workflow's cache dir
This function is provided so that workflow authors can temporarily save information
to workflow's cache dir. The saved data is considered to be irrelevant to workflow's
actual data (for which you should use set
and get
)
Example
use alfred_rs::data::Data; Data::save_to_file("cached_tags.dat", &vec!["rust", "alfred"]).unwrap();
Note
Only the file_name
portion of p
will be used to name the file in
workflow's cache directory.
Errors
File IO related issues as well as serializing problems will cause an error to be returned.
pub fn load_from_file<P, V>(p: P) -> Option<V> where
P: AsRef<Path>,
V: for<'d> Deserialize<'d>,
[src]
pub fn load_from_file<P, V>(p: P) -> Option<V> where
P: AsRef<Path>,
V: for<'d> Deserialize<'d>,
Function to load some (temporary) data from file named p
in workflow's cache dir
This function is provided so that workflow authors can retrieve temporarily information
saved to workflow's cache dir. The saved data is considered to be irrelevant to workflow's
actual data (for which you should use set
and get
)
Example
use alfred_rs::data::Data; let cached_tags: Vec<String> = Data::load_from_file("cached_tags.dat").unwrap();
Note
Only the file_name
portion of p
will be used to name the file in
workflow's cache directory.