use gpui_kit::assets::IconName;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AttachmentKind {
Toggle {
on: bool,
},
Pending,
Offer {
action: String,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Attachment {
pub id: String,
pub kind: AttachmentKind,
pub icon: IconName,
pub label: String,
pub hint: Option<String>,
pub enabled: bool,
}
impl Attachment {
pub fn document(id: impl Into<String>, label: impl Into<String>) -> Self {
Self {
id: id.into(),
kind: AttachmentKind::Toggle { on: false },
icon: IconName::FileText,
label: label.into(),
hint: None,
enabled: true,
}
}
pub fn bibliography(id: impl Into<String>, label: impl Into<String>) -> Self {
Self {
id: id.into(),
kind: AttachmentKind::Toggle { on: false },
icon: IconName::BookOpen,
label: label.into(),
hint: None,
enabled: true,
}
}
pub fn pending(id: impl Into<String>, label: impl Into<String>) -> Self {
Self {
id: id.into(),
kind: AttachmentKind::Pending,
icon: IconName::Paperclip,
label: label.into(),
hint: None,
enabled: true,
}
}
pub fn offer(
id: impl Into<String>,
label: impl Into<String>,
action: impl Into<String>,
) -> Self {
Self {
id: id.into(),
kind: AttachmentKind::Offer {
action: action.into(),
},
icon: IconName::FileText,
label: label.into(),
hint: None,
enabled: true,
}
}
pub fn on(mut self, on: bool) -> Self {
if let AttachmentKind::Toggle { on: state } = &mut self.kind {
*state = on;
}
self
}
pub fn hint(mut self, hint: impl Into<String>) -> Self {
self.hint = Some(hint.into());
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn is_on(&self) -> bool {
matches!(self.kind, AttachmentKind::Toggle { on: true })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn toggle_state_is_readable_and_settable() {
let attachment = Attachment::document("draft", "draft.md");
assert!(!attachment.is_on());
assert!(attachment.clone().on(true).is_on());
}
#[test]
fn kinds_carry_their_own_action() {
let offer = Attachment::offer("refs", "references.bib", "Insert");
assert_eq!(
offer.kind,
AttachmentKind::Offer {
action: "Insert".to_owned()
}
);
assert_eq!(
Attachment::pending("region", "Selection").kind,
AttachmentKind::Pending
);
}
}