Skip to main content

aither_sandbox/
output_policy.rs

1//! What a shell inlines, cuts and reads back: the embedder's output budgets.
2
3use std::sync::Arc;
4
5use arc_swap::ArcSwap;
6
7use crate::output::MediaResolution;
8
9/// The pixel bounding boxes of the [`MediaResolution`] levels that resize.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct MediaBounds {
12    /// The bounding box of [`MediaResolution::Low`], in pixels.
13    pub low: u32,
14    /// The bounding box of [`MediaResolution::Medium`], in pixels.
15    pub medium: u32,
16    /// The bounding box of [`MediaResolution::High`], in pixels.
17    pub high: u32,
18}
19
20impl MediaBounds {
21    /// The bounding box a level resizes to; `None` keeps the source media.
22    #[must_use]
23    pub const fn bound(self, resolution: MediaResolution) -> Option<u32> {
24        match resolution {
25            MediaResolution::Auto | MediaResolution::Native => None,
26            MediaResolution::Low => Some(self.low),
27            MediaResolution::Medium => Some(self.medium),
28            MediaResolution::High => Some(self.high),
29        }
30    }
31}
32
33/// Every budget of the output a shell hands the model.
34///
35/// There is no `Default`: the values are the embedder's.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub struct OutputLimits {
38    /// The most bytes of a text output delivered inline; a larger output is
39    /// saved to a file.
40    pub inline_bytes: usize,
41    /// The most bytes of the preview a saved text output keeps in the result.
42    pub preview_bytes: usize,
43    /// The inline line budget of a call that does not state `max_lines`.
44    pub default_lines: u32,
45    /// The most lines a call may ask to inline.
46    pub ceiling_lines: u32,
47    /// The bytes a read of a job returns when the call states no `max_bytes`.
48    pub read_bytes: usize,
49    /// The pixel bounds of the resizing media levels.
50    pub media: MediaBounds,
51    /// The most bytes of a job's script a job listing shows.
52    pub job_preview_bytes: usize,
53}
54
55/// The live handle to a shell's [`OutputLimits`].
56///
57/// Every clone shares the values, so an update reaches the shells and tools
58/// already running.
59#[derive(Debug, Clone)]
60pub struct OutputPolicy(Arc<ArcSwap<OutputLimits>>);
61
62impl OutputPolicy {
63    /// A policy holding `limits`.
64    #[must_use]
65    pub fn new(limits: OutputLimits) -> Self {
66        Self(Arc::new(ArcSwap::from_pointee(limits)))
67    }
68
69    /// The limits in force now.
70    #[must_use]
71    pub fn get(&self) -> OutputLimits {
72        **self.0.load()
73    }
74
75    /// Replaces the limits for every holder of this handle.
76    pub fn set(&self, limits: OutputLimits) {
77        self.0.store(Arc::new(limits));
78    }
79
80    /// The inline line budget for a call asking for `requested` lines: the
81    /// default when it asks for none, never more than the ceiling.
82    #[must_use]
83    pub fn lines(&self, requested: Option<u32>) -> usize {
84        let limits = self.get();
85        requested
86            .unwrap_or(limits.default_lines)
87            .min(limits.ceiling_lines) as usize
88    }
89}
90
91#[cfg(any(test, feature = "test-budgets"))]
92pub mod fixture {
93    use super::{MediaBounds, OutputLimits, OutputPolicy};
94
95    /// The limits tests use, whose numbers stand for nothing.
96    pub const LIMITS: OutputLimits = OutputLimits {
97        inline_bytes: 16_000,
98        preview_bytes: 16_000,
99        default_lines: 200,
100        ceiling_lines: 800,
101        read_bytes: 32 * 1024,
102        media: MediaBounds {
103            low: 512,
104            medium: 1024,
105            high: 2048,
106        },
107        job_preview_bytes: 50,
108    };
109
110    /// A policy holding [`LIMITS`].
111    pub fn policy() -> OutputPolicy {
112        OutputPolicy::new(LIMITS)
113    }
114}