Skip to main content

flag_rs/
completion_item.rs

1//! Memory-efficient completion item structure
2//!
3//! This module provides a more memory-efficient way to store completion items
4//! by using a single struct instead of parallel vectors.
5
6use std::borrow::Cow;
7
8/// A single completion item with optional description
9#[derive(Clone, Debug)]
10pub struct CompletionItem {
11    /// The completion value
12    pub value: Cow<'static, str>,
13    /// Optional description for the completion
14    pub description: Option<Cow<'static, str>>,
15}
16
17impl CompletionItem {
18    /// Creates a new completion item without description
19    #[must_use]
20    pub fn new(value: impl Into<Cow<'static, str>>) -> Self {
21        Self {
22            value: value.into(),
23            description: None,
24        }
25    }
26
27    /// Creates a new completion item with description
28    #[must_use]
29    pub fn with_description(
30        value: impl Into<Cow<'static, str>>,
31        desc: impl Into<Cow<'static, str>>,
32    ) -> Self {
33        Self {
34            value: value.into(),
35            description: Some(desc.into()),
36        }
37    }
38
39    /// Returns the description or an empty string
40    #[must_use]
41    pub fn description_or_empty(&self) -> &str {
42        self.description.as_deref().unwrap_or("")
43    }
44}
45
46impl From<&'static str> for CompletionItem {
47    fn from(value: &'static str) -> Self {
48        Self::new(value)
49    }
50}
51
52impl From<String> for CompletionItem {
53    fn from(value: String) -> Self {
54        Self::new(value)
55    }
56}
57
58impl From<(&'static str, &'static str)> for CompletionItem {
59    fn from((value, desc): (&'static str, &'static str)) -> Self {
60        Self::with_description(value, desc)
61    }
62}