Skip to main content

earl_core/
lib.rs

1pub mod allowlist;
2pub mod decode;
3pub mod redact;
4pub mod render;
5pub mod schema;
6pub mod transport;
7
8use std::future::Future;
9
10use serde_json::{Map, Value};
11
12pub use allowlist::ensure_url_allowed;
13pub use decode::{DecodedBody, decode_response};
14pub use redact::Redactor;
15pub use render::TemplateRenderer;
16pub use schema::{AllowRule, CommandMode, ResultDecode, ResultExtract, ResultTemplate};
17pub use transport::ResolvedTransport;
18
19/// Body data prepared for HTTP-like protocol execution.
20#[derive(Debug, Clone)]
21pub enum PreparedBody {
22    Empty,
23    Json(Value),
24    Form(Vec<(String, String)>),
25    Multipart(Vec<PreparedMultipartPart>),
26    RawBytes {
27        bytes: Vec<u8>,
28        content_type: Option<String>,
29    },
30}
31
32/// A single part in a multipart body.
33#[derive(Debug, Clone)]
34pub struct PreparedMultipartPart {
35    pub name: String,
36    pub bytes: Vec<u8>,
37    pub content_type: Option<String>,
38    pub filename: Option<String>,
39}
40
41/// Unified result type returned by all protocol executors.
42#[derive(Debug, Clone)]
43pub struct ExecutionResult {
44    pub status: u16,
45    pub url: String,
46    pub result: Value,
47    pub decoded: Value,
48}
49
50/// Raw protocol output before decode/extract post-processing.
51#[derive(Debug, Clone)]
52pub struct RawExecutionResult {
53    pub status: u16,
54    pub url: String,
55    pub body: Vec<u8>,
56    pub content_type: Option<String>,
57}
58
59/// Shared execution context passed to all protocol executors alongside
60/// their protocol-specific prepared data.
61#[derive(Debug, Clone)]
62pub struct ExecutionContext {
63    pub key: String,
64    pub mode: CommandMode,
65    pub allow_rules: Vec<AllowRule>,
66    pub transport: ResolvedTransport,
67    pub result_template: ResultTemplate,
68    pub args: Map<String, Value>,
69    pub redactor: Redactor,
70}
71
72/// Contract implemented by all protocol executors.
73///
74/// Each protocol crate provides an executor struct that implements this trait.
75/// The associated `PreparedData` type links the executor to its matching
76/// prepared data produced by the builder.
77pub trait ProtocolExecutor {
78    /// Protocol-specific prepared data (e.g. `PreparedHttpData`, `PreparedBashScript`).
79    type PreparedData: Clone + std::fmt::Debug + Send + Sync;
80
81    /// Execute a single protocol request and return the raw result.
82    fn execute(
83        &mut self,
84        data: &Self::PreparedData,
85        context: &ExecutionContext,
86    ) -> impl Future<Output = anyhow::Result<RawExecutionResult>> + Send;
87}