legacy
Avril 4 years ago
parent 3533d752c5
commit 074a84860e
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -0,0 +1,113 @@
//! HTML rendering
use super::*;
use std::{
fmt::{
self,
Display,
},
};
/// Coerce a `DisplayHtml` value into an opaque implementor of `fmt::Display`.
#[inline] pub fn display_html<'a, T: DisplayHtml+?Sized>(from: &'a T) -> impl fmt::Display +'a
{
struct Wrap<'a, T: DisplayHtml+?Sized>(&'a T);
impl<'a,T> fmt::Display for Wrap<'a,T>
where T: DisplayHtml+?Sized
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
DisplayHtml::fmt(self.0, f)
}
}
Wrap(from)
}
/// Coerse a `Display` value into a `DisplayHtml` by HTML-escaping its output.
///
/// To output raw HTML, see `Safe<T>`.
#[inline] pub fn escape_html<'a, T: Display+?Sized>(from: &'a T) -> impl DisplayHtml +'a
{
struct Wrap<'a, T: Display+?Sized>(&'a T);
impl<'a,T> DisplayHtml for Wrap<'a,T>
where T: Display +?Sized
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
//TODO: Escape HTML the string
}
}
Wrap(from)
}
/// HTML-safe wrapper around
#[derive(Debug, PartialEq, Eq)]
pub struct Safe<T: Display>(T);
impl<T: Display> Safe<T>
{
/// Create a new instance with this value
#[inline] pub fn new(value: T) -> Self
{
Self(value)
}
/// Consume this instance into its inner value
pub fn into_inner(self) -> T
{
self.0
}
/// Gets a reference to the inner value
pub fn display(&self) -> &T
{
&self.0
}
}
impl<T: Display> From<T> for Safe<T>
{
#[inline] fn from(from: T) -> Self
{
Self(from)
}
}
/// A trait for displaying as html.
///
/// Any type implementing `Display` will implement `DisplayHtml` by HTML-escaping the output from it's display formatter.
/// You can also use `Safe<T>` to render as raw HTML.
pub trait DisplayHtml
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
#[inline] fn to_html(&self) -> String
{
display_html(self).to_string()
}
}
impl<T: Display> DisplayHtml for Safe<T>
{
#[inline] fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result
{
self.0.fmt(fmt)
}
#[inline] fn to_html(&self) -> String
{
self.0.to_string()
}
}
impl<T: ?Sized> DisplayHtml for T
where T: Display
{
#[inline] fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result
{
escape_html(self).fmt(fmt)
}
}

@ -24,6 +24,30 @@ use futures::{
}; };
use hex_literal::hex; use hex_literal::hex;
macro_rules! cfg_debug {
(if {$($if:tt)*} else {$($else:tt)*}) => {
{
#[cfg(debug_assertions)] {
$($if)*
}
#[cfg(not(debug_assertions))] {
$($else)*
}
}
};
(if $if:expr) => {
{
#[cfg(debug_assertions)] $if
}
};
(else $else:expr) => {
{
#[cfg(not(debug_assertions))] $else
}
};
}
mod ext; mod ext;
use ext::*; use ext::*;
mod bytes; mod bytes;
@ -38,6 +62,7 @@ mod identity;
mod post; mod post;
mod state; mod state;
mod html;
mod web; mod web;
#[tokio::main] #[tokio::main]

@ -212,6 +212,16 @@ async fn handle_conn(state: Arc<State>, req: Request<Body>) -> Result<Response<B
pub async fn serve(state: State) -> Result<(), eyre::Report> pub async fn serve(state: State) -> Result<(), eyre::Report>
{ {
cfg_debug!(if {
if &state.config != config::get() {
panic!("Our config is not the same as global? This is unsound.");
}
} else {
if &state.config != config::get() {
warn!("Our config is not the same as global? This is unsound.");
}
});
let h = { let h = {
let state = Arc::new(state); let state = Arc::new(state);

Loading…
Cancel
Save