use crate::model::block::BlockContent;
use crate::model::inline::InlineContent;
#[cfg(feature = "fmt_json")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fmt_json", derive(Serialize, Deserialize))]
pub struct Image {
path_or_label: String,
alt_text: Option<String>,
}
impl From<String> for Image {
fn from(inner: String) -> Self {
Self::new(&inner)
}
}
impl From<&str> for Image {
fn from(inner: &str) -> Self {
Self::new(inner)
}
}
impl Into<BlockContent> for Image {
fn into(self) -> BlockContent {
BlockContent::ImageBlock(self.into())
}
}
inline_impls!(Image);
inner_impl!(Image, String, path_or_label);
impl Image {
pub fn new(path_or_label: &str) -> Self {
assert!(!path_or_label.is_empty());
Self {
path_or_label: path_or_label.to_string(),
alt_text: None,
}
}
pub fn with_alt_text(path_or_label: &str, alt_text: &str) -> Self {
assert!(!path_or_label.is_empty());
assert!(!alt_text.is_empty());
Self {
path_or_label: path_or_label.to_string(),
alt_text: Some(alt_text.to_string()),
}
}
pub fn has_alt_text(&self) -> bool {
self.alt_text.is_some()
}
pub fn alt_text(&self) -> &Option<String> {
&self.alt_text
}
pub fn set_alt_text(&mut self, alt_text: &str) {
self.alt_text = Some(alt_text.to_string())
}
pub fn unset_alt_text(&mut self) {
self.alt_text = None
}
}