markdown_composer/types/
link.rs1use crate::traits::{AsFooter, MarkdownElement};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Clone, Debug, Default)]
8#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
9pub struct Link {
10 pub text: String,
12 pub url: String,
14 pub footer: bool,
16 pub inlined: bool,
18}
19
20impl Link {
21 pub fn new() -> Self {
23 Self::default()
24 }
25
26 pub fn from(
28 text: impl Into<String>,
29 url: impl Into<String>,
30 footer: bool,
31 inlined: bool,
32 ) -> Self {
33 Self {
34 text: text.into(),
35 url: url.into(),
36 footer,
37 inlined,
38 }
39 }
40}
41
42impl AsFooter for Link {
43 fn as_footer(&self) -> Box<dyn MarkdownElement> {
44 Box::new(format!("[{}]: {}", self.text, self.url))
45 }
46}
47
48impl fmt::Display for Link {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 let text = if self.footer {
51 format!("[{}][{}]", self.text, self.text)
52 } else {
53 format!("[{}]({})", self.text, self.url)
54 };
55
56 if self.inlined {
57 write!(f, "{}", text)
58 } else {
59 writeln!(f, "{}", text)
60 }
61 }
62}