aardvark_core/
session.rs

1//! Session state prepared for execution inside the runtime.
2
3use crate::bundle::Bundle;
4use crate::invocation::InvocationDescriptor;
5use once_cell::sync::OnceCell;
6use std::sync::Arc;
7
8/// Represents a prepared execution context for a specific bundle.
9pub struct PySession {
10    bundle: Bundle,
11    descriptor: InvocationDescriptor,
12    rawctx_spec_json: OnceCell<Option<Arc<String>>>,
13}
14
15impl PySession {
16    pub(crate) fn new(bundle: Bundle, descriptor: InvocationDescriptor) -> Self {
17        Self {
18            bundle,
19            descriptor,
20            rawctx_spec_json: OnceCell::new(),
21        }
22    }
23
24    /// Returns the canonical entrypoint (module:function or script) to execute.
25    pub fn entrypoint(&self) -> &str {
26        self.descriptor.entrypoint()
27    }
28
29    /// Provides read-only access to bundle entries.
30    pub fn bundle(&self) -> &Bundle {
31        &self.bundle
32    }
33
34    /// Returns the invocation descriptor driving this session.
35    pub fn descriptor(&self) -> &InvocationDescriptor {
36        &self.descriptor
37    }
38
39    pub(crate) fn rawctx_spec_json<E, F>(&self, build: F) -> Result<Option<Arc<String>>, E>
40    where
41        F: FnOnce() -> Result<Option<String>, E>,
42    {
43        Ok(self
44            .rawctx_spec_json
45            .get_or_try_init(|| build().map(|value| value.map(Arc::new)))?
46            .clone())
47    }
48
49    /// Returns a simple manifest of the bundle contents.
50    pub fn manifest(&self) -> impl Iterator<Item = (&str, usize)> {
51        self.bundle
52            .entries()
53            .iter()
54            .map(|entry| (entry.path(), entry.contents().len()))
55    }
56}