context_store.rs

 1use gpui::SharedString;
 2
 3use crate::context::{Context, ContextId, ContextKind};
 4
 5pub struct ContextStore {
 6    context: Vec<Context>,
 7    next_context_id: ContextId,
 8}
 9
10impl ContextStore {
11    pub fn new() -> Self {
12        Self {
13            context: Vec::new(),
14            next_context_id: ContextId(0),
15        }
16    }
17
18    pub fn context(&self) -> &Vec<Context> {
19        &self.context
20    }
21
22    pub fn drain(&mut self) -> Vec<Context> {
23        self.context.drain(..).collect()
24    }
25
26    pub fn clear(&mut self) {
27        self.context.clear();
28    }
29
30    pub fn insert_context(
31        &mut self,
32        kind: ContextKind,
33        name: impl Into<SharedString>,
34        text: impl Into<SharedString>,
35    ) {
36        self.context.push(Context {
37            id: self.next_context_id.post_inc(),
38            name: name.into(),
39            kind: kind.clone(),
40            text: text.into(),
41            icon: kind.icon(),
42        });
43    }
44
45    pub fn remove_context(&mut self, id: &ContextId) {
46        self.context.retain(|context| context.id != *id);
47    }
48}