aither_sandbox/
output_policy.rs1use std::sync::Arc;
4
5use arc_swap::ArcSwap;
6
7use crate::output::MediaResolution;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct MediaBounds {
12 pub low: u32,
14 pub medium: u32,
16 pub high: u32,
18}
19
20impl MediaBounds {
21 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub struct OutputLimits {
38 pub inline_bytes: usize,
41 pub preview_bytes: usize,
43 pub default_lines: u32,
45 pub ceiling_lines: u32,
47 pub read_bytes: usize,
49 pub media: MediaBounds,
51 pub job_preview_bytes: usize,
53}
54
55#[derive(Debug, Clone)]
60pub struct OutputPolicy(Arc<ArcSwap<OutputLimits>>);
61
62impl OutputPolicy {
63 #[must_use]
65 pub fn new(limits: OutputLimits) -> Self {
66 Self(Arc::new(ArcSwap::from_pointee(limits)))
67 }
68
69 #[must_use]
71 pub fn get(&self) -> OutputLimits {
72 **self.0.load()
73 }
74
75 pub fn set(&self, limits: OutputLimits) {
77 self.0.store(Arc::new(limits));
78 }
79
80 #[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 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 pub fn policy() -> OutputPolicy {
112 OutputPolicy::new(LIMITS)
113 }
114}