ragrig_gpui/model/
attachment.rs1use gpui_kit::assets::IconName;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
7pub enum AttachmentKind {
8 Toggle {
10 on: bool,
12 },
13 Pending,
15 Offer {
17 action: String,
19 },
20}
21
22#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct Attachment {
25 pub id: String,
27 pub kind: AttachmentKind,
29 pub icon: IconName,
31 pub label: String,
33 pub hint: Option<String>,
35 pub enabled: bool,
37}
38
39impl Attachment {
40 pub fn document(id: impl Into<String>, label: impl Into<String>) -> Self {
42 Self {
43 id: id.into(),
44 kind: AttachmentKind::Toggle { on: false },
45 icon: IconName::FileText,
46 label: label.into(),
47 hint: None,
48 enabled: true,
49 }
50 }
51
52 pub fn bibliography(id: impl Into<String>, label: impl Into<String>) -> Self {
54 Self {
55 id: id.into(),
56 kind: AttachmentKind::Toggle { on: false },
57 icon: IconName::BookOpen,
58 label: label.into(),
59 hint: None,
60 enabled: true,
61 }
62 }
63
64 pub fn pending(id: impl Into<String>, label: impl Into<String>) -> Self {
66 Self {
67 id: id.into(),
68 kind: AttachmentKind::Pending,
69 icon: IconName::Paperclip,
70 label: label.into(),
71 hint: None,
72 enabled: true,
73 }
74 }
75
76 pub fn offer(
78 id: impl Into<String>,
79 label: impl Into<String>,
80 action: impl Into<String>,
81 ) -> Self {
82 Self {
83 id: id.into(),
84 kind: AttachmentKind::Offer {
85 action: action.into(),
86 },
87 icon: IconName::FileText,
88 label: label.into(),
89 hint: None,
90 enabled: true,
91 }
92 }
93
94 pub fn on(mut self, on: bool) -> Self {
96 if let AttachmentKind::Toggle { on: state } = &mut self.kind {
97 *state = on;
98 }
99 self
100 }
101
102 pub fn hint(mut self, hint: impl Into<String>) -> Self {
104 self.hint = Some(hint.into());
105 self
106 }
107
108 pub fn enabled(mut self, enabled: bool) -> Self {
110 self.enabled = enabled;
111 self
112 }
113
114 pub fn is_on(&self) -> bool {
116 matches!(self.kind, AttachmentKind::Toggle { on: true })
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn toggle_state_is_readable_and_settable() {
126 let attachment = Attachment::document("draft", "draft.md");
127 assert!(!attachment.is_on());
128 assert!(attachment.clone().on(true).is_on());
129 }
130
131 #[test]
132 fn kinds_carry_their_own_action() {
133 let offer = Attachment::offer("refs", "references.bib", "Insert");
134 assert_eq!(
135 offer.kind,
136 AttachmentKind::Offer {
137 action: "Insert".to_owned()
138 }
139 );
140 assert_eq!(
141 Attachment::pending("region", "Selection").kind,
142 AttachmentKind::Pending
143 );
144 }
145}