Vendor things
This commit is contained in:
parent
5deceec006
commit
977e3c17e5
19434 changed files with 10682014 additions and 0 deletions
125
third-party/vendor/log/src/__private_api.rs
vendored
Normal file
125
third-party/vendor/log/src/__private_api.rs
vendored
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
//! WARNING: this is not part of the crate's public API and is subject to change at any time
|
||||
|
||||
use self::sealed::KVs;
|
||||
use crate::{Level, Metadata, Record};
|
||||
use std::fmt::Arguments;
|
||||
pub use std::{file, format_args, line, module_path, stringify};
|
||||
|
||||
#[cfg(not(feature = "kv"))]
|
||||
pub type Value<'a> = &'a str;
|
||||
|
||||
mod sealed {
|
||||
/// Types for the `kv` argument.
|
||||
pub trait KVs<'a> {
|
||||
fn into_kvs(self) -> Option<&'a [(&'a str, super::Value<'a>)]>;
|
||||
}
|
||||
}
|
||||
|
||||
// Types for the `kv` argument.
|
||||
|
||||
impl<'a> KVs<'a> for &'a [(&'a str, Value<'a>)] {
|
||||
#[inline]
|
||||
fn into_kvs(self) -> Option<&'a [(&'a str, Value<'a>)]> {
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> KVs<'a> for () {
|
||||
#[inline]
|
||||
fn into_kvs(self) -> Option<&'a [(&'a str, Value<'a>)]> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// Log implementation.
|
||||
|
||||
fn log_impl(
|
||||
args: Arguments,
|
||||
level: Level,
|
||||
&(target, module_path, file): &(&str, &'static str, &'static str),
|
||||
line: u32,
|
||||
kvs: Option<&[(&str, Value)]>,
|
||||
) {
|
||||
#[cfg(not(feature = "kv"))]
|
||||
if kvs.is_some() {
|
||||
panic!("key-value support is experimental and must be enabled using the `kv` feature")
|
||||
}
|
||||
|
||||
let mut builder = Record::builder();
|
||||
|
||||
builder
|
||||
.args(args)
|
||||
.level(level)
|
||||
.target(target)
|
||||
.module_path_static(Some(module_path))
|
||||
.file_static(Some(file))
|
||||
.line(Some(line));
|
||||
|
||||
#[cfg(feature = "kv")]
|
||||
builder.key_values(&kvs);
|
||||
|
||||
crate::logger().log(&builder.build());
|
||||
}
|
||||
|
||||
pub fn log<'a, K>(
|
||||
args: Arguments,
|
||||
level: Level,
|
||||
target_module_path_and_file: &(&str, &'static str, &'static str),
|
||||
line: u32,
|
||||
kvs: K,
|
||||
) where
|
||||
K: KVs<'a>,
|
||||
{
|
||||
log_impl(
|
||||
args,
|
||||
level,
|
||||
target_module_path_and_file,
|
||||
line,
|
||||
kvs.into_kvs(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn enabled(level: Level, target: &str) -> bool {
|
||||
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
|
||||
}
|
||||
|
||||
#[cfg(feature = "kv")]
|
||||
mod kv_support {
|
||||
use crate::kv;
|
||||
|
||||
pub type Value<'a> = kv::Value<'a>;
|
||||
|
||||
// NOTE: Many functions here accept a double reference &&V
|
||||
// This is so V itself can be ?Sized, while still letting us
|
||||
// erase it to some dyn Trait (because &T is sized)
|
||||
|
||||
pub fn capture_to_value<'a, V: kv::ToValue + ?Sized>(v: &'a &'a V) -> Value<'a> {
|
||||
v.to_value()
|
||||
}
|
||||
|
||||
pub fn capture_debug<'a, V: core::fmt::Debug + ?Sized>(v: &'a &'a V) -> Value<'a> {
|
||||
Value::from_debug(v)
|
||||
}
|
||||
|
||||
pub fn capture_display<'a, V: core::fmt::Display + ?Sized>(v: &'a &'a V) -> Value<'a> {
|
||||
Value::from_display(v)
|
||||
}
|
||||
|
||||
#[cfg(feature = "kv_std")]
|
||||
pub fn capture_error<'a>(v: &'a (dyn std::error::Error + 'static)) -> Value<'a> {
|
||||
Value::from_dyn_error(v)
|
||||
}
|
||||
|
||||
#[cfg(feature = "kv_sval")]
|
||||
pub fn capture_sval<'a, V: sval::Value + ?Sized>(v: &'a &'a V) -> Value<'a> {
|
||||
Value::from_sval(v)
|
||||
}
|
||||
|
||||
#[cfg(feature = "kv_serde")]
|
||||
pub fn capture_serde<'a, V: serde::Serialize + ?Sized>(v: &'a &'a V) -> Value<'a> {
|
||||
Value::from_serde(v)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "kv")]
|
||||
pub use self::kv_support::*;
|
||||
94
third-party/vendor/log/src/kv/error.rs
vendored
Normal file
94
third-party/vendor/log/src/kv/error.rs
vendored
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
use std::fmt;
|
||||
|
||||
/// An error encountered while working with structured data.
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
inner: Inner,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Inner {
|
||||
#[cfg(feature = "std")]
|
||||
Boxed(std_support::BoxedError),
|
||||
Msg(&'static str),
|
||||
#[cfg(feature = "value-bag")]
|
||||
Value(crate::kv::value::inner::Error),
|
||||
Fmt,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Create an error from a message.
|
||||
pub fn msg(msg: &'static str) -> Self {
|
||||
Error {
|
||||
inner: Inner::Msg(msg),
|
||||
}
|
||||
}
|
||||
|
||||
// Not public so we don't leak the `crate::kv::value::inner` API
|
||||
#[cfg(feature = "value-bag")]
|
||||
pub(super) fn from_value(err: crate::kv::value::inner::Error) -> Self {
|
||||
Error {
|
||||
inner: Inner::Value(err),
|
||||
}
|
||||
}
|
||||
|
||||
// Not public so we don't leak the `crate::kv::value::inner` API
|
||||
#[cfg(feature = "value-bag")]
|
||||
pub(super) fn into_value(self) -> crate::kv::value::inner::Error {
|
||||
match self.inner {
|
||||
Inner::Value(err) => err,
|
||||
#[cfg(feature = "kv_std")]
|
||||
_ => crate::kv::value::inner::Error::boxed(self),
|
||||
#[cfg(not(feature = "kv_std"))]
|
||||
_ => crate::kv::value::inner::Error::msg("error inspecting a value"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::Inner::*;
|
||||
match &self.inner {
|
||||
#[cfg(feature = "std")]
|
||||
Boxed(err) => err.fmt(f),
|
||||
#[cfg(feature = "value-bag")]
|
||||
Value(err) => err.fmt(f),
|
||||
Msg(msg) => msg.fmt(f),
|
||||
Fmt => fmt::Error.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fmt::Error> for Error {
|
||||
fn from(_: fmt::Error) -> Self {
|
||||
Error { inner: Inner::Fmt }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod std_support {
|
||||
use super::*;
|
||||
use std::{error, io};
|
||||
|
||||
pub(super) type BoxedError = Box<dyn error::Error + Send + Sync>;
|
||||
|
||||
impl Error {
|
||||
/// Create an error from a standard error type.
|
||||
pub fn boxed<E>(err: E) -> Self
|
||||
where
|
||||
E: Into<BoxedError>,
|
||||
{
|
||||
Error {
|
||||
inner: Inner::Boxed(err.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(err: io::Error) -> Self {
|
||||
Error::boxed(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
143
third-party/vendor/log/src/kv/key.rs
vendored
Normal file
143
third-party/vendor/log/src/kv/key.rs
vendored
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
//! Structured keys.
|
||||
|
||||
use std::borrow::Borrow;
|
||||
use std::fmt;
|
||||
|
||||
/// A type that can be converted into a [`Key`](struct.Key.html).
|
||||
pub trait ToKey {
|
||||
/// Perform the conversion.
|
||||
fn to_key(&self) -> Key;
|
||||
}
|
||||
|
||||
impl<'a, T> ToKey for &'a T
|
||||
where
|
||||
T: ToKey + ?Sized,
|
||||
{
|
||||
fn to_key(&self) -> Key {
|
||||
(**self).to_key()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'k> ToKey for Key<'k> {
|
||||
fn to_key(&self) -> Key {
|
||||
Key { key: self.key }
|
||||
}
|
||||
}
|
||||
|
||||
impl ToKey for str {
|
||||
fn to_key(&self) -> Key {
|
||||
Key::from_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// A key in a key-value.
|
||||
// These impls must only be based on the as_str() representation of the key
|
||||
// If a new field (such as an optional index) is added to the key they must not affect comparison
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Key<'k> {
|
||||
key: &'k str,
|
||||
}
|
||||
|
||||
impl<'k> Key<'k> {
|
||||
/// Get a key from a borrowed string.
|
||||
pub fn from_str(key: &'k str) -> Self {
|
||||
Key { key }
|
||||
}
|
||||
|
||||
/// Get a borrowed string from this key.
|
||||
pub fn as_str(&self) -> &str {
|
||||
self.key
|
||||
}
|
||||
}
|
||||
|
||||
impl<'k> fmt::Display for Key<'k> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.key.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'k> AsRef<str> for Key<'k> {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'k> Borrow<str> for Key<'k> {
|
||||
fn borrow(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'k> From<&'k str> for Key<'k> {
|
||||
fn from(s: &'k str) -> Self {
|
||||
Key::from_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod std_support {
|
||||
use super::*;
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
impl ToKey for String {
|
||||
fn to_key(&self) -> Key {
|
||||
Key::from_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToKey for Cow<'a, str> {
|
||||
fn to_key(&self) -> Key {
|
||||
Key::from_str(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "kv_sval")]
|
||||
mod sval_support {
|
||||
use super::*;
|
||||
|
||||
use sval::Value;
|
||||
use sval_ref::ValueRef;
|
||||
|
||||
impl<'a> Value for Key<'a> {
|
||||
fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(
|
||||
&'sval self,
|
||||
stream: &mut S,
|
||||
) -> sval::Result {
|
||||
self.key.stream(stream)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ValueRef<'a> for Key<'a> {
|
||||
fn stream_ref<S: sval::Stream<'a> + ?Sized>(&self, stream: &mut S) -> sval::Result {
|
||||
self.key.stream(stream)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "kv_serde")]
|
||||
mod serde_support {
|
||||
use super::*;
|
||||
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
impl<'a> Serialize for Key<'a> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
self.key.serialize(serializer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn key_from_string() {
|
||||
assert_eq!("a key", Key::from_str("a key").as_str());
|
||||
}
|
||||
}
|
||||
265
third-party/vendor/log/src/kv/mod.rs
vendored
Normal file
265
third-party/vendor/log/src/kv/mod.rs
vendored
Normal file
|
|
@ -0,0 +1,265 @@
|
|||
//! Structured logging.
|
||||
//!
|
||||
//! Add the `kv` feature to your `Cargo.toml` to enable
|
||||
//! this module:
|
||||
//!
|
||||
//! ```toml
|
||||
//! [dependencies.log]
|
||||
//! features = ["kv"]
|
||||
//! ```
|
||||
//!
|
||||
//! # Structured logging in `log`
|
||||
//!
|
||||
//! Structured logging enhances traditional text-based log records with user-defined
|
||||
//! attributes. Structured logs can be analyzed using a variety of data processing
|
||||
//! techniques, without needing to find and parse attributes from unstructured text first.
|
||||
//!
|
||||
//! In `log`, user-defined attributes are part of a [`Source`] on the log record.
|
||||
//! Each attribute is a key-value; a pair of [`Key`] and [`Value`]. Keys are strings
|
||||
//! and values are a datum of any type that can be formatted or serialized. Simple types
|
||||
//! like strings, booleans, and numbers are supported, as well as arbitrarily complex
|
||||
//! structures involving nested objects and sequences.
|
||||
//!
|
||||
//! ## Adding key-values to log records
|
||||
//!
|
||||
//! Key-values appear before the message format in the `log!` macros:
|
||||
//!
|
||||
//! ```
|
||||
//! # use log::info;
|
||||
//! info!(a = 1; "Something of interest");
|
||||
//! ```
|
||||
//!
|
||||
//! Key-values support the same shorthand identifer syntax as `format_args`:
|
||||
//!
|
||||
//! ```
|
||||
//! # use log::info;
|
||||
//! let a = 1;
|
||||
//!
|
||||
//! info!(a; "Something of interest");
|
||||
//! ```
|
||||
//!
|
||||
//! Values are capturing using the [`ToValue`] trait by default. To capture a value
|
||||
//! using a different trait implementation, use a modifier after its key. Here's how
|
||||
//! the same example can capture `a` using its `Debug` implementation instead:
|
||||
//!
|
||||
//! ```
|
||||
//! # use log::info;
|
||||
//! info!(a:? = 1; "Something of interest");
|
||||
//! ```
|
||||
//!
|
||||
//! The following capturing modifiers are supported:
|
||||
//!
|
||||
//! - `:?` will capture the value using `Debug`.
|
||||
//! - `:debug` will capture the value using `Debug`.
|
||||
//! - `:%` will capture the value using `Display`.
|
||||
//! - `:display` will capture the value using `Display`.
|
||||
//! - `:err` will capture the value using `std::error::Error` (requires the `kv_std` feature).
|
||||
//! - `:sval` will capture the value using `sval::Value` (requires the `kv_sval` feature).
|
||||
//! - `:serde` will capture the value using `serde::Serialize` (requires the `kv_serde` feature).
|
||||
//!
|
||||
//! ## Working with key-values on log records
|
||||
//!
|
||||
//! Use the [`Record::key_values`](../struct.Record.html#method.key_values) method to access key-values.
|
||||
//!
|
||||
//! Individual values can be pulled from the source by their key:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), log::kv::Error> {
|
||||
//! use log::kv::{Source, Key, Value};
|
||||
//! # let record = log::Record::builder().key_values(&[("a", 1)]).build();
|
||||
//!
|
||||
//! // info!(a = 1; "Something of interest");
|
||||
//!
|
||||
//! let a: Value = record.key_values().get(Key::from("a")).unwrap();
|
||||
//! assert_eq!(1, a.to_i64().unwrap());
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! All key-values can also be enumerated using a [`VisitSource`]:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), log::kv::Error> {
|
||||
//! use std::collections::BTreeMap;
|
||||
//!
|
||||
//! use log::kv::{self, Source, Key, Value, VisitSource};
|
||||
//!
|
||||
//! struct Collect<'kvs>(BTreeMap<Key<'kvs>, Value<'kvs>>);
|
||||
//!
|
||||
//! impl<'kvs> VisitSource<'kvs> for Collect<'kvs> {
|
||||
//! fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), kv::Error> {
|
||||
//! self.0.insert(key, value);
|
||||
//!
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! let mut visitor = Collect(BTreeMap::new());
|
||||
//!
|
||||
//! # let record = log::Record::builder().key_values(&[("a", 1), ("b", 2), ("c", 3)]).build();
|
||||
//! // info!(a = 1, b = 2, c = 3; "Something of interest");
|
||||
//!
|
||||
//! record.key_values().visit(&mut visitor)?;
|
||||
//!
|
||||
//! let collected = visitor.0;
|
||||
//!
|
||||
//! assert_eq!(
|
||||
//! vec!["a", "b", "c"],
|
||||
//! collected
|
||||
//! .keys()
|
||||
//! .map(|k| k.as_str())
|
||||
//! .collect::<Vec<_>>(),
|
||||
//! );
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! [`Value`]s have methods for conversions to common types:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), log::kv::Error> {
|
||||
//! use log::kv::{Source, Key};
|
||||
//! # let record = log::Record::builder().key_values(&[("a", 1)]).build();
|
||||
//!
|
||||
//! // info!(a = 1; "Something of interest");
|
||||
//!
|
||||
//! let a = record.key_values().get(Key::from("a")).unwrap();
|
||||
//!
|
||||
//! assert_eq!(1, a.to_i64().unwrap());
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! Values also have their own [`VisitValue`] type. Value visitors are a lightweight
|
||||
//! API for working with primitives types:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), log::kv::Error> {
|
||||
//! use log::kv::{self, Source, Key, VisitValue};
|
||||
//! # let record = log::Record::builder().key_values(&[("a", 1)]).build();
|
||||
//!
|
||||
//! struct IsNumeric(bool);
|
||||
//!
|
||||
//! impl<'kvs> VisitValue<'kvs> for IsNumeric {
|
||||
//! fn visit_any(&mut self, _value: kv::Value) -> Result<(), kv::Error> {
|
||||
//! self.0 = false;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! fn visit_u64(&mut self, _value: u64) -> Result<(), kv::Error> {
|
||||
//! self.0 = true;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! fn visit_i64(&mut self, _value: i64) -> Result<(), kv::Error> {
|
||||
//! self.0 = true;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! fn visit_u128(&mut self, _value: u128) -> Result<(), kv::Error> {
|
||||
//! self.0 = true;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! fn visit_i128(&mut self, _value: i128) -> Result<(), kv::Error> {
|
||||
//! self.0 = true;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! fn visit_f64(&mut self, _value: f64) -> Result<(), kv::Error> {
|
||||
//! self.0 = true;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! // info!(a = 1; "Something of interest");
|
||||
//!
|
||||
//! let a = record.key_values().get(Key::from("a")).unwrap();
|
||||
//!
|
||||
//! let mut visitor = IsNumeric(false);
|
||||
//!
|
||||
//! a.visit(&mut visitor)?;
|
||||
//!
|
||||
//! let is_numeric = visitor.0;
|
||||
//!
|
||||
//! assert!(is_numeric);
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! To serialize a value to a format like JSON, you can also use either `serde` or `sval`:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! # #[cfg(feature = "serde")]
|
||||
//! # {
|
||||
//! # use log::kv::Key;
|
||||
//! #[derive(serde::Serialize)]
|
||||
//! struct Data {
|
||||
//! a: i32, b: bool,
|
||||
//! c: &'static str,
|
||||
//! }
|
||||
//!
|
||||
//! let data = Data { a: 1, b: true, c: "Some data" };
|
||||
//!
|
||||
//! # let source = [("a", log::kv::Value::from_serde(&data))];
|
||||
//! # let record = log::Record::builder().key_values(&source).build();
|
||||
//! // info!(a = data; "Something of interest");
|
||||
//!
|
||||
//! let a = record.key_values().get(Key::from("a")).unwrap();
|
||||
//!
|
||||
//! assert_eq!("{\"a\":1,\"b\":true,\"c\":\"Some data\"}", serde_json::to_string(&a)?);
|
||||
//! # }
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! The choice of serialization framework depends on the needs of the consumer.
|
||||
//! If you're in a no-std environment, you can use `sval`. In other cases, you can use `serde`.
|
||||
//! Log producers and log consumers don't need to agree on the serialization framework.
|
||||
//! A value can be captured using its `serde::Serialize` implementation and still be serialized
|
||||
//! through `sval` without losing any structure or data.
|
||||
//!
|
||||
//! Values can also always be formatted using the standard `Debug` and `Display`
|
||||
//! traits:
|
||||
//!
|
||||
//! ```
|
||||
//! # use log::kv::Key;
|
||||
//! # #[derive(Debug)]
|
||||
//! struct Data {
|
||||
//! a: i32,
|
||||
//! b: bool,
|
||||
//! c: &'static str,
|
||||
//! }
|
||||
//!
|
||||
//! let data = Data { a: 1, b: true, c: "Some data" };
|
||||
//!
|
||||
//! # let source = [("a", log::kv::Value::from_debug(&data))];
|
||||
//! # let record = log::Record::builder().key_values(&source).build();
|
||||
//! // info!(a = data; "Something of interest");
|
||||
//!
|
||||
//! let a = record.key_values().get(Key::from("a")).unwrap();
|
||||
//!
|
||||
//! assert_eq!("Data { a: 1, b: true, c: \"Some data\" }", format!("{a:?}"));
|
||||
//! ```
|
||||
|
||||
mod error;
|
||||
mod key;
|
||||
|
||||
#[cfg(not(feature = "kv_unstable"))]
|
||||
mod source;
|
||||
#[cfg(not(feature = "kv_unstable"))]
|
||||
mod value;
|
||||
|
||||
pub use self::error::Error;
|
||||
pub use self::key::{Key, ToKey};
|
||||
pub use self::source::{Source, VisitSource};
|
||||
pub use self::value::{ToValue, Value, VisitValue};
|
||||
|
||||
#[cfg(feature = "kv_unstable")]
|
||||
pub mod source;
|
||||
#[cfg(feature = "kv_unstable")]
|
||||
pub mod value;
|
||||
|
||||
#[cfg(feature = "kv_unstable")]
|
||||
pub use self::source::Visitor;
|
||||
516
third-party/vendor/log/src/kv/source.rs
vendored
Normal file
516
third-party/vendor/log/src/kv/source.rs
vendored
Normal file
|
|
@ -0,0 +1,516 @@
|
|||
//! Sources for key-values.
|
||||
//!
|
||||
//! This module defines the [`Source`] type and supporting APIs for
|
||||
//! working with collections of key-values.
|
||||
|
||||
use crate::kv::{Error, Key, ToKey, ToValue, Value};
|
||||
use std::fmt;
|
||||
|
||||
/// A source of key-values.
|
||||
///
|
||||
/// The source may be a single pair, a set of pairs, or a filter over a set of pairs.
|
||||
/// Use the [`VisitSource`](trait.VisitSource.html) trait to inspect the structured data
|
||||
/// in a source.
|
||||
///
|
||||
/// A source is like an iterator over its key-values, except with a push-based API
|
||||
/// instead of a pull-based one.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Enumerating the key-values in a source:
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), log::kv::Error> {
|
||||
/// use log::kv::{self, Source, Key, Value, VisitSource};
|
||||
///
|
||||
/// // A `VisitSource` that prints all key-values
|
||||
/// // VisitSources are fed the key-value pairs of each key-values
|
||||
/// struct Printer;
|
||||
///
|
||||
/// impl<'kvs> VisitSource<'kvs> for Printer {
|
||||
/// fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), kv::Error> {
|
||||
/// println!("{key}: {value}");
|
||||
///
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // A source with 3 key-values
|
||||
/// // Common collection types implement the `Source` trait
|
||||
/// let source = &[
|
||||
/// ("a", 1),
|
||||
/// ("b", 2),
|
||||
/// ("c", 3),
|
||||
/// ];
|
||||
///
|
||||
/// // Pass an instance of the `VisitSource` to a `Source` to visit it
|
||||
/// source.visit(&mut Printer)?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub trait Source {
|
||||
/// Visit key-values.
|
||||
///
|
||||
/// A source doesn't have to guarantee any ordering or uniqueness of key-values.
|
||||
/// If the given visitor returns an error then the source may early-return with it,
|
||||
/// even if there are more key-values.
|
||||
///
|
||||
/// # Implementation notes
|
||||
///
|
||||
/// A source should yield the same key-values to a subsequent visitor unless
|
||||
/// that visitor itself fails.
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error>;
|
||||
|
||||
/// Get the value for a given key.
|
||||
///
|
||||
/// If the key appears multiple times in the source then which key is returned
|
||||
/// is implementation specific.
|
||||
///
|
||||
/// # Implementation notes
|
||||
///
|
||||
/// A source that can provide a more efficient implementation of this method
|
||||
/// should override it.
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
get_default(self, key)
|
||||
}
|
||||
|
||||
/// Count the number of key-values that can be visited.
|
||||
///
|
||||
/// # Implementation notes
|
||||
///
|
||||
/// A source that knows the number of key-values upfront may provide a more
|
||||
/// efficient implementation.
|
||||
///
|
||||
/// A subsequent call to `visit` should yield the same number of key-values.
|
||||
fn count(&self) -> usize {
|
||||
count_default(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// The default implementation of `Source::get`
|
||||
fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value<'v>> {
|
||||
struct Get<'k, 'v> {
|
||||
key: Key<'k>,
|
||||
found: Option<Value<'v>>,
|
||||
}
|
||||
|
||||
impl<'k, 'kvs> VisitSource<'kvs> for Get<'k, 'kvs> {
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
|
||||
if self.key == key {
|
||||
self.found = Some(value);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let mut get = Get { key, found: None };
|
||||
|
||||
let _ = source.visit(&mut get);
|
||||
get.found
|
||||
}
|
||||
|
||||
/// The default implementation of `Source::count`.
|
||||
fn count_default(source: impl Source) -> usize {
|
||||
struct Count(usize);
|
||||
|
||||
impl<'kvs> VisitSource<'kvs> for Count {
|
||||
fn visit_pair(&mut self, _: Key<'kvs>, _: Value<'kvs>) -> Result<(), Error> {
|
||||
self.0 += 1;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let mut count = Count(0);
|
||||
let _ = source.visit(&mut count);
|
||||
count.0
|
||||
}
|
||||
|
||||
impl<'a, T> Source for &'a T
|
||||
where
|
||||
T: Source + ?Sized,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
Source::visit(&**self, visitor)
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
Source::get(&**self, key)
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
Source::count(&**self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Source for (K, V)
|
||||
where
|
||||
K: ToKey,
|
||||
V: ToValue,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
visitor.visit_pair(self.0.to_key(), self.1.to_value())
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
if self.0.to_key() == key {
|
||||
Some(self.1.to_value())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for [S]
|
||||
where
|
||||
S: Source,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
for source in self {
|
||||
source.visit(visitor)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
for source in self {
|
||||
if let Some(found) = source.get(key.clone()) {
|
||||
return Some(found);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
self.iter().map(Source::count).sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, S> Source for [S; N]
|
||||
where
|
||||
S: Source,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
Source::visit(self as &[_], visitor)
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
Source::get(self as &[_], key)
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
Source::count(self as &[_])
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for Option<S>
|
||||
where
|
||||
S: Source,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
if let Some(source) = self {
|
||||
source.visit(visitor)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
self.as_ref().and_then(|s| s.get(key))
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
self.as_ref().map_or(0, Source::count)
|
||||
}
|
||||
}
|
||||
|
||||
/// A visitor for the key-value pairs in a [`Source`](trait.Source.html).
|
||||
pub trait VisitSource<'kvs> {
|
||||
/// Visit a key-value pair.
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
impl<'a, 'kvs, T> VisitSource<'kvs> for &'a mut T
|
||||
where
|
||||
T: VisitSource<'kvs> + ?Sized,
|
||||
{
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
|
||||
(**self).visit_pair(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugMap<'a, 'b> {
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
|
||||
self.entry(&key, &value);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugList<'a, 'b> {
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
|
||||
self.entry(&(key, value));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugSet<'a, 'b> {
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
|
||||
self.entry(&(key, value));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugTuple<'a, 'b> {
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
|
||||
self.field(&key);
|
||||
self.field(&value);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod std_support {
|
||||
use super::*;
|
||||
use std::borrow::Borrow;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::hash::{BuildHasher, Hash};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
impl<S> Source for Box<S>
|
||||
where
|
||||
S: Source + ?Sized,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
Source::visit(&**self, visitor)
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
Source::get(&**self, key)
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
Source::count(&**self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for Arc<S>
|
||||
where
|
||||
S: Source + ?Sized,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
Source::visit(&**self, visitor)
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
Source::get(&**self, key)
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
Source::count(&**self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for Rc<S>
|
||||
where
|
||||
S: Source + ?Sized,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
Source::visit(&**self, visitor)
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
Source::get(&**self, key)
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
Source::count(&**self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Source for Vec<S>
|
||||
where
|
||||
S: Source,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
Source::visit(&**self, visitor)
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
Source::get(&**self, key)
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
Source::count(&**self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'kvs, V> VisitSource<'kvs> for Box<V>
|
||||
where
|
||||
V: VisitSource<'kvs> + ?Sized,
|
||||
{
|
||||
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
|
||||
(**self).visit_pair(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Source for HashMap<K, V, S>
|
||||
where
|
||||
K: ToKey + Borrow<str> + Eq + Hash,
|
||||
V: ToValue,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
for (key, value) in self {
|
||||
visitor.visit_pair(key.to_key(), value.to_value())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
HashMap::get(self, key.as_str()).map(|v| v.to_value())
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Source for BTreeMap<K, V>
|
||||
where
|
||||
K: ToKey + Borrow<str> + Ord,
|
||||
V: ToValue,
|
||||
{
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
for (key, value) in self {
|
||||
visitor.visit_pair(key.to_key(), value.to_value())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, key: Key) -> Option<Value<'_>> {
|
||||
BTreeMap::get(self, key.as_str()).map(|v| v.to_value())
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
use crate::kv::value;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn count() {
|
||||
assert_eq!(1, Source::count(&Box::new(("a", 1))));
|
||||
assert_eq!(2, Source::count(&vec![("a", 1), ("b", 2)]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get() {
|
||||
let source = vec![("a", 1), ("b", 2), ("a", 1)];
|
||||
assert_eq!(
|
||||
value::inner::Token::I64(1),
|
||||
Source::get(&source, Key::from_str("a")).unwrap().to_token()
|
||||
);
|
||||
|
||||
let source = Box::new(None::<(&str, i32)>);
|
||||
assert!(Source::get(&source, Key::from_str("a")).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_map() {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("a", 1);
|
||||
map.insert("b", 2);
|
||||
|
||||
assert_eq!(2, Source::count(&map));
|
||||
assert_eq!(
|
||||
value::inner::Token::I64(1),
|
||||
Source::get(&map, Key::from_str("a")).unwrap().to_token()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn btree_map() {
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert("a", 1);
|
||||
map.insert("b", 2);
|
||||
|
||||
assert_eq!(2, Source::count(&map));
|
||||
assert_eq!(
|
||||
value::inner::Token::I64(1),
|
||||
Source::get(&map, Key::from_str("a")).unwrap().to_token()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: Deprecated; but aliases can't carry this attribute
|
||||
#[cfg(feature = "kv_unstable")]
|
||||
pub use VisitSource as Visitor;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::kv::value;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn source_is_object_safe() {
|
||||
fn _check(_: &dyn Source) {}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn visitor_is_object_safe() {
|
||||
fn _check(_: &dyn VisitSource) {}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count() {
|
||||
struct OnePair {
|
||||
key: &'static str,
|
||||
value: i32,
|
||||
}
|
||||
|
||||
impl Source for OnePair {
|
||||
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
|
||||
visitor.visit_pair(self.key.to_key(), self.value.to_value())
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(1, Source::count(&("a", 1)));
|
||||
assert_eq!(2, Source::count(&[("a", 1), ("b", 2)] as &[_]));
|
||||
assert_eq!(0, Source::count(&None::<(&str, i32)>));
|
||||
assert_eq!(1, Source::count(&OnePair { key: "a", value: 1 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get() {
|
||||
let source = &[("a", 1), ("b", 2), ("a", 1)] as &[_];
|
||||
assert_eq!(
|
||||
value::inner::Token::I64(1),
|
||||
Source::get(source, Key::from_str("a")).unwrap().to_token()
|
||||
);
|
||||
assert_eq!(
|
||||
value::inner::Token::I64(2),
|
||||
Source::get(source, Key::from_str("b")).unwrap().to_token()
|
||||
);
|
||||
assert!(Source::get(&source, Key::from_str("c")).is_none());
|
||||
|
||||
let source = None::<(&str, i32)>;
|
||||
assert!(Source::get(&source, Key::from_str("a")).is_none());
|
||||
}
|
||||
}
|
||||
1394
third-party/vendor/log/src/kv/value.rs
vendored
Normal file
1394
third-party/vendor/log/src/kv/value.rs
vendored
Normal file
File diff suppressed because it is too large
Load diff
1839
third-party/vendor/log/src/lib.rs
vendored
Normal file
1839
third-party/vendor/log/src/lib.rs
vendored
Normal file
File diff suppressed because it is too large
Load diff
369
third-party/vendor/log/src/macros.rs
vendored
Normal file
369
third-party/vendor/log/src/macros.rs
vendored
Normal file
|
|
@ -0,0 +1,369 @@
|
|||
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
/// The standard logging macro.
|
||||
///
|
||||
/// This macro will generically log with the specified `Level` and `format!`
|
||||
/// based argument list.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use log::{log, Level};
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let data = (42, "Forty-two");
|
||||
/// let private_data = "private";
|
||||
///
|
||||
/// log!(Level::Error, "Received errors: {}, {}", data.0, data.1);
|
||||
/// log!(target: "app_events", Level::Warn, "App warning: {}, {}, {}",
|
||||
/// data.0, data.1, private_data);
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! log {
|
||||
// log!(target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log");
|
||||
(target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? $(= $value:expr)?),+; $($arg:tt)+) => ({
|
||||
let lvl = $lvl;
|
||||
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
|
||||
$crate::__private_api::log::<&_>(
|
||||
$crate::__private_api::format_args!($($arg)+),
|
||||
lvl,
|
||||
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
|
||||
$crate::__private_api::line!(),
|
||||
&[$(($crate::__log_key!($key), $crate::__log_value!($key $(:$capture)* = $($value)*))),+]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// log!(target: "my_target", Level::Info, "a {} event", "log");
|
||||
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
|
||||
let lvl = $lvl;
|
||||
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
|
||||
$crate::__private_api::log(
|
||||
$crate::__private_api::format_args!($($arg)+),
|
||||
lvl,
|
||||
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
|
||||
$crate::__private_api::line!(),
|
||||
(),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// log!(Level::Info, "a log event")
|
||||
($lvl:expr, $($arg:tt)+) => ($crate::log!(target: $crate::__private_api::module_path!(), $lvl, $($arg)+));
|
||||
}
|
||||
|
||||
/// Logs a message at the error level.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use log::error;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let (err_info, port) = ("No connection", 22);
|
||||
///
|
||||
/// error!("Error: {err_info} on port {port}");
|
||||
/// error!(target: "app_events", "App Error: {err_info}, Port: {port}");
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
// error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
|
||||
// error!(target: "my_target", "a {} event", "log")
|
||||
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Error, $($arg)+));
|
||||
|
||||
// error!("a {} event", "log")
|
||||
($($arg:tt)+) => ($crate::log!($crate::Level::Error, $($arg)+))
|
||||
}
|
||||
|
||||
/// Logs a message at the warn level.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use log::warn;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let warn_description = "Invalid Input";
|
||||
///
|
||||
/// warn!("Warning! {warn_description}!");
|
||||
/// warn!(target: "input_events", "App received warning: {warn_description}");
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! warn {
|
||||
// warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
|
||||
// warn!(target: "my_target", "a {} event", "log")
|
||||
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Warn, $($arg)+));
|
||||
|
||||
// warn!("a {} event", "log")
|
||||
($($arg:tt)+) => ($crate::log!($crate::Level::Warn, $($arg)+))
|
||||
}
|
||||
|
||||
/// Logs a message at the info level.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use log::info;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// # struct Connection { port: u32, speed: f32 }
|
||||
/// let conn_info = Connection { port: 40, speed: 3.20 };
|
||||
///
|
||||
/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
|
||||
/// info!(target: "connection_events", "Successful connection, port: {}, speed: {}",
|
||||
/// conn_info.port, conn_info.speed);
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
// info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
|
||||
// info!(target: "my_target", "a {} event", "log")
|
||||
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Info, $($arg)+));
|
||||
|
||||
// info!("a {} event", "log")
|
||||
($($arg:tt)+) => ($crate::log!($crate::Level::Info, $($arg)+))
|
||||
}
|
||||
|
||||
/// Logs a message at the debug level.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use log::debug;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// # struct Position { x: f32, y: f32 }
|
||||
/// let pos = Position { x: 3.234, y: -1.223 };
|
||||
///
|
||||
/// debug!("New position: x: {}, y: {}", pos.x, pos.y);
|
||||
/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
// debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
|
||||
// debug!(target: "my_target", "a {} event", "log")
|
||||
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Debug, $($arg)+));
|
||||
|
||||
// debug!("a {} event", "log")
|
||||
($($arg:tt)+) => ($crate::log!($crate::Level::Debug, $($arg)+))
|
||||
}
|
||||
|
||||
/// Logs a message at the trace level.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use log::trace;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// # struct Position { x: f32, y: f32 }
|
||||
/// let pos = Position { x: 3.234, y: -1.223 };
|
||||
///
|
||||
/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
|
||||
/// trace!(target: "app_events", "x is {} and y is {}",
|
||||
/// if pos.x >= 0.0 { "positive" } else { "negative" },
|
||||
/// if pos.y >= 0.0 { "positive" } else { "negative" });
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! trace {
|
||||
// trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
|
||||
// trace!(target: "my_target", "a {} event", "log")
|
||||
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Trace, $($arg)+));
|
||||
|
||||
// trace!("a {} event", "log")
|
||||
($($arg:tt)+) => ($crate::log!($crate::Level::Trace, $($arg)+))
|
||||
}
|
||||
|
||||
/// Determines if a message logged at the specified level in that module will
|
||||
/// be logged.
|
||||
///
|
||||
/// This can be used to avoid expensive computation of log message arguments if
|
||||
/// the message would be ignored anyway.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use log::Level::Debug;
|
||||
/// use log::{debug, log_enabled};
|
||||
///
|
||||
/// # fn foo() {
|
||||
/// if log_enabled!(Debug) {
|
||||
/// let data = expensive_call();
|
||||
/// debug!("expensive debug data: {} {}", data.x, data.y);
|
||||
/// }
|
||||
/// if log_enabled!(target: "Global", Debug) {
|
||||
/// let data = expensive_call();
|
||||
/// debug!(target: "Global", "expensive debug data: {} {}", data.x, data.y);
|
||||
/// }
|
||||
/// # }
|
||||
/// # struct Data { x: u32, y: u32 }
|
||||
/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! log_enabled {
|
||||
(target: $target:expr, $lvl:expr) => {{
|
||||
let lvl = $lvl;
|
||||
lvl <= $crate::STATIC_MAX_LEVEL
|
||||
&& lvl <= $crate::max_level()
|
||||
&& $crate::__private_api::enabled(lvl, $target)
|
||||
}};
|
||||
($lvl:expr) => {
|
||||
$crate::log_enabled!(target: $crate::__private_api::module_path!(), $lvl)
|
||||
};
|
||||
}
|
||||
|
||||
// These macros use a pattern of #[cfg]s to produce nicer error
|
||||
// messages when log features aren't available
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(feature = "kv")]
|
||||
macro_rules! __log_key {
|
||||
// key1 = 42
|
||||
($($args:ident)*) => {
|
||||
$crate::__private_api::stringify!($($args)*)
|
||||
};
|
||||
// "key1" = 42
|
||||
($($args:expr)*) => {
|
||||
$($args)*
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(not(feature = "kv"))]
|
||||
macro_rules! __log_key {
|
||||
($($args:tt)*) => {
|
||||
compile_error!("key value support requires the `kv` feature of `log`")
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(feature = "kv")]
|
||||
macro_rules! __log_value {
|
||||
// Entrypoint
|
||||
($key:tt = $args:expr) => {
|
||||
$crate::__log_value!(($args):value)
|
||||
};
|
||||
($key:tt :$capture:tt = $args:expr) => {
|
||||
$crate::__log_value!(($args):$capture)
|
||||
};
|
||||
($key:ident =) => {
|
||||
$crate::__log_value!(($key):value)
|
||||
};
|
||||
($key:ident :$capture:tt =) => {
|
||||
$crate::__log_value!(($key):$capture)
|
||||
};
|
||||
// ToValue
|
||||
(($args:expr):value) => {
|
||||
$crate::__private_api::capture_to_value(&&$args)
|
||||
};
|
||||
// Debug
|
||||
(($args:expr):?) => {
|
||||
$crate::__private_api::capture_debug(&&$args)
|
||||
};
|
||||
(($args:expr):debug) => {
|
||||
$crate::__private_api::capture_debug(&&$args)
|
||||
};
|
||||
// Display
|
||||
(($args:expr):%) => {
|
||||
$crate::__private_api::capture_display(&&$args)
|
||||
};
|
||||
(($args:expr):display) => {
|
||||
$crate::__private_api::capture_display(&&$args)
|
||||
};
|
||||
//Error
|
||||
(($args:expr):err) => {
|
||||
$crate::__log_value_error!($args)
|
||||
};
|
||||
// sval::Value
|
||||
(($args:expr):sval) => {
|
||||
$crate::__log_value_sval!($args)
|
||||
};
|
||||
// serde::Serialize
|
||||
(($args:expr):serde) => {
|
||||
$crate::__log_value_serde!($args)
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(not(feature = "kv"))]
|
||||
macro_rules! __log_value {
|
||||
($($args:tt)*) => {
|
||||
compile_error!("key value support requires the `kv` feature of `log`")
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(feature = "kv_sval")]
|
||||
macro_rules! __log_value_sval {
|
||||
($args:expr) => {
|
||||
$crate::__private_api::capture_sval(&&$args)
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(not(feature = "kv_sval"))]
|
||||
macro_rules! __log_value_sval {
|
||||
($args:expr) => {
|
||||
compile_error!("capturing values as `sval::Value` requites the `kv_sval` feature of `log`")
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(feature = "kv_serde")]
|
||||
macro_rules! __log_value_serde {
|
||||
($args:expr) => {
|
||||
$crate::__private_api::capture_serde(&&$args)
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(not(feature = "kv_serde"))]
|
||||
macro_rules! __log_value_serde {
|
||||
($args:expr) => {
|
||||
compile_error!(
|
||||
"capturing values as `serde::Serialize` requites the `kv_serde` feature of `log`"
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(feature = "kv_std")]
|
||||
macro_rules! __log_value_error {
|
||||
($args:expr) => {
|
||||
$crate::__private_api::capture_error(&$args)
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
#[cfg(not(feature = "kv_std"))]
|
||||
macro_rules! __log_value_error {
|
||||
($args:expr) => {
|
||||
compile_error!(
|
||||
"capturing values as `std::error::Error` requites the `kv_std` feature of `log`"
|
||||
)
|
||||
};
|
||||
}
|
||||
397
third-party/vendor/log/src/serde.rs
vendored
Normal file
397
third-party/vendor/log/src/serde.rs
vendored
Normal file
|
|
@ -0,0 +1,397 @@
|
|||
#![cfg(feature = "serde")]
|
||||
|
||||
use serde::de::{
|
||||
Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, Unexpected, VariantAccess,
|
||||
Visitor,
|
||||
};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
|
||||
use crate::{Level, LevelFilter, LOG_LEVEL_NAMES};
|
||||
|
||||
use std::fmt;
|
||||
use std::str::{self, FromStr};
|
||||
|
||||
// The Deserialize impls are handwritten to be case insensitive using FromStr.
|
||||
|
||||
impl Serialize for Level {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
Level::Error => serializer.serialize_unit_variant("Level", 0, "ERROR"),
|
||||
Level::Warn => serializer.serialize_unit_variant("Level", 1, "WARN"),
|
||||
Level::Info => serializer.serialize_unit_variant("Level", 2, "INFO"),
|
||||
Level::Debug => serializer.serialize_unit_variant("Level", 3, "DEBUG"),
|
||||
Level::Trace => serializer.serialize_unit_variant("Level", 4, "TRACE"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Level {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct LevelIdentifier;
|
||||
|
||||
impl<'de> Visitor<'de> for LevelIdentifier {
|
||||
type Value = Level;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("log level")
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
let variant = LOG_LEVEL_NAMES[1..]
|
||||
.get(v as usize)
|
||||
.ok_or_else(|| Error::invalid_value(Unexpected::Unsigned(v), &self))?;
|
||||
|
||||
self.visit_str(variant)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
// Case insensitive.
|
||||
FromStr::from_str(s).map_err(|_| Error::unknown_variant(s, &LOG_LEVEL_NAMES[1..]))
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
let variant = str::from_utf8(value)
|
||||
.map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
|
||||
|
||||
self.visit_str(variant)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> DeserializeSeed<'de> for LevelIdentifier {
|
||||
type Value = Level;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_identifier(LevelIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
struct LevelEnum;
|
||||
|
||||
impl<'de> Visitor<'de> for LevelEnum {
|
||||
type Value = Level;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("log level")
|
||||
}
|
||||
|
||||
fn visit_enum<A>(self, value: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: EnumAccess<'de>,
|
||||
{
|
||||
let (level, variant) = value.variant_seed(LevelIdentifier)?;
|
||||
// Every variant is a unit variant.
|
||||
variant.unit_variant()?;
|
||||
Ok(level)
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_enum("Level", &LOG_LEVEL_NAMES[1..], LevelEnum)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for LevelFilter {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
LevelFilter::Off => serializer.serialize_unit_variant("LevelFilter", 0, "OFF"),
|
||||
LevelFilter::Error => serializer.serialize_unit_variant("LevelFilter", 1, "ERROR"),
|
||||
LevelFilter::Warn => serializer.serialize_unit_variant("LevelFilter", 2, "WARN"),
|
||||
LevelFilter::Info => serializer.serialize_unit_variant("LevelFilter", 3, "INFO"),
|
||||
LevelFilter::Debug => serializer.serialize_unit_variant("LevelFilter", 4, "DEBUG"),
|
||||
LevelFilter::Trace => serializer.serialize_unit_variant("LevelFilter", 5, "TRACE"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for LevelFilter {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct LevelFilterIdentifier;
|
||||
|
||||
impl<'de> Visitor<'de> for LevelFilterIdentifier {
|
||||
type Value = LevelFilter;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("log level filter")
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
let variant = LOG_LEVEL_NAMES
|
||||
.get(v as usize)
|
||||
.ok_or_else(|| Error::invalid_value(Unexpected::Unsigned(v), &self))?;
|
||||
|
||||
self.visit_str(variant)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
// Case insensitive.
|
||||
FromStr::from_str(s).map_err(|_| Error::unknown_variant(s, &LOG_LEVEL_NAMES))
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
let variant = str::from_utf8(value)
|
||||
.map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
|
||||
|
||||
self.visit_str(variant)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> DeserializeSeed<'de> for LevelFilterIdentifier {
|
||||
type Value = LevelFilter;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_identifier(LevelFilterIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
struct LevelFilterEnum;
|
||||
|
||||
impl<'de> Visitor<'de> for LevelFilterEnum {
|
||||
type Value = LevelFilter;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("log level filter")
|
||||
}
|
||||
|
||||
fn visit_enum<A>(self, value: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: EnumAccess<'de>,
|
||||
{
|
||||
let (level_filter, variant) = value.variant_seed(LevelFilterIdentifier)?;
|
||||
// Every variant is a unit variant.
|
||||
variant.unit_variant()?;
|
||||
Ok(level_filter)
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_enum("LevelFilter", &LOG_LEVEL_NAMES, LevelFilterEnum)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{Level, LevelFilter};
|
||||
use serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};
|
||||
|
||||
fn level_token(variant: &'static str) -> Token {
|
||||
Token::UnitVariant {
|
||||
name: "Level",
|
||||
variant,
|
||||
}
|
||||
}
|
||||
|
||||
fn level_bytes_tokens(variant: &'static [u8]) -> [Token; 3] {
|
||||
[
|
||||
Token::Enum { name: "Level" },
|
||||
Token::Bytes(variant),
|
||||
Token::Unit,
|
||||
]
|
||||
}
|
||||
|
||||
fn level_variant_tokens(variant: u32) -> [Token; 3] {
|
||||
[
|
||||
Token::Enum { name: "Level" },
|
||||
Token::U32(variant),
|
||||
Token::Unit,
|
||||
]
|
||||
}
|
||||
|
||||
fn level_filter_token(variant: &'static str) -> Token {
|
||||
Token::UnitVariant {
|
||||
name: "LevelFilter",
|
||||
variant,
|
||||
}
|
||||
}
|
||||
|
||||
fn level_filter_bytes_tokens(variant: &'static [u8]) -> [Token; 3] {
|
||||
[
|
||||
Token::Enum {
|
||||
name: "LevelFilter",
|
||||
},
|
||||
Token::Bytes(variant),
|
||||
Token::Unit,
|
||||
]
|
||||
}
|
||||
|
||||
fn level_filter_variant_tokens(variant: u32) -> [Token; 3] {
|
||||
[
|
||||
Token::Enum {
|
||||
name: "LevelFilter",
|
||||
},
|
||||
Token::U32(variant),
|
||||
Token::Unit,
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_ser_de() {
|
||||
let cases = &[
|
||||
(Level::Error, [level_token("ERROR")]),
|
||||
(Level::Warn, [level_token("WARN")]),
|
||||
(Level::Info, [level_token("INFO")]),
|
||||
(Level::Debug, [level_token("DEBUG")]),
|
||||
(Level::Trace, [level_token("TRACE")]),
|
||||
];
|
||||
|
||||
for (s, expected) in cases {
|
||||
assert_tokens(s, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_case_insensitive() {
|
||||
let cases = &[
|
||||
(Level::Error, [level_token("error")]),
|
||||
(Level::Warn, [level_token("warn")]),
|
||||
(Level::Info, [level_token("info")]),
|
||||
(Level::Debug, [level_token("debug")]),
|
||||
(Level::Trace, [level_token("trace")]),
|
||||
];
|
||||
|
||||
for (s, expected) in cases {
|
||||
assert_de_tokens(s, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_de_bytes() {
|
||||
let cases = &[
|
||||
(Level::Error, level_bytes_tokens(b"ERROR")),
|
||||
(Level::Warn, level_bytes_tokens(b"WARN")),
|
||||
(Level::Info, level_bytes_tokens(b"INFO")),
|
||||
(Level::Debug, level_bytes_tokens(b"DEBUG")),
|
||||
(Level::Trace, level_bytes_tokens(b"TRACE")),
|
||||
];
|
||||
|
||||
for (value, tokens) in cases {
|
||||
assert_de_tokens(value, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_de_variant_index() {
|
||||
let cases = &[
|
||||
(Level::Error, level_variant_tokens(0)),
|
||||
(Level::Warn, level_variant_tokens(1)),
|
||||
(Level::Info, level_variant_tokens(2)),
|
||||
(Level::Debug, level_variant_tokens(3)),
|
||||
(Level::Trace, level_variant_tokens(4)),
|
||||
];
|
||||
|
||||
for (value, tokens) in cases {
|
||||
assert_de_tokens(value, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_de_error() {
|
||||
let msg = "unknown variant `errorx`, expected one of \
|
||||
`ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`";
|
||||
assert_de_tokens_error::<Level>(&[level_token("errorx")], msg);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_filter_ser_de() {
|
||||
let cases = &[
|
||||
(LevelFilter::Off, [level_filter_token("OFF")]),
|
||||
(LevelFilter::Error, [level_filter_token("ERROR")]),
|
||||
(LevelFilter::Warn, [level_filter_token("WARN")]),
|
||||
(LevelFilter::Info, [level_filter_token("INFO")]),
|
||||
(LevelFilter::Debug, [level_filter_token("DEBUG")]),
|
||||
(LevelFilter::Trace, [level_filter_token("TRACE")]),
|
||||
];
|
||||
|
||||
for (s, expected) in cases {
|
||||
assert_tokens(s, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_filter_case_insensitive() {
|
||||
let cases = &[
|
||||
(LevelFilter::Off, [level_filter_token("off")]),
|
||||
(LevelFilter::Error, [level_filter_token("error")]),
|
||||
(LevelFilter::Warn, [level_filter_token("warn")]),
|
||||
(LevelFilter::Info, [level_filter_token("info")]),
|
||||
(LevelFilter::Debug, [level_filter_token("debug")]),
|
||||
(LevelFilter::Trace, [level_filter_token("trace")]),
|
||||
];
|
||||
|
||||
for (s, expected) in cases {
|
||||
assert_de_tokens(s, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_filter_de_bytes() {
|
||||
let cases = &[
|
||||
(LevelFilter::Off, level_filter_bytes_tokens(b"OFF")),
|
||||
(LevelFilter::Error, level_filter_bytes_tokens(b"ERROR")),
|
||||
(LevelFilter::Warn, level_filter_bytes_tokens(b"WARN")),
|
||||
(LevelFilter::Info, level_filter_bytes_tokens(b"INFO")),
|
||||
(LevelFilter::Debug, level_filter_bytes_tokens(b"DEBUG")),
|
||||
(LevelFilter::Trace, level_filter_bytes_tokens(b"TRACE")),
|
||||
];
|
||||
|
||||
for (value, tokens) in cases {
|
||||
assert_de_tokens(value, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_filter_de_variant_index() {
|
||||
let cases = &[
|
||||
(LevelFilter::Off, level_filter_variant_tokens(0)),
|
||||
(LevelFilter::Error, level_filter_variant_tokens(1)),
|
||||
(LevelFilter::Warn, level_filter_variant_tokens(2)),
|
||||
(LevelFilter::Info, level_filter_variant_tokens(3)),
|
||||
(LevelFilter::Debug, level_filter_variant_tokens(4)),
|
||||
(LevelFilter::Trace, level_filter_variant_tokens(5)),
|
||||
];
|
||||
|
||||
for (value, tokens) in cases {
|
||||
assert_de_tokens(value, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_level_filter_de_error() {
|
||||
let msg = "unknown variant `errorx`, expected one of \
|
||||
`OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`";
|
||||
assert_de_tokens_error::<LevelFilter>(&[level_filter_token("errorx")], msg);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue