#![forbid(unsafe_code)]
use std::{
fmt::{Arguments, Write},
rc::Rc,
sync::Arc,
};
#[cfg(feature = "macros")]
#[doc(hidden)]
pub use avosetta_macros::asx as __asx;
#[cfg(feature = "macros")]
#[macro_export]
macro_rules! asx {
($($tt:tt)*) => {
$crate::__asx!($crate, $($tt)*)
};
}
pub trait Html {
fn write(self, s: &mut String);
#[doc(hidden)]
#[inline]
fn is_none(&self) -> bool {
false
}
#[doc(hidden)]
#[inline]
fn is_false(&self) -> bool {
false
}
#[doc(hidden)]
#[inline]
fn is_true(&self) -> bool {
false
}
}
impl Html for () {
#[inline]
fn write(self, _s: &mut String) {}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Raw<T>(pub T);
impl<T> Html for Raw<T>
where
T: AsRef<str>,
{
#[inline]
fn write(self, s: &mut String) {
s.push_str(self.0.as_ref());
}
}
impl Html for bool {
#[inline]
fn write(self, s: &mut String) {
if self {
s.push_str("true");
} else {
s.push_str("false");
}
}
#[inline]
fn is_false(&self) -> bool {
!*self
}
#[inline]
fn is_true(&self) -> bool {
*self
}
}
impl Html for char {
#[inline]
fn write(self, s: &mut String) {
match self {
'&' => s.push_str("&"),
'<' => s.push_str("<"),
'>' => s.push_str(">"),
'"' => s.push_str("""),
'\'' => s.push_str("'"),
x => s.push(x),
}
}
}
impl<T> Html for Option<T>
where
T: Html,
{
#[inline]
fn write(self, s: &mut String) {
if let Some(x) = self {
x.write(s);
}
}
#[inline]
fn is_none(&self) -> bool {
self.is_none()
}
#[inline]
fn is_false(&self) -> bool {
self.as_ref().is_some_and(|x| x.is_false())
}
#[inline]
fn is_true(&self) -> bool {
self.as_ref().is_some_and(|x| x.is_true())
}
}
impl<T, E> Html for Result<T, E>
where
T: Html,
E: Html,
{
#[inline]
fn write(self, s: &mut String) {
match self {
Ok(x) => x.write(s),
Err(x) => x.write(s),
}
}
#[inline]
fn is_none(&self) -> bool {
match self {
Ok(x) => x.is_none(),
Err(x) => x.is_none(),
}
}
#[inline]
fn is_false(&self) -> bool {
match self {
Ok(x) => x.is_false(),
Err(x) => x.is_false(),
}
}
#[inline]
fn is_true(&self) -> bool {
match self {
Ok(x) => x.is_true(),
Err(x) => x.is_true(),
}
}
}
impl<T> Html for &T
where
T: Html + Copy,
{
#[inline]
fn write(self, s: &mut String) {
(*self).write(s);
}
}
impl<T> Html for &[T]
where
for<'a> &'a T: Html,
{
#[inline]
fn write(self, s: &mut String) {
for x in self {
x.write(s);
}
}
}
macro_rules! impl_owned_iter {
($ty:ty) => {
impl<T> Html for $ty
where
T: Html,
{
#[inline]
fn write(self, s: &mut String) {
for x in self {
x.write(s);
}
}
}
};
}
impl_owned_iter!(Box<[T]>);
impl_owned_iter!(Vec<T>);
impl Html for Arguments<'_> {
fn write(self, s: &mut String) {
struct Writer<'a>(&'a mut String);
impl Write for Writer<'_> {
#[inline]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
s.write(self.0);
Ok(())
}
#[inline]
fn write_char(&mut self, c: char) -> std::fmt::Result {
c.write(self.0);
Ok(())
}
}
match self.as_str() {
Some(x) => x.write(s),
None => {
write!(Writer(s), "{self}").unwrap();
}
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Escape<T>(pub T);
impl<T> Html for Escape<T>
where
T: AsRef<str>,
{
fn write(self, s: &mut String) {
s.reserve(self.0.as_ref().len());
for x in self.0.as_ref().chars() {
match x {
'&' => s.push_str("&"),
'<' => s.push_str("<"),
'>' => s.push_str(">"),
'"' => s.push_str("""),
'\'' => s.push_str("'"),
x => s.push(x),
}
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Attr<K, V>(pub K, pub V);
impl<K, V> Html for Attr<K, V>
where
K: Html,
V: Html,
{
fn write(self, s: &mut String) {
if self.1.is_true() {
let start = s.len();
self.0.write(s);
let end = s.len();
s.push_str("=\"");
s.extend_from_within(start..end);
s.push('\"');
} else if !self.1.is_none() && !self.1.is_false() {
self.0.write(s);
s.push_str("=\"");
self.1.write(s);
s.push('\"');
}
}
}
macro_rules! impl_integer {
($ty:ty) => {
impl Html for $ty {
#[inline]
fn write(self, s: &mut String) {
s.push_str(itoa::Buffer::new().format(self));
}
}
};
}
impl_integer!(usize);
impl_integer!(isize);
impl_integer!(u8);
impl_integer!(i8);
impl_integer!(u16);
impl_integer!(i16);
impl_integer!(u32);
impl_integer!(i32);
impl_integer!(u64);
impl_integer!(i64);
impl_integer!(u128);
impl_integer!(i128);
macro_rules! impl_float {
($ty:ty) => {
impl Html for $ty {
#[inline]
fn write(self, s: &mut String) {
s.push_str(ryu::Buffer::new().format(self));
}
}
};
}
impl_float!(f32);
impl_float!(f64);
macro_rules! impl_string {
($ty:ty) => {
impl Html for $ty {
#[inline]
fn write(self, s: &mut String) {
Escape(self).write(s);
}
}
};
}
impl_string!(&str);
impl_string!(String);
impl_string!(Box<str>);
impl_string!(Rc<str>);
impl_string!(Arc<str>);
#[allow(non_camel_case_types)]
#[doc(hidden)]
#[cfg(feature = "macros")]
pub mod __completion {
pub mod elements {
pub struct html;
pub struct base;
pub struct head;
pub struct link;
pub struct meta;
pub struct style;
pub struct title;
pub struct body;
pub struct address;
pub struct article;
pub struct aside;
pub struct footer;
pub struct header;
pub struct h1;
pub struct h2;
pub struct h3;
pub struct h4;
pub struct h5;
pub struct h6;
pub struct hgroup;
pub struct main;
pub struct nav;
pub struct section;
pub struct search;
pub struct blockquote;
pub struct dd;
pub struct div;
pub struct dl;
pub struct dt;
pub struct figcaption;
pub struct figure;
pub struct hr;
pub struct li;
pub struct menu;
pub struct ol;
pub struct p;
pub struct pre;
pub struct ul;
pub struct a;
pub struct abbr;
pub struct b;
pub struct bdi;
pub struct bdo;
pub struct br;
pub struct cite;
pub struct code;
pub struct data;
pub struct dfn;
pub struct em;
pub struct i;
pub struct kbd;
pub struct mark;
pub struct q;
pub struct rp;
pub struct rt;
pub struct ruby;
pub struct s;
pub struct samp;
pub struct small;
pub struct span;
pub struct strong;
pub struct sub;
pub struct sup;
pub struct time;
pub struct u;
pub struct var;
pub struct wbr;
pub struct area;
pub struct audio;
pub struct img;
pub struct map;
pub struct track;
pub struct video;
pub struct embed;
pub struct fencedframe;
pub struct iframe;
pub struct object;
pub struct picture;
pub struct source;
pub struct svg;
pub struct math;
pub struct canvas;
pub struct noscript;
pub struct script;
pub struct del;
pub struct ins;
pub struct caption;
pub struct col;
pub struct colgroup;
pub struct table;
pub struct tbody;
pub struct td;
pub struct tfoot;
pub struct th;
pub struct thead;
pub struct tr;
pub struct button;
pub struct datalist;
pub struct fieldset;
pub struct form;
pub struct input;
pub struct label;
pub struct legend;
pub struct meter;
pub struct optgroup;
pub struct option;
pub struct output;
pub struct progress;
pub struct select;
pub struct selectedcontent;
pub struct textarea;
pub struct details;
pub struct dialog;
pub struct geolocation;
pub struct summary;
pub struct slot;
pub struct template;
}
pub mod attrs {
pub struct accept;
pub struct accesskey;
pub struct action;
pub struct allow;
pub struct alpha;
pub struct alt;
pub struct autocapitalize;
pub struct autocomplete;
pub struct autocorrect;
pub struct autofocus;
pub struct autoplay;
pub struct capture;
pub struct charset;
pub struct checked;
pub struct cite;
pub struct class;
pub struct colorspace;
pub struct cols;
pub struct colspan;
pub struct content;
pub struct contenteditable;
pub struct controls;
pub struct coords;
pub struct crossorigin;
pub struct csp;
pub struct data;
pub struct datetime;
pub struct decoding;
pub struct default;
pub struct defer;
pub struct dir;
pub struct dirname;
pub struct disabled;
pub struct download;
pub struct draggable;
pub struct elementtiming;
pub struct enctype;
pub struct enterkeyhint;
pub struct exportparts;
pub struct fetchpriority;
pub struct form;
pub struct formaction;
pub struct formenctype;
pub struct formmethod;
pub struct formnovalidate;
pub struct formtarget;
pub struct headers;
pub struct height;
pub struct hidden;
pub struct high;
pub struct href;
pub struct hreflang;
pub struct id;
pub struct inert;
pub struct inputmode;
pub struct integrity;
pub struct is;
pub struct ismap;
pub struct itemid;
pub struct itemprop;
pub struct itemref;
pub struct itemscope;
pub struct itemtype;
pub struct kind;
pub struct label;
pub struct lang;
pub struct list;
pub struct loading;
pub struct low;
pub struct max;
pub struct maxlength;
pub struct media;
pub struct method;
pub struct min;
pub struct minlength;
pub struct multiple;
pub struct muted;
pub struct name;
pub struct nonce;
pub struct novalidate;
pub struct open;
pub struct optimum;
pub struct part;
pub struct pattern;
pub struct ping;
pub struct placeholder;
pub struct playsinline;
pub struct popover;
pub struct poster;
pub struct preload;
pub struct readonly;
pub struct referrerpolicy;
pub struct rel;
pub struct required;
pub struct reversed;
pub struct role;
pub struct rows;
pub struct rowspan;
pub struct sandbox;
pub struct scope;
pub struct selected;
pub struct shape;
pub struct size;
pub struct sizes;
pub struct slot;
pub struct span;
pub struct spellcheck;
pub struct src;
pub struct srcdoc;
pub struct srclang;
pub struct srcset;
pub struct start;
pub struct step;
pub struct style;
pub struct tabindex;
pub struct target;
pub struct title;
pub struct translate;
pub struct usemap;
pub struct value;
pub struct virtualkeyboardpolicy;
pub struct width;
pub struct wrap;
pub struct writingsuggestions;
pub struct onabort;
pub struct onanimationcancel;
pub struct onanimationend;
pub struct onanimationiteration;
pub struct onanimationstart;
pub struct onauxclick;
pub struct onbeforeinput;
pub struct onbeforematch;
pub struct onbeforetoggle;
pub struct onblur;
pub struct oncancel;
pub struct oncanplay;
pub struct oncanplaythrough;
pub struct onchange;
pub struct onclick;
pub struct onclose;
pub struct oncommand;
pub struct oncontentvisibilityautostatechange;
pub struct oncontextlost;
pub struct oncontextmenu;
pub struct oncontextrestored;
pub struct oncopy;
pub struct oncuechange;
pub struct oncut;
pub struct ondblclick;
pub struct ondrag;
pub struct ondragend;
pub struct ondragenter;
pub struct ondragleave;
pub struct ondragover;
pub struct ondragstart;
pub struct ondrop;
pub struct ondurationchange;
pub struct onemptied;
pub struct onended;
pub struct onerror;
pub struct onfocus;
pub struct onfocusin;
pub struct onfocusout;
pub struct onformdata;
pub struct onfullscreenchange;
pub struct onfullscreenerror;
pub struct ongotpointercapture;
pub struct oninput;
pub struct oninvalid;
pub struct onkeydown;
pub struct onkeyup;
pub struct onload;
pub struct onloadeddata;
pub struct onloadedmetadata;
pub struct onloadstart;
pub struct onlostpointercapture;
pub struct onmousedown;
pub struct onmouseenter;
pub struct onmouseleave;
pub struct onmousemove;
pub struct onmouseout;
pub struct onmouseover;
pub struct onmouseup;
pub struct onpaste;
pub struct onpause;
pub struct onplay;
pub struct onplaying;
pub struct onpointercancel;
pub struct onpointerdown;
pub struct onpointerenter;
pub struct onpointerleave;
pub struct onpointermove;
pub struct onpointerout;
pub struct onpointerover;
pub struct onpointerrawupdate;
pub struct onpointerup;
pub struct onprogress;
pub struct onratechange;
pub struct onreset;
pub struct onresize;
pub struct onscroll;
pub struct onscrollend;
pub struct onscrollsnapchange;
pub struct onscrollsnapchanging;
pub struct onsecuritypolicyviolation;
pub struct onseeked;
pub struct onseeking;
pub struct onselect;
pub struct onselectionchange;
pub struct onselectstart;
pub struct onslotchange;
pub struct onstalled;
pub struct onsubmit;
pub struct onsuspend;
pub struct ontimeupdate;
pub struct ontoggle;
pub struct ontouchcancel;
pub struct ontouchend;
pub struct ontouchmove;
pub struct ontouchstart;
pub struct ontransitioncancel;
pub struct ontransitionend;
pub struct ontransitionrun;
pub struct ontransitionstart;
pub struct onvolumechange;
pub struct onwaiting;
pub struct onwheel;
}
}