move str values into str module

This commit is contained in:
Conrad Ludgate
2025-07-18 09:46:00 +01:00
parent 03522b3434
commit 8ba106d832
2 changed files with 26 additions and 26 deletions

View File

@@ -10,6 +10,30 @@
use std::fmt::{self, Write};
use crate::{KeyEncoder, ValueEncoder, ValueSer};
impl KeyEncoder for &str {}
impl ValueEncoder for &str {
#[inline]
fn encode(self, v: ValueSer<'_>) {
format_escaped_str(v.buf, self);
v.finish();
}
}
impl KeyEncoder for fmt::Arguments<'_> {}
impl ValueEncoder for fmt::Arguments<'_> {
#[inline]
fn encode(self, v: ValueSer<'_>) {
if let Some(s) = self.as_str() {
format_escaped_str(v.buf, s);
} else {
format_escaped_fmt(v.buf, self);
}
v.finish();
}
}
/// Represents a character escape code in a type-safe manner.
pub enum CharEscape {
/// An escaped quote `"`
@@ -50,7 +74,7 @@ impl CharEscape {
}
}
pub(crate) fn format_escaped_str(writer: &mut Vec<u8>, value: &str) {
fn format_escaped_str(writer: &mut Vec<u8>, value: &str) {
writer.reserve(2 + value.len());
writer.push(b'"');
@@ -61,7 +85,7 @@ pub(crate) fn format_escaped_str(writer: &mut Vec<u8>, value: &str) {
writer.push(b'"');
}
pub(crate) fn format_escaped_fmt(writer: &mut Vec<u8>, args: fmt::Arguments) {
fn format_escaped_fmt(writer: &mut Vec<u8>, args: fmt::Arguments) {
writer.push(b'"');
Collect { buf: writer }

View File

@@ -1,7 +1,5 @@
use core::fmt;
use std::collections::{BTreeMap, HashMap};
use crate::str::{format_escaped_fmt, format_escaped_str};
use crate::{ValueSer, value_as_list, value_as_object};
/// Marker trait for values that are valid keys
@@ -27,15 +25,6 @@ impl<T: Copy + ValueEncoder> ValueEncoder for &T {
}
}
impl KeyEncoder for &str {}
impl ValueEncoder for &str {
#[inline]
fn encode(self, v: ValueSer<'_>) {
format_escaped_str(v.buf, self);
v.finish();
}
}
impl KeyEncoder for String {}
impl ValueEncoder for String {
#[inline]
@@ -44,19 +33,6 @@ impl ValueEncoder for String {
}
}
impl KeyEncoder for fmt::Arguments<'_> {}
impl ValueEncoder for fmt::Arguments<'_> {
#[inline]
fn encode(self, v: ValueSer<'_>) {
if let Some(s) = self.as_str() {
format_escaped_str(v.buf, s);
} else {
format_escaped_fmt(v.buf, self);
}
v.finish();
}
}
macro_rules! int {
[$($t:ty),*] => {
$(