vimwiki_core/lang/elements/blocks/inline/
code.rs

1use crate::StrictEq;
2use derive_more::{AsRef, Constructor, Display, Into};
3use serde::{Deserialize, Serialize};
4use std::borrow::Cow;
5
6#[derive(
7    AsRef,
8    Constructor,
9    Clone,
10    Debug,
11    Display,
12    Into,
13    Eq,
14    PartialEq,
15    Hash,
16    Serialize,
17    Deserialize,
18)]
19#[as_ref(forward)]
20pub struct CodeInline<'a>(
21    /// Represents the text contained within the inline code snippet
22    Cow<'a, str>,
23);
24
25impl<'a> CodeInline<'a> {
26    /// Extracts a string slice containing the entire code snippet
27    ///
28    /// # Examples
29    ///
30    /// Basic usage:
31    ///
32    /// ```
33    /// # use std::borrow::Cow;
34    /// # use vimwiki_core::CodeInline;
35    /// let code = CodeInline::new(Cow::Borrowed("some code"));
36    /// assert_eq!(code.as_str(), "some code");
37    /// ```
38    pub fn as_str(&self) -> &str {
39        self.0.as_ref()
40    }
41}
42
43impl CodeInline<'_> {
44    pub fn as_borrowed(&self) -> CodeInline {
45        use self::Cow::*;
46
47        let code = Cow::Borrowed(match &self.0 {
48            Borrowed(x) => *x,
49            Owned(x) => x.as_str(),
50        });
51
52        CodeInline::new(code)
53    }
54
55    pub fn into_owned(self) -> CodeInline<'static> {
56        let code = Cow::from(self.0.into_owned());
57
58        CodeInline::new(code)
59    }
60}
61
62impl<'a> From<&'a str> for CodeInline<'a> {
63    fn from(s: &'a str) -> Self {
64        Self::new(Cow::Borrowed(s))
65    }
66}
67
68impl From<String> for CodeInline<'static> {
69    fn from(s: String) -> Self {
70        Self::new(Cow::Owned(s))
71    }
72}
73
74impl<'a> StrictEq for CodeInline<'a> {
75    /// Same as PartialEq
76    #[inline]
77    fn strict_eq(&self, other: &Self) -> bool {
78        self == other
79    }
80}