quilt-rs 0.27.3

Rust library for accessing Quilt data packages.
Documentation
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//!
//! Module that contains various structs and helpers to work with `.quilt/lineage.json`.

use std::collections::BTreeMap;
use std::path::Path;
use std::path::PathBuf;

use serde::Deserialize;
use serde::Serialize;
use tracing::log;

#[cfg(test)]
use tempfile::TempDir;

use crate::io::storage::Storage;
use crate::io::storage::StorageExt;
use crate::paths;
use crate::uri::Namespace;
use crate::Error;
use crate::Res;

mod status;
pub use status::Change;
pub use status::ChangeSet;
pub use status::InstalledPackageStatus;
pub use status::UpstreamState;

mod package;
pub use package::CommitState;
pub use package::LineagePaths;
pub use package::PackageLineage;
pub use package::PathState;

mod home;
pub use home::Home;

/// It's essentially just a map of `PackageLineage`.
/// Represents the contents of `.quilt/data.json`
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct DomainLineage {
    #[serde(default = "BTreeMap::new")]
    pub packages: BTreeMap<Namespace, PackageLineage>,
    #[serde(default)]
    pub home: Home,
}

impl DomainLineage {
    pub fn new(home_dir: impl AsRef<Path>) -> Self {
        DomainLineage {
            packages: BTreeMap::new(),
            home: Home::from(home_dir),
        }
    }

    /// Returns a sorted vector of all namespaces in the lineage
    pub fn namespaces(&self) -> Vec<Namespace> {
        let mut namespaces: Vec<Namespace> = self.packages.keys().cloned().collect();
        namespaces.sort();
        namespaces
    }

    #[cfg(test)]
    pub fn from_temp_dir() -> Res<(Self, tempfile::TempDir)> {
        let temp_dir = TempDir::new()?;
        Ok((DomainLineage::new(temp_dir.path()), temp_dir))
    }
}

impl AsRef<PathBuf> for DomainLineage {
    fn as_ref(&self) -> &PathBuf {
        self.home.as_ref()
    }
}

impl DomainLineage {
    pub fn from_slice(input: &[u8]) -> Res<Self> {
        let result: Result<Self, serde_json::Error> = serde_json::from_slice(input);

        match result {
            Ok(lineage) => {
                if lineage.as_ref().as_os_str().is_empty() {
                    return Err(Error::LineageMissingHome);
                }
                Ok(lineage)
            }
            Err(err) => {
                log::error!("Failed to parse DomainLineage from `{input:?}`");
                Err(Error::LineageParse(err))
            }
        }
    }
}

/// Wrapper for reading and writing `DomainLineage`
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DomainLineageIo {
    path: PathBuf,
}

// TODO impl std::io::Write and std::io::Read for DomainLineageIo
impl DomainLineageIo {
    pub fn new(path: PathBuf) -> Self {
        DomainLineageIo { path }
    }

    pub async fn read(&self, storage: &(impl Storage + Sync)) -> Res<DomainLineage> {
        match storage.read_bytes(&self.path).await {
            Ok(bytes) => DomainLineage::from_slice(&bytes),
            Err(_) if !storage.exists(&self.path).await => Err(Error::LineageMissing),
            Err(e) => Err(e),
        }
    }

    /// Read a specific package lineage from the domain lineage
    pub async fn read_package_lineage(
        &self,
        storage: &(impl Storage + Sync),
        namespace: &Namespace,
    ) -> Res<(PathBuf, PackageLineage)> {
        let domain_lineage = self.read(storage).await?;

        match domain_lineage.packages.get(namespace) {
            Some(package_lineage) => {
                let package_home = paths::package_home(&domain_lineage.home, namespace);
                Ok((package_home, package_lineage.clone()))
            }
            None => Err(Error::PackageNotInstalled(namespace.clone())),
        }
    }

    /// Write a specific package lineage to the domain lineage
    pub async fn write_package_lineage(
        &self,
        storage: &(impl Storage + Sync),
        namespace: &Namespace,
        package_lineage: PackageLineage,
    ) -> Res<PackageLineage> {
        let mut domain_lineage = self.read(storage).await?;
        domain_lineage
            .packages
            .insert(namespace.clone(), package_lineage.clone());
        self.write(storage, domain_lineage).await?;
        Ok(package_lineage)
    }

