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
// Copyright 2021 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

use std::{
    hash::{Hash, Hasher},
    io,
    str::FromStr,
};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum RequestError {
    #[error("apt command failed")]
    Command(#[from] io::Error),
    #[error("uri not found in output: {0}")]
    UriNotFound(String),
    #[error("invalid URI value: {0}")]
    UriInvalid(String),
    #[error("name not found in output: {0}")]
    NameNotFound(String),
    #[error("size not found in output: {0}")]
    SizeNotFound(String),
    #[error("size in output could not be parsed as an integer: {0}")]
    SizeParse(String),
    #[error("checksum not found in output: {0}")]
    ChecksumNotFound(String),
    #[error("unknown checksum for print-uri output: {0}")]
    UnknownChecksum(String),
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum RequestChecksum {
    Md5(String),
    Sha1(String)
}

#[derive(Debug, Clone, Eq)]
pub struct Request {
    pub uri: String,
    pub name: String,
    pub size: u64,
    pub checksum: RequestChecksum,
}

impl PartialEq for Request {
    fn eq(&self, other: &Self) -> bool {
        self.uri == other.uri
    }
}

impl Hash for Request {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.uri.hash(state);
    }
}

impl FromStr for Request {
    type Err = RequestError;

    fn from_str(line: &str) -> Result<Self, Self::Err> {
        let mut words = line.split_whitespace();

        let mut uri = words
            .next()
            .ok_or_else(|| RequestError::UriNotFound(line.into()))?;

        // We need to remove the single quotes that apt-get encloses the URI within.
        if uri.len() <= 3 {
            return Err(RequestError::UriInvalid(uri.into()));
        } else {
            uri = &uri[1..uri.len() - 1];
        }

        let name = words
            .next()
            .ok_or_else(|| RequestError::NameNotFound(line.into()))?;
        let size = words
            .next()
            .ok_or_else(|| RequestError::SizeNotFound(line.into()))?;
        let size = size
            .parse::<u64>()
            .map_err(|_| RequestError::SizeParse(size.into()))?;

        let checksum_string = words
            .next()
            .ok_or_else(|| RequestError::ChecksumNotFound(line.into()))?;

        let checksum = if let Some(value) = checksum_string.strip_prefix("MD5Sum:") {
            RequestChecksum::Md5(value.to_owned())
        } else if let Some(value) = checksum_string.strip_prefix("SHA1:") {
            RequestChecksum::Sha1(value.to_owned())
        } else {
            return Err(RequestError::UnknownChecksum(checksum_string.into()));
        };

        Ok(Request {
            uri: uri.into(),
            name: name.into(),
            size,
            checksum,
        })
    }
}