1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use crate::wire::de::Error;
use crate::{
    narinfo::Signature,
    nixhash::CAHash,
    store_path::StorePath,
    wire::{
        de::{NixDeserialize, NixRead},
        ser::{NixSerialize, NixWrite},
    },
};
use nix_compat_derive::{NixDeserialize, NixSerialize};
use std::future::Future;

/// Marker type that consumes/sends and ignores a u64.
#[derive(Clone, Debug, NixDeserialize, NixSerialize)]
#[nix(from = "u64", into = "u64")]
pub struct IgnoredZero;
impl From<u64> for IgnoredZero {
    fn from(_: u64) -> Self {
        IgnoredZero
    }
}

impl From<IgnoredZero> for u64 {
    fn from(_: IgnoredZero) -> Self {
        0
    }
}

#[derive(Debug, NixSerialize)]
pub struct TraceLine {
    have_pos: IgnoredZero,
    hint: String,
}

/// Represents an error returned by the nix-daemon to its client.
///
/// Adheres to the format described in serialization.md
#[derive(NixSerialize)]
pub struct NixError {
    #[nix(version = "26..")]
    type_: &'static str,

    #[nix(version = "26..")]
    level: u64,

    #[nix(version = "26..")]
    name: &'static str,

    msg: String,
    #[nix(version = "26..")]
    have_pos: IgnoredZero,

    #[nix(version = "26..")]
    traces: Vec<TraceLine>,

    #[nix(version = "..=25")]
    exit_status: u64,
}

impl NixError {
    pub fn new(msg: String) -> Self {
        Self {
            type_: "Error",
            level: 0, // error
            name: "Error",
            msg,
            have_pos: IgnoredZero {},
            traces: vec![],
            exit_status: 1,
        }
    }
}

nix_compat_derive::nix_serialize_remote!(#[nix(display)] Signature<String>);

impl NixDeserialize for Signature<String> {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        let value: Option<String> = reader.try_read_value().await?;
        match value {
            Some(value) => Ok(Some(
                Signature::<String>::parse(&value).map_err(R::Error::invalid_data)?,
            )),
            None => Ok(None),
        }
    }
}

impl NixSerialize for CAHash {
    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
    where
        W: NixWrite,
    {
        writer.write_value(&self.to_nix_nixbase32_string()).await
    }
}

impl NixSerialize for Option<CAHash> {
    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
    where
        W: NixWrite,
    {
        match self {
            Some(value) => writer.write_value(value).await,
            None => writer.write_value("").await,
        }
    }
}

impl NixDeserialize for CAHash {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        let value: Option<String> = reader.try_read_value().await?;
        match value {
            Some(value) => Ok(Some(CAHash::from_nix_hex_str(&value).ok_or_else(|| {
                R::Error::invalid_data(format!("Invalid cahash {}", value))
            })?)),
            None => Ok(None),
        }
    }
}

impl NixDeserialize for Option<CAHash> {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        let value: Option<String> = reader.try_read_value().await?;
        match value {
            Some(value) => {
                if value.is_empty() {
                    Ok(None)
                } else {
                    Ok(Some(Some(CAHash::from_nix_hex_str(&value).ok_or_else(
                        || R::Error::invalid_data(format!("Invalid cahash {}", value)),
                    )?)))
                }
            }
            None => Ok(None),
        }
    }
}

impl NixSerialize for Option<UnkeyedValidPathInfo> {
    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
    where
        W: NixWrite,
    {
        match self {
            Some(value) => {
                writer.write_value(&true).await?;
                writer.write_value(value).await
            }
            None => writer.write_value(&false).await,
        }
    }
}

// Custom implementation since FromStr does not use from_absolute_path
impl NixDeserialize for StorePath<String> {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        use crate::wire::de::Error;
        if let Some(buf) = reader.try_read_bytes().await? {
            let result = StorePath::<String>::from_absolute_path(&buf);
            result.map(Some).map_err(R::Error::invalid_data)
        } else {
            Ok(None)
        }
    }
}

impl NixDeserialize for Option<StorePath<String>> {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        use crate::wire::de::Error;
        if let Some(buf) = reader.try_read_bytes().await? {
            if buf.is_empty() {
                Ok(Some(None))
            } else {
                let result = StorePath::<String>::from_absolute_path(&buf);
                result
                    .map(|r| Some(Some(r)))
                    .map_err(R::Error::invalid_data)
            }
        } else {
            Ok(Some(None))
        }
    }
}

// Custom implementation since Display does not use absolute paths.
impl<S> NixSerialize for StorePath<S>
where
    S: AsRef<str>,
{
    fn serialize<W>(&self, writer: &mut W) -> impl Future<Output = Result<(), W::Error>> + Send
    where
        W: NixWrite,
    {
        let sp = self.to_absolute_path();
        async move { writer.write_value(&sp).await }
    }
}

// Writes StorePath or an empty string.
impl NixSerialize for Option<StorePath<String>> {
    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
    where
        W: NixWrite,
    {
        match self {
            Some(value) => writer.write_value(value).await,
            None => writer.write_value("").await,
        }
    }
}

#[derive(NixSerialize, Debug, Clone, Default, PartialEq)]
pub struct UnkeyedValidPathInfo {
    pub deriver: Option<StorePath<String>>,
    pub nar_hash: String,
    pub references: Vec<StorePath<String>>,
    pub registration_time: u64,
    pub nar_size: u64,
    pub ultimate: bool,
    pub signatures: Vec<Signature<String>>,
    pub ca: Option<CAHash>,
}

/// Request tupe for [super::worker_protocol::Operation::QueryValidPaths]
#[derive(NixDeserialize)]
pub struct QueryValidPaths {
    // Paths to query
    pub paths: Vec<StorePath<String>>,

    // Whether to try and substitute the paths.
    #[nix(version = "27..")]
    pub substitute: bool,
}

/// newtype wrapper for the byte array that correctly implements NixSerialize, NixDeserialize.
#[derive(Debug)]
pub struct NarHash([u8; 32]);

impl std::ops::Deref for NarHash {
    type Target = [u8; 32];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl NixDeserialize for NarHash {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        if let Some(bytes) = reader.try_read_bytes().await? {
            let result = data_encoding::HEXLOWER
                .decode(bytes.as_ref())
                .map_err(R::Error::invalid_data)?;
            Ok(Some(NarHash(result.try_into().map_err(|_| {
                R::Error::invalid_data("incorrect length")
            })?)))
        } else {
            Ok(None)
        }
    }
}

/// Request type for [super::worker_protocol::Operation::AddToStoreNar]
#[derive(NixDeserialize, Debug)]
pub struct AddToStoreNarRequest {
    // - path :: [StorePath][se-StorePath]
    pub path: StorePath<String>,
    // - deriver :: [OptStorePath][se-OptStorePath]
    pub deriver: Option<StorePath<String>>,
    // - narHash :: [NARHash][se-NARHash] - always sha256
    pub nar_hash: NarHash,
    // - references :: [Set][se-Set] of [StorePath][se-StorePath]
    pub references: Vec<StorePath<String>>,
    // - registrationTime :: [Time][se-Time]
    pub registration_time: u64,
    // - narSize :: [UInt64][se-UInt64]
    pub nar_size: u64,
    // - ultimate :: [Bool64][se-Bool64]
    pub ultimate: bool,
    // - signatures :: [Set][se-Set] of [Signature][se-Signature]
    pub signatures: Vec<Signature<String>>,
    // - ca :: [OptContentAddress][se-OptContentAddress]
    pub ca: Option<CAHash>,
    // - repair :: [Bool64][se-Bool64]
    pub repair: bool,
    // - dontCheckSigs :: [Bool64][se-Bool64]
    pub dont_check_sigs: bool,
}