aboutsummaryrefslogtreecommitdiff
path: root/src/app/config/markdown_string.rs
diff options
context:
space:
mode:
authorXe Iaso <me@christine.website>2023-02-02 08:05:34 -0500
committerXe Iaso <me@christine.website>2023-02-02 08:05:34 -0500
commitc117eae7c5af977d0299d34169e4a403f77e2afa (patch)
tree83438cf5abc605e0ec174aa4a3f0e4b91b640008 /src/app/config/markdown_string.rs
parent757cee6fdd13502eb62305f77e73698878b01aa2 (diff)
downloadxesite-c117eae7c5af977d0299d34169e4a403f77e2afa.tar.xz
xesite-c117eae7c5af977d0299d34169e4a403f77e2afa.zip
add stream VOD page
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'src/app/config/markdown_string.rs')
-rw-r--r--src/app/config/markdown_string.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/app/config/markdown_string.rs b/src/app/config/markdown_string.rs
new file mode 100644
index 0000000..aad803b
--- /dev/null
+++ b/src/app/config/markdown_string.rs
@@ -0,0 +1,64 @@
+use std::fmt;
+
+use maud::{html, Markup, PreEscaped, Render};
+use serde::{
+ de::{self, Visitor},
+ Deserialize, Deserializer, Serialize,
+};
+
+struct StringVisitor;
+
+impl<'de> Visitor<'de> for StringVisitor {
+ type Value = MarkdownString;
+
+ fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(MarkdownString(xesite_markdown::render(value).map_err(
+ |why| de::Error::invalid_value(de::Unexpected::Other(&format!("{why}")), &self),
+ )?))
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(MarkdownString(xesite_markdown::render(value).map_err(
+ |why| de::Error::invalid_value(de::Unexpected::Other(&format!("{why}")), &self),
+ )?))
+ }
+
+ fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(MarkdownString(xesite_markdown::render(&value).map_err(
+ |why| de::Error::invalid_value(de::Unexpected::Other(&format!("{why}")), &self),
+ )?))
+ }
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a string with xesite-flavored markdown")
+ }
+}
+
+#[derive(Serialize, Clone, Default)]
+pub struct MarkdownString(String);
+
+impl<'de> Deserialize<'de> for MarkdownString {
+ fn deserialize<D>(deserializer: D) -> Result<MarkdownString, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_string(StringVisitor)
+ }
+}
+
+impl Render for MarkdownString {
+ fn render(&self) -> Markup {
+ html! {
+ (PreEscaped(&self.0))
+ }
+ }
+}