#![cfg_attr(test, allow(deprecated))]
#![cfg_attr(not(any(feature = "std", feature = "std-deps")), no_std)]
extern crate alloc;
#[cfg(not(any(feature = "std", feature = "std-deps")))]
#[doc(hidden)]
pub mod eyre {
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use core::any::Any;
use core::fmt::{self, Debug, Display};
pub type Result<T, E = Report> = core::result::Result<T, E>;
pub type Error = Report;
pub trait EyreHandler: Any + Send + Sync {
fn display(
&self,
error: &(dyn core::error::Error + 'static),
f: &mut fmt::Formatter<'_>,
) -> fmt::Result;
fn debug(
&self,
error: &(dyn core::error::Error + 'static),
f: &mut fmt::Formatter<'_>,
) -> fmt::Result;
fn track_caller(&mut self, _location: &'static core::panic::Location<'static>) {}
}
pub struct DefaultHandler;
impl DefaultHandler {
pub fn default_with(_error: &(dyn core::error::Error + 'static)) -> Box<dyn EyreHandler> {
Box::new(DefaultHandler)
}
}
impl EyreHandler for DefaultHandler {
fn display(
&self,
error: &(dyn core::error::Error + 'static),
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
Display::fmt(error, f)
}
fn debug(
&self,
error: &(dyn core::error::Error + 'static),
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
Debug::fmt(error, f)
}
}
trait ErrorObj: Any + Send + Sync + Display + Debug {
fn as_any(&self) -> &(dyn Any + Send + Sync);
fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync>;
fn as_core_error(&self) -> &(dyn core::error::Error + 'static);
}
struct WrappedError<E>(E);
impl<E: Display> Display for WrappedError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<E: Debug> Debug for WrappedError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl<E: Display + Debug> core::error::Error for WrappedError<E> {}
impl<E: Display + Debug + Send + Sync + 'static> ErrorObj for WrappedError<E> {
fn as_any(&self) -> &(dyn Any + Send + Sync) {
&self.0
}
fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync> {
Box::new(self.0)
}
fn as_core_error(&self) -> &(dyn core::error::Error + 'static) {
self
}
}
pub struct Report {
inner: Box<dyn ErrorObj>,
handler: Box<dyn EyreHandler>,
}
impl Report {
#[track_caller]
pub fn new<E>(error: E) -> Self
where
E: Display + Debug + Send + Sync + 'static,
{
let location = core::panic::Location::caller();
let wrapped = Box::new(WrappedError(error));
let mut handler = DefaultHandler::default_with(wrapped.as_core_error());
handler.track_caller(location);
Self {
inner: wrapped,
handler,
}
}
#[track_caller]
pub fn msg<M>(message: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
{
Self::new(message)
}
pub fn wrap_err<D>(self, msg: D) -> Self
where
D: Display + Send + Sync + 'static,
{
Self::msg(format!("{msg}: {}", self.inner))
}
pub fn handler(&self) -> &(dyn Any + Send + Sync) {
&*self.handler
}
pub fn is<T: Any + Send + Sync + 'static>(&self) -> bool {
self.inner.as_any().is::<T>()
}
pub fn downcast_ref<T: Any + Send + Sync + 'static>(&self) -> Option<&T> {
self.inner.as_any().downcast_ref::<T>()
}
pub fn downcast<T: Any + Send + Sync + 'static>(self) -> core::result::Result<T, Self> {
if self.is::<T>() {
match self.inner.into_any().downcast::<T>() {
Ok(boxed) => Ok(*boxed),
Err(_) => unreachable!(),
}
} else {
Err(self)
}
}
}
impl AsRef<dyn core::error::Error + 'static> for Report {
fn as_ref(&self) -> &(dyn core::error::Error + 'static) {
self.inner.as_core_error()
}
}
impl Display for Report {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.handler.display(self.inner.as_core_error(), f)
}
}
impl Debug for Report {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.handler.debug(self.inner.as_core_error(), f)
}
}
impl<E> From<E> for Report
where
E: core::error::Error + Send + Sync + 'static,
{
#[track_caller]
fn from(error: E) -> Self {
Self::new(error)
}
}
pub trait WrapErr<T, E> {
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static;
fn wrap_err_with<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
Self: Sized,
{
self.wrap_err(msg)
}
fn with_context<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
Self: Sized,
{
self.wrap_err_with(f)
}
}
pub use WrapErr as Context;
impl<T, E> WrapErr<T, E> for core::result::Result<T, E>
where
E: Into<Report>,
{
#[track_caller]
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
{
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.into().wrap_err(msg)),
}
}
#[track_caller]
fn wrap_err_with<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.into().wrap_err(f())),
}
}
}
pub trait ContextCompat<T> {
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static;
fn with_context<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
Self: Sized,
{
self.context(msg)
}
fn wrap_err_with<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
Self: Sized,
{
self.with_context(f)
}
}
impl<T> ContextCompat<T> for Option<T> {
#[track_caller]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
{
match self {
Some(v) => Ok(v),
None => Err(Report::msg(msg.to_string())),
}
}
#[track_caller]
fn with_context<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
match self {
Some(v) => Ok(v),
None => Err(Report::msg(f().to_string())),
}
}
}
pub use crate::{
__endpoint_libs_bail as bail, __endpoint_libs_ensure as ensure,
__endpoint_libs_eyre as eyre,
};
}
#[cfg(not(any(feature = "std", feature = "std-deps")))]
#[doc(hidden)]
#[macro_export]
macro_rules! __endpoint_libs_eyre {
($msg:literal $(,)?) => {
$crate::eyre::Report::msg(::alloc::format!($msg))
};
($err:expr $(,)?) => {
$crate::eyre::Report::msg(::alloc::format!("{}", $err))
};
($fmt:expr, $($arg:tt)*) => {
$crate::eyre::Report::msg(::alloc::format!($fmt, $($arg)*))
};
}
#[cfg(not(any(feature = "std", feature = "std-deps")))]
#[doc(hidden)]
#[macro_export]
macro_rules! __endpoint_libs_bail {
($($tt:tt)*) => {
return ::core::result::Result::Err($crate::__endpoint_libs_eyre!($($tt)*))
};
}
#[cfg(not(any(feature = "std", feature = "std-deps")))]
#[doc(hidden)]
#[macro_export]
macro_rules! __endpoint_libs_ensure {
($cond:expr, $($tt:tt)*) => {
if !($cond) {
$crate::__endpoint_libs_bail!($($tt)*);
}
};
($cond:expr) => {
if !($cond) {
$crate::__endpoint_libs_bail!(concat!("Condition failed: `", stringify!($cond), "`"));
}
};
}
#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub mod convert_case {
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Case {
Snake,
Camel,
Pascal,
}
fn split_words(input: &str) -> Vec<String> {
let mut words = Vec::new();
let mut current = String::new();
let chars: Vec<char> = input.chars().collect();
for i in 0..chars.len() {
let ch = chars[i];
if !ch.is_alphanumeric() {
if !current.is_empty() {
words.push(core::mem::take(&mut current));
}
continue;
}
if i > 0 && ch.is_uppercase() {
let prev = chars[i - 1];
let next_lower = chars.get(i + 1).is_some_and(|n| n.is_lowercase());
if prev.is_lowercase() || prev.is_numeric() || (prev.is_uppercase() && next_lower) {
if !current.is_empty() {
words.push(core::mem::take(&mut current));
}
}
}
current.push(ch);
}
if !current.is_empty() {
words.push(current);
}
words
}
fn capitalize(word: &str) -> String {
let mut chars = word.chars();
match chars.next() {
None => String::new(),
Some(first) => {
let mut out = String::new();
for c in first.to_uppercase() {
out.push(c);
}
for c in chars {
for lc in c.to_lowercase() {
out.push(lc);
}
}
out
}
}
}
fn lowercase(word: &str) -> String {
let mut out = String::new();
for c in word.chars() {
for lc in c.to_lowercase() {
out.push(lc);
}
}
out
}
pub trait Casing {
fn to_case(&self, case: Case) -> String;
}
impl<T: AsRef<str> + ?Sized> Casing for T {
fn to_case(&self, case: Case) -> String {
let words = split_words(self.as_ref());
match case {
Case::Snake => {
let mut out = String::new();
for (i, w) in words.iter().enumerate() {
if i > 0 {
out.push('_');
}
out.push_str(&lowercase(w));
}
out
}
Case::Pascal => {
let mut out = String::new();
for w in &words {
out.push_str(&capitalize(w));
}
out
}
Case::Camel => {
let mut out = String::new();
for (i, w) in words.iter().enumerate() {
if i == 0 {
out.push_str(&lowercase(w));
} else {
out.push_str(&capitalize(w));
}
}
out
}
}
}
}
}
#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub mod urlencoding {
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
#[derive(Debug)]
pub struct FromUrlEncodingError;
impl fmt::Display for FromUrlEncodingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid percent-encoded UTF-8 sequence")
}
}
impl core::error::Error for FromUrlEncodingError {}
pub fn encode(data: &str) -> Cow<'_, str> {
let bytes = data.as_bytes();
let safe = |b: u8| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~');
if bytes.iter().copied().all(safe) {
return Cow::Borrowed(data);
}
const HEX: &[u8; 16] = b"0123456789ABCDEF";
let mut out = String::with_capacity(bytes.len() * 3);
for &b in bytes {
if safe(b) {
out.push(b as char);
} else {
out.push('%');
out.push(HEX[(b >> 4) as usize] as char);
out.push(HEX[(b & 0x0f) as usize] as char);
}
}
Cow::Owned(out)
}
pub fn decode(data: &str) -> Result<Cow<'_, str>, FromUrlEncodingError> {
let bytes = data.as_bytes();
if !bytes.contains(&b'%') {
return Ok(Cow::Borrowed(data));
}
let hex = |b: u8| -> Result<u8, FromUrlEncodingError> {
match b {
b'0'..=b'9' => Ok(b - b'0'),
b'a'..=b'f' => Ok(b - b'a' + 10),
b'A'..=b'F' => Ok(b - b'A' + 10),
_ => Err(FromUrlEncodingError),
}
};
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' {
if i + 2 >= bytes.len() {
return Err(FromUrlEncodingError);
}
out.push((hex(bytes[i + 1])? << 4) | hex(bytes[i + 2])?);
i += 3;
} else {
out.push(bytes[i]);
i += 1;
}
}
String::from_utf8(out)
.map(Cow::Owned)
.map_err(|_| FromUrlEncodingError)
}
}
pub mod libs;
#[cfg(feature = "types")]
pub mod model;