Skip to main content

SessionTargetHandle

Struct SessionTargetHandle 

Source
pub struct SessionTargetHandle<'a> { /* private fields */ }
Expand description

A scoped handle for operations on a specific target within a session.

Created via Session::target(). Carries the target, email, and timestamp from the session so callers never have to pass them.

§Example

let session = Session::discover()?;
let handle = session.target(&Target::parse("commit:abc123")?);
handle.set("agent:model", "claude")?;
let val = handle.get_value("agent:model")?;

Implementations§

Source§

impl<'a> SessionTargetHandle<'a>

Source

pub fn get_value(&self, key: &str) -> Result<Option<MetaValue>>

Get a metadata value by key.

Source

pub fn set(&self, key: &str, value: impl Into<MetaValue>) -> Result<()>

Set a metadata value with convenience conversion.

Accepts anything that converts to MetaValue: &str, String, Vec<ListEntry>, BTreeSet<String>, or MetaValue directly.

handle.set("key", "hello")?;                    // string
handle.set("key", MetaValue::String("hello".into()))?; // explicit

Uses the session’s email and timestamp automatically.

Source

pub fn set_record(&self, prefix: &str, record: impl Serialize) -> Result<()>

Merge string metadata fields under a common key prefix.

The record must serialize to a JSON object. Object field names, including serde renames, become key suffixes. String values are written as prefix:field.

This is a partial update, not a replacement operation. Null fields are skipped and existing keys are left untouched. This makes Option<T> fields useful for patch-style records, but callers that need to clear a field must remove that key explicitly.

#[derive(serde::Serialize)]
#[serde(rename_all = "kebab-case")]
struct Source<'a> {
    agent: &'a str,
    tool_version: Option<&'a str>,
}

handle.set_record("agent-session:abc:source:def", &Source {
    agent: "codex",
    tool_version: Some("1.2.3"),
})?;

// Later updates that serialize `tool_version` as null do not remove the
// existing `agent-session:abc:source:def:tool-version` key.
handle.set_record("agent-session:abc:source:def", &Source {
    agent: "codex",
    tool_version: None,
})?;
Source

pub fn get_record<T>(&self, prefix: &str) -> Result<Option<T>>

Read string metadata fields under a common key prefix into a record.

This is the read-side pair to set_record. Immediate child keys like prefix:field become JSON object fields before being deserialized into T. Missing records return Ok(None). Nested keys such as prefix:child:field are ignored because they belong to a deeper metadata subtree, not this record.

Because set_record leaves null or omitted fields untouched, get_record reads the current merged field set under the prefix, not the exact last value passed to set_record.

§Errors

Returns an error if an immediate child field exists but is not a string, or if the collected fields do not deserialize into T.

Source

pub fn remove(&self, key: &str) -> Result<bool>

Remove a metadata key.

Uses the session’s email and timestamp automatically.

Source

pub fn list_push(&self, key: &str, value: &str) -> Result<()>

Push a value onto a list.

Uses the session’s email and timestamp automatically.

Source

pub fn apply_edits<'b>( &self, edits: impl IntoIterator<Item = MetaEdit<'b>>, ) -> Result<()>

Apply list/set edits in one transaction.

Empty edit batches are no-ops. If any edit fails, none of the batch is committed. The session email and timestamp are used for every edit.

Source

pub fn list_pop(&self, key: &str, value: &str) -> Result<()>

Pop a value from a list.

Uses the session’s email and timestamp automatically.

Source

pub fn list_remove(&self, key: &str, index: usize) -> Result<()>

Remove a list entry by index.

Uses the session’s email and timestamp automatically.

Source

pub fn set_add(&self, key: &str, value: &str) -> Result<()>

Add a member to a set.

Uses the session’s email and timestamp automatically.

Source

pub fn set_remove(&self, key: &str, value: &str) -> Result<()>

Remove a member from a set.

Uses the session’s email and timestamp automatically.

Source

pub fn target(&self) -> &Target

The target this handle is scoped to.

Source

pub fn get_all_values( &self, prefix: Option<&str>, ) -> Result<Vec<(String, MetaValue)>>

Get all metadata for this target as typed (key, value) pairs.

Optionally filters by key prefix (e.g., Some("agent") returns all keys starting with agent or agent:).

§Parameters
  • prefix: optional key prefix to filter by
§Returns

A vector of (key, MetaValue) pairs for matching metadata entries.

§Errors

Returns an error if the database read or deserialization fails.

Source

pub fn list_entries(&self, key: &str) -> Result<Vec<ListEntry>>

Get list entries for a key on this target.

§Parameters
  • key: the metadata key name
§Returns

A vector of ListEntry values with resolved content and timestamps.

§Errors

Returns an error if the key is missing, the value is not a list, or the database read fails.

Source

pub fn get_authorship(&self, key: &str) -> Result<Option<Authorship>>

Get authorship info (last author email and timestamp) for a key on this target.

§Parameters
  • key: the metadata key name
§Returns

Some(Authorship) if the key has been modified at least once, None otherwise.

§Errors

Returns an error if the database read fails.

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
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.