    pub async fn set_home(
        &self,
        storage: &(impl Storage + Sync),
        home: impl AsRef<Path>,
    ) -> Res<DomainLineage> {
        match storage.read_bytes(&self.path).await {
            Ok(bytes) => {
                let mut lineage = DomainLineage::from_slice(&bytes)?;
                lineage.home = home.into();
                self.write(storage, lineage).await
            }
            Err(_) if !storage.exists(&self.path).await => {
                self.write(storage, DomainLineage::new(home)).await
            }
            Err(e) => Err(e),
        }
    }

    pub async fn write(
        &self,
        storage: &(impl Storage + Sync),
        lineage: DomainLineage,
    ) -> Res<DomainLineage> {
        let contents = serde_json::to_string_pretty(&lineage)?;
        storage
            .write_byte_stream(self.path.clone(), contents.into_bytes().into())
            .await?;
        Ok(lineage)
    }

    pub fn create_package_lineage(&self, namespace: Namespace) -> PackageLineageIo {
        PackageLineageIo::new(self.clone(), namespace)
    }
}

/// Wrapper for reading and writing `PackageLineage`
/// It re-uses `DomainLineageIo`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageLineageIo {
    domain_lineage: DomainLineageIo,
    namespace: Namespace,
}

impl PackageLineageIo {
    pub fn new(domain_lineage: DomainLineageIo, namespace: Namespace) -> Self {
        PackageLineageIo {
            domain_lineage,
            namespace,
        }
    }

    pub async fn read(&self, storage: &(impl Storage + Sync)) -> Res<(PathBuf, PackageLineage)> {
        self.domain_lineage
            .read_package_lineage(storage, &self.namespace)
            .await
    }

    pub async fn package_home(&self, storage: &(impl Storage + Sync)) -> Res<PathBuf> {
        Ok(self
            .domain_home(storage)
            .await?
            .join(self.namespace.to_string()))
    }

    pub async fn domain_home(&self, storage: &(impl Storage + Sync)) -> Res<Home> {
        let domain_lineage = self.domain_lineage.read(storage).await?;
        Ok(domain_lineage.home)
    }

