fix(users/Profpatsch/netencode): decode U::Text directly into str
Since `Text` is a scalar, it doesn’t make sense to delay the utf-8 verification to the consumer. Change-Id: I36e4d228fbf35374d7c1addb4b24828cf6e927e5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2478 Tested-by: BuildkiteCI Reviewed-by: Profpatsch <mail@profpatsch.de>
This commit is contained in:
parent
e91d5e4e61
commit
14f9a22f46
2 changed files with 15 additions and 15 deletions
|
@ -44,7 +44,7 @@ pub enum U<'a> {
|
|||
I6(i64),
|
||||
I7(i128),
|
||||
// Text
|
||||
Text(&'a [u8]),
|
||||
Text(&'a str),
|
||||
Binary(&'a [u8]),
|
||||
// Tags
|
||||
Sum(Tag<&'a str, U<'a>>),
|
||||
|
@ -87,7 +87,7 @@ pub fn encode<W: Write>(w: &mut W, u: U) -> std::io::Result<()> {
|
|||
U::I7(i) => write!(w, "i7:{},", i),
|
||||
U::Text(s) => {
|
||||
write!(w, "t{}:", s.len());
|
||||
w.write(&s);
|
||||
w.write(s.as_bytes());
|
||||
write!(w, ",")
|
||||
}
|
||||
U::Binary(s) => {
|
||||
|
@ -247,16 +247,16 @@ pub mod parse {
|
|||
|
||||
/// parse text scalar (`t5:hello,`)
|
||||
fn text(s: &[u8]) -> IResult<&[u8], T> {
|
||||
let (s, res) = text_g()(s)?;
|
||||
Ok((s, T::Text(
|
||||
std::str::from_utf8(res)
|
||||
.map_err(|_| nom::Err::Failure((s, ErrorKind::Char)))
|
||||
.map(|s| s.to_string())?,
|
||||
)))
|
||||
let (s, res) = text_g(s)?;
|
||||
Ok((s, T::Text(res.to_string())))
|
||||
}
|
||||
|
||||
fn text_g() -> impl Fn(&[u8]) -> IResult<&[u8], &[u8]> {
|
||||
sized('t', ',')
|
||||
fn text_g(s: &[u8]) -> IResult<&[u8], &str> {
|
||||
let (s, res) = sized('t', ',')(s)?;
|
||||
Ok((s,
|
||||
std::str::from_utf8(res)
|
||||
.map_err(|_| nom::Err::Failure((s, ErrorKind::Char)))?,
|
||||
))
|
||||
}
|
||||
|
||||
fn binary<'a>() -> impl Fn(&'a [u8]) -> IResult<&'a [u8], T> {
|
||||
|
@ -324,7 +324,7 @@ pub mod parse {
|
|||
|
||||
pub fn u_u(s: &[u8]) -> IResult<&[u8], U> {
|
||||
alt((
|
||||
map(text_g(), U::Text),
|
||||
map(text_g, U::Text),
|
||||
map(binary_g(), U::Binary),
|
||||
map(unit_t, |()| U::Unit),
|
||||
map(tag_g(u_u), |t| U::Sum(t)),
|
||||
|
|
|
@ -128,8 +128,8 @@ fn main() -> std::io::Result<()> {
|
|||
|
||||
fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> {
|
||||
let mut http = vec![
|
||||
("method", U::Text(method.as_bytes())),
|
||||
("path", U::Text(path.as_bytes())),
|
||||
("method", U::Text(method)),
|
||||
("path", U::Text(path)),
|
||||
];
|
||||
write_dict(http, headers)
|
||||
}
|
||||
|
@ -137,7 +137,7 @@ fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String,
|
|||
fn write_dict_resp<'buf>(code: u16, reason: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> {
|
||||
let mut http = vec![
|
||||
("status", U::N6(code as u64)),
|
||||
("status-text", U::Text(reason.as_bytes())),
|
||||
("status-text", U::Text(reason)),
|
||||
];
|
||||
write_dict(http, headers)
|
||||
}
|
||||
|
@ -147,7 +147,7 @@ fn write_dict<'buf, 'a>(mut http: Vec<(&str, U<'a>)>, headers: &'a[(String, &str
|
|||
http.push(("headers", U::Record(
|
||||
headers.iter().map(
|
||||
|(name, value)|
|
||||
(name.as_str(), U::Text(value.as_bytes()))
|
||||
(name.as_str(), U::Text(value))
|
||||
).collect::<Vec<_>>()
|
||||
)));
|
||||
|
||||
|
|
Loading…
Reference in a new issue