rust_releases_io/
document.rs

1use std::path::PathBuf;
2
3/// A [`Document`] with added information about the its retrieval.
4#[derive(Debug, Eq, PartialEq)]
5pub struct RetrievedDocument {
6    document: Document,
7    retrieval_location: RetrievalLocation,
8}
9
10impl RetrievedDocument {
11    /// Create a new RetrievedDocument from the Document and its retrieval location.
12    pub fn new(document: Document, retrieval_location: RetrievalLocation) -> Self {
13        Self {
14            document,
15            retrieval_location,
16        }
17    }
18
19    /// Transforms the document into a buffer of bytes.
20    pub fn into_document(self) -> Document {
21        self.document
22    }
23
24    /// Where the document data came from.
25    pub fn retrieval_location(&self) -> &RetrievalLocation {
26        &self.retrieval_location
27    }
28}
29
30/// A `Document` represents a resource which can be used as an input to construct a `ReleaseIndex`.
31#[derive(Debug, Eq, PartialEq)]
32pub struct Document {
33    buffer: Vec<u8>,
34}
35
36impl Document {
37    /// Create a new document from a raw buffer of bytes.
38    pub fn new(buffer: Vec<u8>) -> Self {
39        Self { buffer }
40    }
41
42    /// Read-only access to the underlying buffer.
43    pub fn buffer(&self) -> &[u8] {
44        &self.buffer
45    }
46
47    /// Consumes `self` to expose the underlying buffer of bytes.
48    pub fn into_buffer(self) -> Vec<u8> {
49        self.buffer
50    }
51}
52
53/// Location a [`Document`] was retrieved from.
54#[derive(Debug, Eq, PartialEq)]
55pub enum RetrievalLocation {
56    /// A document retrieved from a remote URL.
57    RemoteUrl(String),
58    /// A document retrieved from a cache.
59    Cache(PathBuf),
60}