Skip to main content

tokenfold_core/
input.rs

1use serde::{Deserialize, Serialize};
2
3use crate::report::CompressionReport;
4
5#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
6#[serde(rename_all = "snake_case")]
7pub enum InputFormat {
8    Auto,
9    OpenAiJson,
10    AnthropicJson,
11    /// Generic JSON data (API responses, records, logs) that isn't an LLM message payload.
12    /// Unlocks the JSON-data transforms (`json_minify`, `json_field_fold`).
13    Json,
14    PlainText,
15    CommandOutput,
16    GitDiff,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
20pub struct CompressionInput {
21    pub format: InputFormat,
22    pub bytes: Vec<u8>,
23}
24
25impl CompressionInput {
26    pub fn openai_json(bytes: impl Into<Vec<u8>>) -> Self {
27        Self {
28            format: InputFormat::OpenAiJson,
29            bytes: bytes.into(),
30        }
31    }
32
33    pub fn anthropic_json(bytes: impl Into<Vec<u8>>) -> Self {
34        Self {
35            format: InputFormat::AnthropicJson,
36            bytes: bytes.into(),
37        }
38    }
39
40    /// Generic JSON data (not an LLM message payload). Enables `json_minify`,
41    /// `json_field_fold`, and `json_value_dict`.
42    pub fn json(bytes: impl Into<Vec<u8>>) -> Self {
43        Self {
44            format: InputFormat::Json,
45            bytes: bytes.into(),
46        }
47    }
48
49    pub fn plain_text(bytes: impl Into<Vec<u8>>) -> Self {
50        Self {
51            format: InputFormat::PlainText,
52            bytes: bytes.into(),
53        }
54    }
55
56    pub fn command_output(bytes: impl Into<Vec<u8>>) -> Self {
57        Self {
58            format: InputFormat::CommandOutput,
59            bytes: bytes.into(),
60        }
61    }
62
63    pub fn git_diff(bytes: impl Into<Vec<u8>>) -> Self {
64        Self {
65            format: InputFormat::GitDiff,
66            bytes: bytes.into(),
67        }
68    }
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] // NOT Eq: report holds f64
72pub struct CompressionOutput {
73    pub bytes: Vec<u8>,
74    pub report: CompressionReport,
75}