async_fetcher/
source.rs

1// Copyright 2021-2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4use std::path::Path;
5use std::sync::Arc;
6
7/// Information about a source being fetched.
8#[derive(Debug)]
9pub struct Source {
10    /// URLs whereby the file can be found.
11    pub urls: Arc<[Box<str>]>,
12
13    /// Where the file shall ultimately be fetched to.
14    pub dest: Arc<Path>,
15
16    /// Where partial files should be stored.
17    pub part: Option<Arc<Path>>,
18}
19
20impl Source {
21    pub fn builder(dest: Arc<Path>, url: Box<str>) -> SourceBuilder {
22        SourceBuilder::new(dest, url)
23    }
24
25    pub fn new(urls: Arc<[Box<str>]>, dest: Arc<Path>) -> Self {
26        Self {
27            urls,
28            dest,
29            part: None,
30        }
31    }
32
33    /// Sets the partial destination of a source.
34    pub fn set_part(&mut self, part: Option<Arc<Path>>) {
35        self.part = part;
36    }
37}
38
39/// Constructs a `Source`.
40pub struct SourceBuilder {
41    urls: Vec<Box<str>>,
42    dest: Arc<Path>,
43    part: Option<Arc<Path>>,
44}
45
46impl SourceBuilder {
47    pub fn new(dest: Arc<Path>, url: Box<str>) -> Self {
48        SourceBuilder {
49            dest,
50            urls: vec![url],
51            part: None,
52        }
53    }
54
55    /// A mirror where the source can be located.
56    pub fn append_url(mut self, url: Box<str>) -> Self {
57        self.urls.push(url);
58        self
59    }
60
61    /// A partial destination for a source.
62    pub fn partial(mut self, part: Arc<Path>) -> Self {
63        self.part = Some(part);
64        self
65    }
66
67    pub fn build(self) -> Source {
68        Source {
69            urls: Arc::from(self.urls),
70            dest: self.dest,
71            part: self.part,
72        }
73    }
74}