Skip to main content

save

Function save 

Source
pub fn save(
    store: &dyn Store,
    log: &str,
    conversation: &mut Conversation,
) -> Result<(), SessionError>
Expand description

Appends the unsaved changes of conversation to the log log of store, starting it when there is none, and leaves the conversation with nothing unsaved. A torn last line of the log is first set aside, as Writer::open does.

use rig_core::message::Message;
use rig_memory::{MemoryStore, load, save};

let store = MemoryStore::default();
let mut conversation = load(&store, "user-123")?;
conversation.push(Message::user("My name is Alice."));
save(&store, "user-123", &mut conversation)?;
assert_eq!(load(&store, "user-123")?, conversation);

§Errors

SessionError::Changed when conversation does not continue the log (the log moved on since it was loaded, or it was loaded from another log or built elsewhere) and the log holds changes, or when another writer changed the log between this save’s read and its append, writing nothing to it, so no change made there is lost; when the store fails, log is in another format, or a line of it has no stamp; or when a write fails. It reads the stamps of log’s records, not the records, so a change that does not apply is refused by load, not here. Onto an empty log any conversation is saved, whole when it was not begun from nothing.