-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for structured logging (KV)
- Loading branch information
Showing
11 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Workaround https://stackoverflow.com/questions/79216846 | ||
|
||
use std::{borrow::Borrow, ops::Deref}; | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum ConcreteCow<'a, B: ?Sized, O> { | ||
Borrowed(&'a B), | ||
Owned(O), | ||
} | ||
|
||
impl<B: ?Sized, O: Clone> Clone for ConcreteCow<'_, B, O> { | ||
fn clone(&self) -> Self { | ||
match self { | ||
Self::Borrowed(b) => Self::Borrowed(b), | ||
Self::Owned(o) => Self::Owned(o.clone()), | ||
} | ||
} | ||
} | ||
|
||
impl<B: ?Sized, O: Borrow<B>> Deref for ConcreteCow<'_, B, O> { | ||
type Target = B; | ||
|
||
fn deref(&self) -> &B { | ||
match *self { | ||
Self::Borrowed(borrowed) => borrowed, | ||
Self::Owned(ref owned) => owned.borrow(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod concrete_cow; | ||
|
||
pub(crate) use concrete_cow::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use std::borrow::Cow; | ||
|
||
use value_bag::{OwnedValueBag, ValueBag}; | ||
|
||
use crate::helper::ConcreteCow; | ||
|
||
#[derive(Debug, Clone)] | ||
enum KeyInner<'a> { | ||
Str(&'a str), | ||
StaticStr(&'static str), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Key<'a>(KeyInner<'a>); | ||
|
||
impl<'a> Key<'a> { | ||
pub fn __from_static_str(key: &'static str) -> Self { | ||
Key(KeyInner::StaticStr(key)) | ||
} | ||
|
||
fn from_str(key: &'a str) -> Self { | ||
Key(KeyInner::Str(key)) | ||
} | ||
|
||
pub(crate) fn to_owned(&self) -> KeyOwned { | ||
let inner = match self.0 { | ||
KeyInner::Str(s) => KeyOwnedInner::CowStr(Cow::Owned(s.to_string())), | ||
KeyInner::StaticStr(s) => KeyOwnedInner::CowStr(Cow::Borrowed(s)), | ||
}; | ||
KeyOwned(inner) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
enum KeyOwnedInner { | ||
CowStr(Cow<'static, str>), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub(crate) struct KeyOwned(KeyOwnedInner); | ||
|
||
impl KeyOwned { | ||
pub(crate) fn as_ref(&self) -> Key { | ||
let inner = match &self.0 { | ||
KeyOwnedInner::CowStr(s) => match s { | ||
Cow::Borrowed(s) => KeyInner::StaticStr(s), | ||
Cow::Owned(s) => KeyInner::Str(s), | ||
}, | ||
}; | ||
Key(inner) | ||
} | ||
} | ||
|
||
pub type Value<'a> = ValueBag<'a>; | ||
pub(crate) type ValueOwned = OwnedValueBag; | ||
|
||
pub(crate) type Pair<'a> = (Key<'a>, Value<'a>); | ||
|
||
#[cfg(feature = "log")] | ||
pub(crate) struct LogCrateConverter<'a>(Vec<(Key<'a>, Value<'a>)>); | ||
|
||
#[cfg(feature = "log")] | ||
impl<'a> LogCrateConverter<'a> { | ||
pub(crate) fn new(capacity: usize) -> Self { | ||
Self(Vec::with_capacity(capacity)) | ||
} | ||
|
||
pub(crate) fn finalize(self) -> ConcreteCow<'a, [Pair<'a>], Vec<Pair<'a>>> { | ||
ConcreteCow::Owned(self.0) | ||
} | ||
} | ||
|
||
#[cfg(feature = "log")] | ||
impl<'a> log::kv::VisitSource<'a> for LogCrateConverter<'a> { | ||
fn visit_pair( | ||
&mut self, | ||
key: log::kv::Key<'a>, | ||
value: log::kv::Value<'a>, | ||
) -> Result<(), log::kv::Error> { | ||
self.0.push(( | ||
Key::from_str(key.as_str()), | ||
todo!("convert `lov::kv::Value` to `Value`"), | ||
)); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters