Skip to main content

troupekit_trace/
spec.rs

1use crate::category::{Trace, TraceSet};
2use crate::error::TraceError;
3
4const NAMES_MAX: usize = 64;
5
6/// Plain trace settings as written in a persona or workflow file.
7/// Input: categories, content flag and an optional file path.
8/// Output: getters; the default is every category, no content, no file.
9#[derive(Clone, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub struct TraceSpec {
12    categories: TraceSet,
13    content: bool,
14    file: Option<String>,
15}
16
17impl Default for TraceSpec {
18    fn default() -> TraceSpec {
19        TraceSpec::new(TraceSet::all())
20    }
21}
22
23impl TraceSpec {
24    /// Returns a spec for `categories`, with content off and no file.
25    /// Input: the categories. Output: the spec.
26    pub fn new(categories: TraceSet) -> TraceSpec {
27        TraceSpec {
28            categories,
29            content: false,
30            file: None,
31        }
32    }
33
34    /// Returns this spec with prompt and reply text kept or withheld.
35    /// Input: true to keep content. Output: the spec.
36    pub fn with_content(mut self, content: bool) -> TraceSpec {
37        self.content = content;
38        self
39    }
40
41    /// Returns this spec writing to `file`. The path is not checked here.
42    /// Input: the file path. Output: the spec.
43    pub fn with_file(mut self, file: impl Into<String>) -> TraceSpec {
44        self.file = Some(file.into());
45        self
46    }
47
48    /// Returns the categories. Input: none. Output: the set.
49    pub fn categories(&self) -> TraceSet {
50        self.categories
51    }
52
53    /// Returns whether content is kept. Input: none. Output: the flag.
54    pub fn content(&self) -> bool {
55        self.content
56    }
57
58    /// Returns the file path, if set. Input: none. Output: the path.
59    pub fn file(&self) -> Option<&str> {
60        self.file.as_deref()
61    }
62
63    /// Parses a TOML `trace` value given as one string or a list of strings.
64    /// `"all"` or `"true"` is every category, `"false"` or `"none"` is none,
65    /// any other item is a category name. Input: at most 64 items.
66    /// Output: a spec with those categories, or the first bad item.
67    pub fn parse_value<I, S>(items: I) -> Result<TraceSpec, TraceError>
68    where
69        I: IntoIterator<Item = S>,
70        S: AsRef<str>,
71    {
72        let mut categories = TraceSet::none();
73        for (count, item) in items.into_iter().enumerate() {
74            if count >= NAMES_MAX {
75                return Err(TraceError::TooManyNames(count + 1));
76            }
77            categories = categories.union(parse_item(item.as_ref())?);
78        }
79        Ok(TraceSpec::new(categories))
80    }
81}
82
83fn parse_item(item: &str) -> Result<TraceSet, TraceError> {
84    match item {
85        "all" | "true" => Ok(TraceSet::all()),
86        "false" | "none" => Ok(TraceSet::none()),
87        name => name.parse::<Trace>().map(|t| TraceSet::none().with(t)),
88    }
89}