about summary refs log tree commit diff
path: root/tvix/nix-compat/src/wire/bytes.rs
diff options
context:
space:
mode:
authorPicnoir <picnoir@alternativebit.fr>2024-04-05T09·33+0200
committerpicnoir picnoir <picnoir@alternativebit.fr>2024-04-07T20·30+0000
commit199f9b0a79de0fb0fd57ce9307b36390339ee7e7 (patch)
tree806fb4ec29232cd25b0b4c41b072948cd2239679 /tvix/nix-compat/src/wire/bytes.rs
parent289b3126db6248ea7dafddaf9931f4bf277c3d88 (diff)
feat(tvix/nix-compat): read client setting from wire r/7867
Add the primitives necessary to read the client settings from the Nix
daemon wire protocol.

Introducing the read_string primitive. This trivial primitive parses a
read_bytes call, check the bytes are valid utf-8 bytes and wraps the
result in a String.

Change-Id: Ie1253523a6bd4e31e7924e9898a0898109da2fa0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11358
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/wire/bytes.rs')
-rw-r--r--tvix/nix-compat/src/wire/bytes.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/tvix/nix-compat/src/wire/bytes.rs b/tvix/nix-compat/src/wire/bytes.rs
index 1a25daca8b..070f2368fd 100644
--- a/tvix/nix-compat/src/wire/bytes.rs
+++ b/tvix/nix-compat/src/wire/bytes.rs
@@ -1,4 +1,7 @@
-use std::ops::RangeBounds;
+use std::{
+    io::{Error, ErrorKind},
+    ops::RangeBounds,
+};
 
 use tokio::io::{AsyncReadExt, AsyncWriteExt};
 
@@ -58,6 +61,21 @@ where
     Ok(buf)
 }
 
+/// Read a Nix daemon string from the AsyncWrite, encoded as utf8.
+/// Rejects reading more than `allowed_size` bytes
+///
+/// A Nix daemon string is made up of two distincts parts:
+/// 1. Its lenght, LE-encoded on 64 bits.
+/// 2. Its content. 0-padded on 64 bits.
+pub async fn read_string<R, S>(r: &mut R, allowed_size: S) -> std::io::Result<String>
+where
+    R: AsyncReadExt + Unpin,
+    S: RangeBounds<u64>,
+{
+    let bytes = read_bytes(r, allowed_size).await?;
+    String::from_utf8(bytes).map_err(|e| Error::new(ErrorKind::InvalidData, e))
+}
+
 /// Writes a sequence of sized bits to a (hopefully buffered)
 /// [AsyncWriteExt] handle.
 ///