Skip to content

Commit

Permalink
Merge pull request #194 from sval-rs/feat/json-str-methods
Browse files Browse the repository at this point in the history
Add some more methods to JsonStr
  • Loading branch information
KodrAus committed Mar 28, 2024
2 parents 0e84b9a + 406d898 commit 875d678
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 7 deletions.
6 changes: 5 additions & 1 deletion buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ Rather than conditionally compile these methods, this library stubs
out functionality when an allocator isn't available.
*/

#![cfg_attr(not(test), no_std)]
#![no_std]
#![deny(missing_docs)]

mod error;

#[cfg(feature = "std")]
#[macro_use]
#[allow(unused_imports)]
extern crate std as libstd;

#[cfg(not(feature = "alloc"))]
extern crate core as std;

#[cfg(any(test, feature = "alloc"))]
#[macro_use]
#[allow(unused_imports)]
extern crate alloc;
#[cfg(feature = "alloc")]
extern crate core;
Expand Down
2 changes: 2 additions & 0 deletions buffer/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,8 @@ mod tests {
mod alloc_tests {
use super::*;

use crate::std::string::String;

use sval::Stream as _;
use sval_derive_macros::*;

Expand Down
2 changes: 2 additions & 0 deletions derive/test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ mod derive_tuple {

#[test]
fn skip() {
#[allow(dead_code)]
#[derive(Value)]
struct Tuple(#[sval(skip)] i32, i32);

Expand Down Expand Up @@ -1045,6 +1046,7 @@ mod derive_enum {
#[test]
fn skip() {
#[derive(Value)]
#[allow(dead_code)]
enum Enum {
Record {
#[sval(skip)]
Expand Down
3 changes: 2 additions & 1 deletion flatten/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ value is stringified and concatenated together.
- **tuples**: tuple values are passed through directly.
*/

#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
#![deny(missing_docs)]

#[cfg(any(test, feature = "alloc"))]
extern crate alloc;

mod flattener;
Expand Down
3 changes: 2 additions & 1 deletion flatten/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,12 @@ impl<'sval, S: Stream<'sval>> Stream<'sval> for PassThru<S> {

#[cfg(test)]
mod tests {
use alloc::borrow::ToOwned;
use sval_derive_macros::*;

use super::*;

use alloc::borrow::ToOwned;

struct Outer<I> {
a: i32,
// #[sval(flatten)]
Expand Down
4 changes: 2 additions & 2 deletions fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ that formats it using the same output that you'd get if you
derived [`core::fmt::Debug`].
*/

#![cfg_attr(not(test), no_std)]
#![no_std]
#![deny(missing_docs)]

#[cfg(feature = "alloc")]
#[cfg(any(test, feature = "alloc"))]
extern crate alloc;

mod writer;
Expand Down
2 changes: 2 additions & 0 deletions fmt/src/token_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ impl<'a, W: TokenWrite + ?Sized> TokenWrite for &'a mut W {
mod tests {
use super::*;

use alloc::string::String;

#[test]
fn escape_debug() {
let cases = [
Expand Down
34 changes: 33 additions & 1 deletion json/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::fmt;
use core::{borrow::Borrow, fmt};

/**
A string containing encoded JSON.
Expand All @@ -25,6 +25,19 @@ impl JsonStr {
pub const fn as_str(&self) -> &str {
&self.0
}

/**
Get a reference to the bytes of the underlying string.
*/
pub const fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}

impl<'a> From<&'a str> for &'a JsonStr {
fn from(value: &'a str) -> Self {
JsonStr::new(value)
}
}

impl fmt::Debug for JsonStr {
Expand Down Expand Up @@ -53,6 +66,18 @@ impl sval::Value for JsonStr {
}
}

impl AsRef<str> for JsonStr {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Borrow<str> for JsonStr {
fn borrow(&self) -> &str {
&self.0
}
}

#[cfg(feature = "alloc")]
mod alloc_support {
use super::*;
Expand All @@ -70,4 +95,11 @@ mod alloc_support {
unsafe { Box::from_raw(Box::into_raw(json) as *mut str as *mut JsonStr) }
}
}

impl From<Box<str>> for Box<JsonStr> {
fn from(value: Box<str>) -> Self {
// SAFETY: `JsonStr` and `str` have the same ABI
unsafe { Box::from_raw(Box::into_raw(value) as *mut JsonStr) }
}
}
}
14 changes: 14 additions & 0 deletions json/test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ fn stream_unit_struct() {
assert_stream("\"UnitStruct\"", UnitStruct);
}

#[test]
fn stream_json_str() {
let json = "{\"a\":[1,true,4.3]}";

assert_eq!(
json,
sval_json::stream_to_string(sval_json::JsonStr::new(json)).unwrap()
);
assert_eq!(
json,
sval_json::stream_to_string(sval_json::JsonStr::boxed(json)).unwrap()
);
}

#[test]
fn stream_map_struct() {
assert_json(MapStruct {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ The [`Value`] and [`Stream`] traits aren't object-safe themselves, but object-sa
wrappers are provided by the `sval_dynamic` crate. This wrapper works in no-std.
*/

#![cfg_attr(not(test), no_std)]
#![no_std]
#![deny(missing_docs)]

#[cfg(feature = "std")]
Expand Down

0 comments on commit 875d678

Please sign in to comment.