Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some API of the key-value feature #585

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/kv/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ impl<'k> Key<'k> {
pub fn as_str(&self) -> &str {
self.key
}

/// Try get a string borrowed for the `'k` lifetime from this key.
pub fn to_borrowed_str(&self) -> Option<&'k str> {
// NOTE: This API leaves room for keys to be owned
Some(self.key)
}
}

impl<'k> fmt::Display for Key<'k> {
Expand Down
325 changes: 0 additions & 325 deletions src/kv/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,319 +402,6 @@ mod std_support {
}
}

/// The result of calling `Source::as_map`.
pub struct AsMap<S>(S);

/// Visit this source as a map.
pub fn as_map<S>(source: S) -> AsMap<S>
where
S: Source,
{
AsMap(source)
}

impl<S> Source for AsMap<S>
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
self.0.visit(visitor)
}

fn get(&self, key: Key) -> Option<Value<'_>> {
self.0.get(key)
}

fn count(&self) -> usize {
self.0.count()
}
}

impl<S> fmt::Debug for AsMap<S>
where
S: Source,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_map();
self.0.visit(&mut f).map_err(|_| fmt::Error)?;
f.finish()
}
}

/// The result of calling `Source::as_list`
pub struct AsList<S>(S);

/// Visit this source as a list.
pub fn as_list<S>(source: S) -> AsList<S>
where
S: Source,
{
AsList(source)
}

impl<S> Source for AsList<S>
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
self.0.visit(visitor)
}

fn get(&self, key: Key) -> Option<Value<'_>> {
self.0.get(key)
}

fn count(&self) -> usize {
self.0.count()
}
}

impl<S> fmt::Debug for AsList<S>
where
S: Source,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_list();
self.0.visit(&mut f).map_err(|_| fmt::Error)?;
f.finish()
}
}

#[cfg(feature = "kv_unstable_sval")]
mod sval_support {
use super::*;

impl<S> sval::Value for AsMap<S>
where
S: Source,
{
fn stream<'sval, SV: sval::Stream<'sval> + ?Sized>(
&'sval self,
stream: &mut SV,
) -> sval::Result {
struct StreamVisitor<'a, V: ?Sized>(&'a mut V);

impl<'a, 'kvs, V: sval::Stream<'kvs> + ?Sized> Visitor<'kvs> for StreamVisitor<'a, V> {
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.0
.map_key_begin()
.map_err(|_| Error::msg("failed to stream map key"))?;
sval_ref::stream_ref(self.0, key)
.map_err(|_| Error::msg("failed to stream map key"))?;
self.0
.map_key_end()
.map_err(|_| Error::msg("failed to stream map key"))?;

self.0
.map_value_begin()
.map_err(|_| Error::msg("failed to stream map value"))?;
sval_ref::stream_ref(self.0, value)
.map_err(|_| Error::msg("failed to stream map value"))?;
self.0
.map_value_end()
.map_err(|_| Error::msg("failed to stream map value"))?;

Ok(())
}
}

stream
.map_begin(Some(self.count()))
.map_err(|_| sval::Error::new())?;

self.visit(&mut StreamVisitor(stream))
.map_err(|_| sval::Error::new())?;

stream.map_end().map_err(|_| sval::Error::new())
}
}

impl<S> sval::Value for AsList<S>
where
S: Source,
{
fn stream<'sval, SV: sval::Stream<'sval> + ?Sized>(
&'sval self,
stream: &mut SV,
) -> sval::Result {
struct StreamVisitor<'a, V: ?Sized>(&'a mut V);

impl<'a, 'kvs, V: sval::Stream<'kvs> + ?Sized> Visitor<'kvs> for StreamVisitor<'a, V> {
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.0
.seq_value_begin()
.map_err(|_| Error::msg("failed to stream seq value"))?;
sval_ref::stream_ref(self.0, (key, value))
.map_err(|_| Error::msg("failed to stream seq value"))?;
self.0
.seq_value_end()
.map_err(|_| Error::msg("failed to stream seq value"))?;

Ok(())
}
}

stream
.seq_begin(Some(self.count()))
.map_err(|_| sval::Error::new())?;

self.visit(&mut StreamVisitor(stream))
.map_err(|_| sval::Error::new())?;

stream.seq_end().map_err(|_| sval::Error::new())
}
}

#[cfg(test)]
mod tests {
use super::*;
use sval_derive::Value;

#[test]
fn derive_stream() {
#[derive(Value)]
pub struct MyRecordAsMap<'a> {
msg: &'a str,
kvs: AsMap<&'a dyn Source>,
}

#[derive(Value)]
pub struct MyRecordAsList<'a> {
msg: &'a str,
kvs: AsList<&'a dyn Source>,
}
}
}
}

