use std::time::Duration;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq)]
pub struct ResolveConfig {
pub timeout: Duration,
pub reset_globals: bool,
pub globals: Globals,
pub tracing: bool,
pub skip_cleanup: bool,
}
impl ResolveConfig {
#[inline]
#[must_use]
pub fn keep_globals() -> Self {
Self {
timeout: Duration::from_secs(30),
reset_globals: false,
globals: Globals::default(),
tracing: false,
skip_cleanup: false,
}
}
}
impl ResolveConfig {
#[inline]
#[must_use]
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
#[inline]
#[must_use]
pub fn reset_globals(mut self, reset_globals: bool) -> Self {
self.reset_globals = reset_globals;
self
}
#[inline]
#[must_use]
pub fn globals(mut self, globals: Globals) -> Self {
self.globals = globals;
self
}
#[inline]
#[must_use]
pub fn tracing(mut self, tracing: bool) -> Self {
self.tracing = tracing;
self
}
#[inline]
#[must_use]
pub fn skip_cleanup(mut self, skip_cleanup: bool) -> Self {
self.skip_cleanup = skip_cleanup;
self
}
}
impl Default for ResolveConfig {
fn default() -> Self {
Self {
timeout: Duration::from_secs(30),
reset_globals: true,
globals: Globals::default(),
tracing: false,
skip_cleanup: false,
}
}
}
#[derive(Clone, PartialEq)]
pub struct Globals {
pub(crate) list: Vec<(String, Vec<u8>)>,
}
impl Globals {
#[inline]
#[must_use]
pub fn new() -> Self {
Self { list: Vec::new() }
}
#[inline]
#[must_use]
pub fn with_capacity(n: usize) -> Self {
Self {
list: Vec::with_capacity(n),
}
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.list.len()
}
pub fn add<T: Serialize, S: Into<String>>(
&mut self,
key: S,
value: &T,
) -> Result<(), rmp_serde::encode::Error> {
self.list.push((key.into(), rmp_serde::to_vec(value)?));
Ok(())
}
}
impl std::fmt::Debug for Globals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Globals")
.field(
"list",
&format!(
"{:?}",
self.list.iter().map(|s| &s.0).collect::<Vec<&String>>()
),
)
.finish()
}
}
impl Default for Globals {
fn default() -> Self {
Self::new()
}
}