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

@ -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
}