vimwiki_core/lang/elements/blocks/inline/
code.rs1use 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 Cow<'a, str>,
23);
24
25impl<'a> CodeInline<'a> {
26 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 #[inline]
77 fn strict_eq(&self, other: &Self) -> bool {
78 self == other
79 }
80}