refactor(nix-compat): Move serialization machinery into wire.

This groups most `wire` feature gated logic into a single module.
The nix_daemon module will be gated by a feature that adds
nix-compat-derive as a dependency.

All of this is a way to break the crate2nix dependency cycle between
nix-compat and nix-compat-derive(which depends on nix-compat for its
doctests).

Change-Id: I95938a6f280c11967371ff21f8b5a19e6d3d3805
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12761
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Vova Kryachko 2024-11-10 11:16:48 -05:00 committed by Vladimir Kryachko
parent 11ee751aff
commit 8df919dcf0
28 changed files with 45 additions and 49 deletions

View file

@ -1,7 +1,7 @@
use std::str::FromStr;
use nix_compat::nix_daemon::de::mock::{Builder, Error};
use nix_compat::nix_daemon::de::NixRead;
use nix_compat::wire::de::mock::{Builder, Error};
use nix_compat::wire::de::NixRead;
use nix_compat_derive::NixDeserialize;
#[derive(Debug, PartialEq, Eq, NixDeserialize)]

View file

@ -15,7 +15,7 @@ error[E0277]: the trait bound `BadType: NixDeserialize` is not satisfied
u64
usize
note: required by a bound in `try_read_value`
--> $WORKSPACE/nix-compat/src/nix_daemon/de/mod.rs
--> $WORKSPACE/nix-compat/src/wire/de/mod.rs
|
| fn try_read_value<V: NixDeserialize>(
| ^^^^^^^^^^^^^^ required by this bound in `NixRead::try_read_value`

View file

@ -7,7 +7,7 @@ error[E0277]: `()` doesn't implement `std::fmt::Display`
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
note: required by a bound in `invalid_data`
--> $WORKSPACE/nix-compat/src/nix_daemon/de/mod.rs
--> $WORKSPACE/nix-compat/src/wire/de/mod.rs
|
| fn invalid_data<T: fmt::Display>(msg: T) -> Self {
| ^^^^^^^^^^^^ required by this bound in `Error::invalid_data`

View file

@ -1,5 +1,6 @@
error: Missing from_str, from or try_from attribute
--> tests/ui/deserialize_remote_missing_attr.rs:10:25
--> tests/ui/deserialize_remote_missing_attr.rs:11:5
|
10 | nix_deserialize_remote!(#[nix()] Value);
| ^^^^^^^^^^^^^^
11 | / #[nix()]
12 | | Value
| |_________^

View file

@ -7,7 +7,7 @@ error[E0277]: `()` doesn't implement `std::fmt::Display`
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
note: required by a bound in `invalid_data`
--> $WORKSPACE/nix-compat/src/nix_daemon/de/mod.rs
--> $WORKSPACE/nix-compat/src/wire/de/mod.rs
|
| fn invalid_data<T: fmt::Display>(msg: T) -> Self {
| ^^^^^^^^^^^^ required by this bound in `Error::invalid_data`

View file

@ -1,6 +1,6 @@
use std::fmt;
use nix_compat::nix_daemon::ser::{
use nix_compat::wire::ser::{
mock::{Builder, Error},
NixWrite as _,
};

View file

@ -58,12 +58,12 @@ fn nix_deserialize_impl(
quote! {
#[automatically_derived]
impl #impl_generics #crate_path::nix_daemon::de::NixDeserialize for #ty #ty_generics
impl #impl_generics #crate_path::wire::de::NixDeserialize for #ty #ty_generics
#where_clause
{
#[allow(clippy::manual_async_fn)]
fn try_deserialize<R>(reader: &mut R) -> impl ::std::future::Future<Output=Result<Option<Self>, R::Error>> + Send + '_
where R: ?Sized + #crate_path::nix_daemon::de::NixRead + Send,
where R: ?Sized + #crate_path::wire::de::NixRead + Send,
{
#body
}
@ -253,7 +253,7 @@ fn nix_deserialize_try_from(crate_path: &Path, ty: &Type) -> TokenStream {
quote_spanned! {
ty.span() =>
async move {
use #crate_path::nix_daemon::de::Error;
use #crate_path::wire::de::Error;
if let Some(item) = reader.try_read_value::<#ty>().await? {
<Self as ::std::convert::TryFrom<#ty>>::try_from(item)
.map_err(Error::invalid_data)
@ -269,7 +269,7 @@ fn nix_deserialize_from_str(crate_path: &Path, span: Span) -> TokenStream {
quote_spanned! {
span =>
async move {
use #crate_path::nix_daemon::de::Error;
use #crate_path::wire::de::Error;
if let Some(buf) = reader.try_read_bytes().await? {
let s = ::std::str::from_utf8(&buf)
.map_err(Error::invalid_data)?;

View file

@ -55,13 +55,13 @@ fn nix_serialize_impl(
quote! {
#[automatically_derived]
impl #impl_generics #crate_path::nix_daemon::ser::NixSerialize for #ty #ty_generics
impl #impl_generics #crate_path::wire::ser::NixSerialize for #ty #ty_generics
#where_clause
{
async fn serialize<W>(&self, writer: &mut W) -> std::result::Result<(), W::Error>
where W: #crate_path::nix_daemon::ser::NixWrite
where W: #crate_path::wire::ser::NixWrite
{
use #crate_path::nix_daemon::ser::Error as _;
use #crate_path::wire::ser::Error as _;
#body
}
}
@ -207,7 +207,7 @@ fn nix_serialize_try_into(crate_path: &Path, ty: &Type) -> TokenStream {
quote_spanned! {
ty.span() =>
{
use #crate_path::nix_daemon::ser::Error;
use #crate_path::wire::ser::Error;
let other : #ty = <Self as Clone>::clone(self).try_into().map_err(Error::unsupported_data)?;
writer.write_value(&other).await
}

View file

@ -18,5 +18,3 @@ pub mod wire;
pub mod nix_daemon;
#[cfg(feature = "wire")]
pub use nix_daemon::worker_protocol;
#[cfg(feature = "wire")]
pub use nix_daemon::ProtocolVersion;

View file

@ -1,7 +1 @@
pub mod worker_protocol;
mod protocol_version;
pub use protocol_version::ProtocolVersion;
pub mod de;
pub mod ser;

View file

@ -10,7 +10,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
use crate::wire;
use super::ProtocolVersion;
use crate::wire::ProtocolVersion;
static WORKER_MAGIC_1: u64 = 0x6e697863; // "nixc"
static WORKER_MAGIC_2: u64 = 0x6478696f; // "dxio"

View file

@ -34,7 +34,7 @@ mod test {
use rstest::rstest;
use tokio_test::io::Builder;
use crate::nix_daemon::de::{NixRead, NixReader};
use crate::wire::de::{NixRead, NixReader};
#[rstest]
#[case::empty("", &hex!("0000 0000 0000 0000"))]

View file

@ -64,7 +64,7 @@ mod test {
use rstest::rstest;
use tokio_test::io::Builder;
use crate::nix_daemon::de::{NixDeserialize, NixRead, NixReader};
use crate::wire::de::{NixDeserialize, NixRead, NixReader};
#[rstest]
#[case::empty(vec![], &hex!("0000 0000 0000 0000"))]

View file

@ -45,7 +45,7 @@ mod test {
use rstest::rstest;
use tokio_test::io::Builder;
use crate::nix_daemon::de::{NixRead, NixReader};
use crate::wire::de::{NixRead, NixReader};
#[rstest]
#[case::simple_false(false, &hex!("0000 0000 0000 0000"))]

View file

@ -6,7 +6,7 @@ use std::thread;
use bytes::Bytes;
use thiserror::Error;
use crate::nix_daemon::ProtocolVersion;
use crate::wire::ProtocolVersion;
use super::NixRead;
@ -189,7 +189,7 @@ mod test {
use bytes::Bytes;
use hex_literal::hex;
use crate::nix_daemon::de::NixRead;
use crate::wire::de::NixRead;
use super::{Builder, Error};

View file

@ -8,8 +8,7 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};
use pin_project_lite::pin_project;
use tokio::io::{AsyncBufRead, AsyncRead, AsyncReadExt, ReadBuf};
use crate::nix_daemon::ProtocolVersion;
use crate::wire::EMPTY_BYTES;
use crate::wire::{ProtocolVersion, EMPTY_BYTES};
use super::{Error, NixRead};
@ -270,7 +269,7 @@ mod test {
use tokio_test::io::Builder;
use super::*;
use crate::nix_daemon::de::NixRead;
use crate::wire::de::NixRead;
#[tokio::test]
async fn test_read_u64() {

View file

@ -3,3 +3,9 @@
mod bytes;
pub use bytes::*;
mod protocol_version;
pub use protocol_version::ProtocolVersion;
pub mod de;
pub mod ser;

View file

@ -45,7 +45,7 @@ mod test {
use tokio::io::AsyncWriteExt as _;
use tokio_test::io::Builder;
use crate::nix_daemon::ser::{NixWrite, NixWriter};
use crate::wire::ser::{NixWrite, NixWriter};
#[rstest]
#[case::empty("", &hex!("0000 0000 0000 0000"))]

View file

@ -53,7 +53,7 @@ mod test {
use tokio::io::AsyncWriteExt as _;
use tokio_test::io::Builder;
use crate::nix_daemon::ser::{NixSerialize, NixWrite, NixWriter};
use crate::wire::ser::{NixSerialize, NixWrite, NixWriter};
#[rstest]
#[case::empty(vec![], &hex!("0000 0000 0000 0000"))]

View file

@ -67,7 +67,7 @@ mod test {
use tokio::io::AsyncWriteExt as _;
use tokio_test::io::Builder;
use crate::nix_daemon::ser::{NixWrite, NixWriter};
use crate::wire::ser::{NixWrite, NixWriter};
#[rstest]
#[case::simple_false(false, &hex!("0000 0000 0000 0000"))]

View file

@ -7,7 +7,7 @@ use std::thread;
use ::proptest::prelude::TestCaseError;
use thiserror::Error;
use crate::nix_daemon::ProtocolVersion;
use crate::wire::ProtocolVersion;
use super::NixWrite;
@ -491,11 +491,11 @@ mod test {
use proptest::prelude::TestCaseError;
use proptest::proptest;
use crate::nix_daemon::ser::mock::proptest::arb_extra_write;
use crate::nix_daemon::ser::mock::Operation;
use crate::nix_daemon::ser::mock::OperationType;
use crate::nix_daemon::ser::Error as _;
use crate::nix_daemon::ser::NixWrite;
use crate::wire::ser::mock::proptest::arb_extra_write;
use crate::wire::ser::mock::Operation;
use crate::wire::ser::mock::OperationType;
use crate::wire::ser::Error as _;
use crate::wire::ser::NixWrite;
use super::{Builder, Error};

View file

@ -8,9 +8,7 @@ use bytes::{Buf, BufMut, BytesMut};
use pin_project_lite::pin_project;
use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::nix_daemon::ProtocolVersion;
use crate::wire::padding_len;
use crate::wire::EMPTY_BYTES;
use crate::wire::{padding_len, ProtocolVersion, EMPTY_BYTES};
use super::{Error, NixWrite};
@ -225,7 +223,7 @@ mod test {
use tokio::io::AsyncWriteExt as _;
use tokio_test::io::Builder;
use crate::nix_daemon::ser::NixWrite;
use crate::wire::ser::NixWrite;
use super::NixWriter;

View file

@ -3,9 +3,9 @@ use clap::Subcommand;
use futures::StreamExt;
use futures::TryStreamExt;
use nix_compat::nix_daemon::de::Error;
use nix_compat::nixhash::CAHash;
use nix_compat::nixhash::NixHash;
use nix_compat::wire::de::Error;
use nix_compat::{path_info::ExportedPathInfo, store_path::StorePath};
use serde::Deserialize;
use serde::Serialize;

View file

@ -3,8 +3,8 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_listener::{self, SystemOptions, UserOptions};
use tracing::{debug, error, info, instrument, Level};
use nix_compat::wire::ProtocolVersion;
use nix_compat::worker_protocol::{self, server_handshake_client, ClientSettings, Trust};
use nix_compat::ProtocolVersion;
#[derive(Parser, Debug)]
struct Cli {