    pub async fn write(
        &self,
        storage: &(impl Storage + Sync),
        lineage: PackageLineage,
    ) -> Res<PackageLineage> {
        self.domain_lineage
            .write_package_lineage(storage, &self.namespace, lineage)
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use test_log::test;

    use aws_sdk_s3::primitives::ByteStream;
    use aws_smithy_types::base64;

    use crate::checksum::Sha256ChunkedHash;
    use crate::io::storage::mocks::MockStorage;
    use crate::uri::ManifestUri;

    #[test]
    fn test_syntax_error() {
        assert_eq!(
            DomainLineage::from_slice(b"err").unwrap_err().to_string(),
            "Failed to parse lineage file: expected value at line 1 column 1".to_string()
        );
    }

    #[test]
    fn test_wrong_key() {
        // NOTE: @fiskus I don't think this is developer friendly
        //       I'd like to remove serde(default), so this test fails
        assert!(DomainLineage::from_slice(br#"{"notkey": 123}"#).is_err());
    }

    #[test]
    fn test_wrong_value() {
        assert!(DomainLineage::from_slice(br#"{"packages": 123}"#)
            .unwrap_err()
            .to_string()
            .starts_with("Failed to parse lineage file: invalid type:"));
    }

    #[test]
    fn test_missing_working_directory() {
        assert_eq!(
            DomainLineage::from_slice(br###"{"packages":{}}"###)
                .unwrap_err()
                .to_string(),
            "Domain lineage missing Home directory".to_string()
        );
    }

    #[test]
    fn test_with_working_directory() -> Res {
        let lineage =
            DomainLineage::from_slice(br###"{"packages":{},"home":"/tmp/working_dir"}"###).unwrap();
        assert_eq!(lineage.as_ref(), &PathBuf::from("/tmp/working_dir"));
        Ok(())
    }

    #[test]
    fn test_domain_lineage_from_temp_dir() -> Res {
        let (lineage, temp_dir) = DomainLineage::from_temp_dir()?;
        assert_eq!(lineage.as_ref(), &temp_dir.path().to_path_buf());
        assert!(lineage.packages.is_empty());
        Ok(())
    }

    #[test]
    fn test_namespaces() -> Res {
        let mut lineage = DomainLineage::new("/tmp/home");

        // Empty lineage should return empty vector
        assert!(lineage.namespaces().is_empty());

        // Add some packages
        lineage
            .packages
            .insert(Namespace::from(("foo", "bar")), PackageLineage::default());
        lineage
            .packages
            .insert(Namespace::from(("abc", "xyz")), PackageLineage::default());
        lineage.packages.insert(
            Namespace::from(("test", "package")),
            PackageLineage::default(),
        );

        // Check that namespaces are returned in sorted order
        let namespaces = lineage.namespaces();
        assert_eq!(namespaces.len(), 3);
        assert_eq!(namespaces[0], Namespace::from(("abc", "xyz")));
        assert_eq!(namespaces[1], Namespace::from(("foo", "bar")));
        assert_eq!(namespaces[2], Namespace::from(("test", "package")));

        Ok(())
    }

    #[test(tokio::test)]
    async fn test_domain_lineage_from_file() -> Res {
        let storage = MockStorage::default();
        let file_path = PathBuf::from("foo");
        storage
            .write_byte_stream(
                &file_path,
                ByteStream::from_static(br###"{"packages":{},"home":"/home/directory"}"###),
            )
            .await?;
        let lineage = DomainLineageIo::new(file_path).read(&storage).await?;
        assert_eq!(lineage, DomainLineage::new("/home/directory"));
        Ok(())
    }

    #[test(tokio::test)]
    async fn test_domain_lineage_from_nothing() -> Res {
        let storage = MockStorage::default();
        let lineage = DomainLineageIo::new(PathBuf::from("does-not-exist"))
            .read(&storage)
            .await
            .unwrap_err();
        assert!(matches!(lineage, Error::LineageMissing));
        Ok(())
    }

    #[test(tokio::test)]
    async fn test_domain_lineage_write() -> Res {
        let storage = MockStorage::default();
        let file_path = PathBuf::from("foo");
        assert!(!storage.exists(&file_path).await);
        let bytes = "0123456789abcdef".as_bytes();
        let working_dir = PathBuf::from("/tmp/working_dir");
        DomainLineageIo::new(file_path.clone())
            .write(
                &storage,
                DomainLineage {
                    home: Home::new(working_dir),
                    packages: BTreeMap::from([(
                        ("foo", "bar").into(),
                        PackageLineage {
                            commit: None,
                            remote: ManifestUri {
                                bucket: "bucket".to_string(),
                                namespace: ("foo", "bar").into(),
                                hash: "abcdef".to_string(),
                                origin: None,
                            },
                            base_hash: "abcdef".to_string(),
                            latest_hash: "abcdef".to_string(),
                            paths: BTreeMap::from([(
                                PathBuf::from("foo"),
                                PathState {
                                    timestamp: chrono::DateTime::from_timestamp_millis(
                                        1737031820534,
                                    )
                                    .unwrap(),
                                    hash: Sha256ChunkedHash::from_async_read(
                                        bytes,
                                        bytes.len() as u64,
                                    )
                                    .await?
                                    .into(),
                                },
                            )]),
                        },
                    )]),
                },
            )
            .await?;
        assert!(storage.exists(&file_path).await);
        let file_contents = storage.read_bytes(&file_path).await?;
        let lineage = DomainLineage::from_slice(&file_contents)?;

        assert_eq!(lineage.as_ref(), &PathBuf::from("/tmp/working_dir"));

        let multihash_from_lineage = lineage
            .packages
            .get(&(("foo".to_string(), "bar".to_string()).into()))
            .unwrap()
            .paths
            .get(&PathBuf::from("foo"))
            .unwrap()
            .hash;
        let hash_from_lineage = base64::encode(multihash_from_lineage.digest());
        assert_eq!(
            hash_from_lineage,
            "Xb1PbjJeWof4zD7zuHc9PI7sLiz/Ykj4gphlaZEt3xA="
        );
        Ok(())
    }

    #[test(tokio::test)]
    async fn test_read_package_lineage() -> Res {
        let storage = MockStorage::default();
        let file_path = PathBuf::from("lineage.json");
        let namespace = Namespace::from(("foo", "bar"));
        let package_lineage = PackageLineage {
            commit: None,
            remote: ManifestUri {
                bucket: "bucket".to_string(),
                namespace: namespace.clone(),
                hash: "abcdef".to_string(),
                origin: None,
            },
            base_hash: "abcdef".to_string(),
            latest_hash: "abcdef".to_string(),
            paths: BTreeMap::new(),
        };

        // Create a domain lineage with a package
        let lineage = DomainLineage {
            home: Home::from("/home/user/quilt"),
            packages: BTreeMap::from([(namespace.clone(), package_lineage.clone())]),
        };

        // Write it to storage
        let lineage_io = DomainLineageIo::new(file_path.clone());
        lineage_io.write(&storage, lineage).await?;

        // Read the package lineage
        let (package_home, read_package_lineage) = lineage_io
            .read_package_lineage(&storage, &namespace)
            .await?;

        // Verify the results
        assert_eq!(package_home, PathBuf::from("/home/user/quilt/foo/bar"));
        assert_eq!(read_package_lineage, package_lineage);

        // Try reading a non-existent package
        let non_existent = Namespace::from(("does", "notexist"));
        let result = lineage_io
            .read_package_lineage(&storage, &non_existent)
            .await;
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "The given package is not installed: does/notexist"
        );

        Ok(())
    }

    #[test(tokio::test)]
    async fn test_write_package_lineage() -> Res {
        let storage = MockStorage::default();
        let file_path = PathBuf::from("lineage.json");
        let lineage_io = DomainLineageIo::new(file_path.clone());

        // Create an initial domain lineage with home directory
        let initial_lineage = DomainLineage {
            home: Home::from("/home/user/quilt"),
            packages: BTreeMap::new(),
        };

        // Write the initial lineage
        lineage_io.write(&storage, initial_lineage).await?;

        // Create a package lineage to write
        let namespace = Namespace::from(("foo", "bar"));
        let package_lineage = PackageLineage {
            commit: None,
            remote: ManifestUri {
                bucket: "bucket".to_string(),
                namespace: namespace.clone(),
                hash: "abcdef".to_string(),
                origin: None,
            },
            base_hash: "abcdef".to_string(),
            latest_hash: "abcdef".to_string(),
            paths: BTreeMap::new(),
        };

        // Write the package lineage
        let written_lineage = lineage_io
            .write_package_lineage(&storage, &namespace, package_lineage.clone())
            .await?;

        // Verify the written lineage matches what we provided
        assert_eq!(written_lineage, package_lineage);

        // Read the domain lineage to verify the package was added
        let domain_lineage = lineage_io.read(&storage).await?;
        assert_eq!(domain_lineage.packages.len(), 1);
        assert!(domain_lineage.packages.contains_key(&namespace));
        assert_eq!(
            domain_lineage.packages.get(&namespace).unwrap(),
            &package_lineage
        );

        // Update the package lineage
        let updated_package_lineage = PackageLineage {
            commit: Some(CommitState {
                timestamp: chrono::Utc::now(),
                hash: "".to_string(),
                prev_hashes: Vec::new(),
            }),
            ..package_lineage.clone()
        };

        // Write the updated package lineage
        lineage_io
            .write_package_lineage(&storage, &namespace, updated_package_lineage.clone())
            .await?;

        // Read the domain lineage again to verify the update
        let updated_domain_lineage = lineage_io.read(&storage).await?;
        assert_eq!(updated_domain_lineage.packages.len(), 1);
        assert!(updated_domain_lineage.packages.contains_key(&namespace));
        assert_eq!(
            updated_domain_lineage.packages.get(&namespace).unwrap(),
            &updated_package_lineage
        );

        Ok(())
    }

    #[test(tokio::test)]
    async fn test_domain_lineage_create_package_lineage() -> Res {
        let namespace = ("foo", "bar");
        let domain_lineage = DomainLineageIo::default();
        let lineage = domain_lineage.create_package_lineage(namespace.into());
        assert_eq!(
            lineage,
            PackageLineageIo {
                namespace: namespace.into(),
                domain_lineage,
            }
        );
        Ok(())
    }
}