Skip to main content

oharness_core/
task.rs

1//! Task and Attachment types (ยง4.3).
2
3use crate::MetadataMap;
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6use url::Url;
7
8/// Pure data description of a task. No behaviour, no closures. Success predicates
9/// live in `TaskEvaluator`.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[cfg_attr(feature = "schemars-export", derive(schemars::JsonSchema))]
12pub struct Task {
13    #[serde(default, skip_serializing_if = "Option::is_none")]
14    pub id: Option<String>,
15
16    pub instruction: String,
17
18    #[serde(default, skip_serializing_if = "Vec::is_empty")]
19    pub attachments: Vec<Attachment>,
20
21    /// Reverse-DNS-namespaced keys. Unknown keys round-trip.
22    #[serde(default, skip_serializing_if = "MetadataMap::is_empty")]
23    pub metadata: MetadataMap,
24}
25
26impl Task {
27    pub fn new(instruction: impl Into<String>) -> Self {
28        Self {
29            id: None,
30            instruction: instruction.into(),
31            attachments: Vec::new(),
32            metadata: MetadataMap::new(),
33        }
34    }
35
36    pub fn with_id(mut self, id: impl Into<String>) -> Self {
37        self.id = Some(id.into());
38        self
39    }
40
41    pub fn with_attachment(mut self, a: Attachment) -> Self {
42        self.attachments.push(a);
43        self
44    }
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[cfg_attr(feature = "schemars-export", derive(schemars::JsonSchema))]
49#[serde(tag = "kind", rename_all = "snake_case")]
50pub enum Attachment {
51    Text {
52        name: String,
53        content: String,
54    },
55    File {
56        name: String,
57        #[cfg_attr(feature = "schemars-export", schemars(with = "String"))]
58        path: PathBuf,
59    },
60    Inline {
61        name: String,
62        mime: String,
63        bytes: Vec<u8>,
64    },
65    Url {
66        #[cfg_attr(feature = "schemars-export", schemars(with = "String"))]
67        url: Url,
68        mime_hint: Option<String>,
69    },
70}