#[cfg(feature = "kv_unstable_serde")]
pub mod as_map {
//! `serde` adapters for serializing a `Source` as a map.

use super::*;
use serde::{Serialize, Serializer};

/// Serialize a `Source` as a map.
pub fn serialize<T, S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: Source,
S: Serializer,
{
as_map(source).serialize(serializer)
}
}

#[cfg(feature = "kv_unstable_serde")]
pub mod as_list {
//! `serde` adapters for serializing a `Source` as a list.

use super::*;
use serde::{Serialize, Serializer};

/// Serialize a `Source` as a list.
pub fn serialize<T, S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: Source,
S: Serializer,
{
as_list(source).serialize(serializer)
}
}

#[cfg(feature = "kv_unstable_serde")]
mod serde_support {
use super::*;
use serde::ser::{Error as SerError, Serialize, SerializeMap, SerializeSeq, Serializer};

impl<T> Serialize for AsMap<T>
where
T: Source,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
struct SerializerVisitor<'a, S>(&'a mut S);

impl<'a, 'kvs, S> Visitor<'kvs> for SerializerVisitor<'a, S>
where
S: SerializeMap,
{
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.0
.serialize_entry(&key, &value)
.map_err(|_| Error::msg("failed to serialize map entry"))?;
Ok(())
}
}

let mut map = serializer.serialize_map(Some(self.count()))?;

self.visit(&mut SerializerVisitor(&mut map))
.map_err(|_| S::Error::custom("failed to visit key-values"))?;

map.end()
}
}

impl<T> Serialize for AsList<T>
where
T: Source,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
struct SerializerVisitor<'a, S>(&'a mut S);

impl<'a, 'kvs, S> Visitor<'kvs> for SerializerVisitor<'a, S>
where
S: SerializeSeq,
{
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.0
.serialize_element(&(key, value))
.map_err(|_| Error::msg("failed to serialize seq entry"))?;
Ok(())
}
}

let mut seq = serializer.serialize_seq(Some(self.count()))?;

self.visit(&mut SerializerVisitor(&mut seq))
.map_err(|_| S::Error::custom("failed to visit seq"))?;

seq.end()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::kv::source;
use serde::Serialize;

#[test]
fn derive_serialize() {
#[derive(Serialize)]
pub struct MyRecordAsMap<'a> {
msg: &'a str,
#[serde(flatten)]
#[serde(with = "source::as_map")]
kvs: &'a dyn Source,
}

#[derive(Serialize)]
pub struct MyRecordAsList<'a> {
msg: &'a str,
#[serde(flatten)]
#[serde(with = "source::as_list")]
kvs: &'a dyn Source,
}
}
}
}

#[cfg(test)]
mod tests {
use crate::kv::value::tests::Token;
Expand Down Expand Up @@ -766,16 +453,4 @@ mod tests {
let source = None::<(&str, i32)>;
assert!(Source::get(&source, Key::from_str("a")).is_none());
}

#[test]
fn as_map() {
let _ = crate::kv::source::as_map(("a", 1));
let _ = crate::kv::source::as_map(&("a", 1) as &dyn Source);
}

#[test]
fn as_list() {
let _ = crate::kv::source::as_list(("a", 1));
let _ = crate::kv::source::as_list(&("a", 1) as &dyn Source);
}
}
28 changes: 0 additions & 28 deletions src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ macro_rules! as_sval {
/// assert_eq!(Some(42), value.to_i64());
/// ```
///
/// ```
/// # use std::fmt::Debug;
/// use log::kv::ToValue;
///
/// let value = (&42i32 as &dyn Debug).to_value();
///
/// assert_eq!(None, value.to_i64());
/// ```
///
/// ## Using the standard `From` trait
///
/// Standard types that implement `ToValue` also implement `From`.
Expand Down Expand Up @@ -376,25 +367,6 @@ impl<'v> fmt::Display for Value<'v> {
}
}

impl ToValue for dyn fmt::Debug {
fn to_value(&self) -> Value {
Value::from_dyn_debug(self)
}
}

impl ToValue for dyn fmt::Display {
fn to_value(&self) -> Value {
Value::from_dyn_display(self)
}
}

#[cfg(feature = "kv_unstable_std")]
impl ToValue for dyn std::error::Error + 'static {
fn to_value(&self) -> Value {
Value::from_dyn_error(self)
}
}

#[cfg(feature = "kv_unstable_serde")]
impl<'v> serde::Serialize for Value<'v> {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
Expand Down