Skip to main content

opys_core/
extract.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4pub struct ExtractPick {
5    pub file: String,
6    pub into: String,
7}
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct ExtractScan {
11    pub matches: String,
12    pub into: String,
13    #[serde(default, skip_serializing_if = "Option::is_none")]
14    pub strip: Option<Vec<String>>,
15    #[serde(default, skip_serializing_if = "Option::is_none")]
16    pub includes: Option<Vec<String>>,
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub excludes: Option<Vec<String>>,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
22pub struct ExtractDump {
23    pub into: String,
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub clean: Option<bool>,
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub includes: Option<Vec<String>>,
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub excludes: Option<Vec<String>>,
30}
31
32/// A single extract rule, discriminated structurally on the wire (which
33/// field is present: `file` → Pick, `matches` → Scan, otherwise Dump).
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum ExtractRule {
37    Pick(ExtractPick),
38    Scan(ExtractScan),
39    Dump(ExtractDump),
40}
41
42/// Wire form: one rule or many. Decoding flattens both into `Vec<ExtractRule>`;
43/// encoding emits the bare object when the list has exactly one entry.
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45#[serde(untagged)]
46pub enum ExtractWire {
47    One(ExtractRule),
48    Many(Vec<ExtractRule>),
49}
50
51pub fn decode_extract(raw: ExtractWire) -> Vec<ExtractRule> {
52    match raw {
53        ExtractWire::One(r) => vec![r],
54        ExtractWire::Many(v) => v,
55    }
56}
57
58pub fn encode_extract(rules: &[ExtractRule]) -> ExtractWire {
59    if rules.len() == 1 {
60        ExtractWire::One(rules[0].clone())
61    } else {
62        ExtractWire::Many(rules.to_vec())
63    }
64}