debian_packaging/repository/
sink_writer.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5/*! A special repository writer that writes to a black hole. */
6
7use {
8    crate::{
9        error::{DebianError, Result},
10        io::ContentDigest,
11        repository::{
12            RepositoryPathVerification, RepositoryPathVerificationState, RepositoryWrite,
13            RepositoryWriter,
14        },
15    },
16    async_trait::async_trait,
17    futures::AsyncRead,
18    std::{borrow::Cow, pin::Pin, str::FromStr},
19};
20
21/// How [RepositoryWriter::verify_path()] should behave for [SinkWriter] instances.
22#[derive(Clone, Copy, Debug, Eq, PartialEq)]
23pub enum SinkWriterVerifyBehavior {
24    /// Path exists but an integrity check was not performed.
25    ExistsNoIntegrityCheck,
26    /// Path exists and its integrity was verified.
27    ExistsIntegrityVerified,
28    /// Path exists but its integrity doesn't match expectations.
29    ExistsIntegrityMismatch,
30    /// Path does not exist.
31    Missing,
32}
33
34impl From<SinkWriterVerifyBehavior> for RepositoryPathVerificationState {
35    fn from(v: SinkWriterVerifyBehavior) -> Self {
36        match v {
37            SinkWriterVerifyBehavior::ExistsNoIntegrityCheck => Self::ExistsNoIntegrityCheck,
38            SinkWriterVerifyBehavior::ExistsIntegrityVerified => Self::ExistsIntegrityVerified,
39            SinkWriterVerifyBehavior::ExistsIntegrityMismatch => Self::ExistsIntegrityMismatch,
40            SinkWriterVerifyBehavior::Missing => Self::Missing,
41        }
42    }
43}
44
45impl FromStr for SinkWriterVerifyBehavior {
46    type Err = DebianError;
47
48    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
49        match s {
50            "exists-no-integrity-check" => Ok(Self::ExistsNoIntegrityCheck),
51            "exists-integrity-verified" => Ok(Self::ExistsIntegrityVerified),
52            "exists-integrity-mismatch" => Ok(Self::ExistsIntegrityMismatch),
53            "missing" => Ok(Self::Missing),
54            _ => Err(DebianError::SinkWriterVerifyBehaviorUnknown(s.to_string())),
55        }
56    }
57}
58
59/// A [RepositoryWriter] that writes data to a black hole.
60pub struct SinkWriter {
61    verify_behavior: SinkWriterVerifyBehavior,
62}
63
64impl Default for SinkWriter {
65    fn default() -> Self {
66        Self {
67            verify_behavior: SinkWriterVerifyBehavior::Missing,
68        }
69    }
70}
71
72impl SinkWriter {
73    /// Set the behavior for [RepositoryWriter::verify_path()] on this instance.
74    pub fn set_verify_behavior(&mut self, behavior: SinkWriterVerifyBehavior) {
75        self.verify_behavior = behavior;
76    }
77}
78
79#[async_trait]
80impl RepositoryWriter for SinkWriter {
81    async fn verify_path<'path>(
82        &self,
83        path: &'path str,
84        _expected_content: Option<(u64, ContentDigest)>,
85    ) -> Result<RepositoryPathVerification<'path>> {
86        Ok(RepositoryPathVerification {
87            path,
88            state: self.verify_behavior.into(),
89        })
90    }
91
92    async fn write_path<'path, 'reader>(
93        &self,
94        path: Cow<'path, str>,
95        reader: Pin<Box<dyn AsyncRead + Send + 'reader>>,
96    ) -> Result<RepositoryWrite<'path>> {
97        let mut writer = futures::io::sink();
98        let bytes_written = futures::io::copy(reader, &mut writer).await?;
99
100        Ok(RepositoryWrite {
101            path,
102            bytes_written,
103        })
104    }
105}