Skip to main content

elicitation/containers/
vec.rs

1//! Vec<T> implementation for collection elicitation.
2
3use crate::{ElicitClient, ElicitResult, Elicitation, Prompt};
4
5// Default-only style for Vec
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
7pub enum VecStyle {
8    #[default]
9    Default,
10}
11
12impl Prompt for VecStyle {
13    fn prompt() -> Option<&'static str> {
14        None
15    }
16}
17
18impl Elicitation for VecStyle {
19    type Style = VecStyle;
20
21    #[tracing::instrument(skip(_client), level = "trace")]
22    async fn elicit(_client: &ElicitClient<'_>) -> ElicitResult<Self> {
23        Ok(Self::Default)
24    }
25}
26
27impl<T: Elicitation + Send> Prompt for Vec<T> {
28    fn prompt() -> Option<&'static str> {
29        Some("Would you like to add items to this collection?")
30    }
31}
32
33impl<T: Elicitation + Send> Elicitation for Vec<T> {
34    type Style = VecStyle;
35
36    #[tracing::instrument(skip(client), fields(item_type = std::any::type_name::<T>()))]
37    async fn elicit(client: &ElicitClient<'_>) -> ElicitResult<Self> {
38        let mut items = Vec::new();
39        tracing::debug!("Eliciting vector");
40
41        loop {
42            let add_more = if items.is_empty() {
43                // First item - different prompt
44                tracing::debug!("Prompting for first item");
45                // TODO: In future, could customize prompt: "Add first item?"
46                bool::elicit(client).await?
47            } else {
48                // Subsequent items
49                tracing::debug!(count = items.len(), "Prompting for additional item");
50                // TODO: In future, could customize prompt: "Add another item?"
51                bool::elicit(client).await?
52            };
53
54            if !add_more {
55                tracing::debug!(final_count = items.len(), "Collection complete");
56                break;
57            }
58
59            tracing::debug!("Eliciting item");
60            let item = T::elicit(client).await?;
61            items.push(item);
62        }
63
64        Ok(items)
65    }
66}