remove digest type from apis

This commit is contained in:
Robin Appelman 2020-07-18 13:40:44 +02:00
commit 6a1851eae7
4 changed files with 11 additions and 16 deletions

View file

@ -1,6 +1,5 @@
use crate::Error; use crate::Error;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use md5::Digest;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use smol_str::SmolStr; use smol_str::SmolStr;
use std::fmt; use std::fmt;
@ -24,13 +23,13 @@ pub struct Demo {
pub player_count: u8, pub player_count: u8,
pub uploader: u32, pub uploader: u32,
#[serde(deserialize_with = "hex_to_digest")] #[serde(deserialize_with = "hex_to_digest")]
pub hash: Digest, pub hash: [u8; 16],
pub backend: SmolStr, pub backend: SmolStr,
pub path: String, pub path: String,
} }
/// Deserializes a lowercase hex string to a `Vec<u8>`. /// Deserializes a lowercase hex string to a `[u8; 16]`.
pub fn hex_to_digest<'de, D>(deserializer: D) -> Result<Digest, D::Error> pub fn hex_to_digest<'de, D>(deserializer: D) -> Result<[u8; 16], D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
@ -40,12 +39,10 @@ where
let string = <&str>::deserialize(deserializer)?; let string = <&str>::deserialize(deserializer)?;
if string.len() == 0 { if string.len() == 0 {
return Ok(Digest([0; 16])); return Ok([0; 16]);
} }
<[u8; 16]>::from_hex(string) <[u8; 16]>::from_hex(string).map_err(|err| Error::custom(err.to_string()))
.map_err(|err| Error::custom(err.to_string()))
.map(Digest)
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -1,7 +1,6 @@
use crate::api::{list_demos, ListOrder, ListParams}; use crate::api::{list_demos, ListOrder, ListParams};
use crate::store::Store; use crate::store::Store;
use crate::Error; use crate::Error;
use md5::Digest;
pub struct Backup { pub struct Backup {
store: Store, store: Store,
@ -12,12 +11,12 @@ impl Backup {
Backup { store } Backup { store }
} }
fn backup_demo(&self, name: &str, url: &str, hash: Digest) -> Result<(), Error> { fn backup_demo(&self, name: &str, url: &str, hash: [u8; 16]) -> Result<(), Error> {
let resp = ureq::get(url).call(); let resp = ureq::get(url).call();
let digest = self.store.store(name, &mut resp.into_reader())?; let digest = self.store.store(name, &mut resp.into_reader())?;
if digest == hash || digest == Digest([0; 16]) { if digest == hash || digest == [0; 16] {
Ok(()) Ok(())
} else { } else {
let _ = self.store.remove(name); let _ = self.store.remove(name);

View file

@ -4,7 +4,6 @@ mod store;
use crate::backup::Backup; use crate::backup::Backup;
use crate::store::Store; use crate::store::Store;
use main_error::MainError; use main_error::MainError;
use md5::Digest;
use std::cmp::max; use std::cmp::max;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
@ -17,7 +16,7 @@ pub enum Error {
#[error("Request failed: {0}")] #[error("Request failed: {0}")]
Request(#[from] std::io::Error), Request(#[from] std::io::Error),
#[error("MD5 digest mismatch for downloaded demo, expected {expected:?}, received {got:?}")] #[error("MD5 digest mismatch for downloaded demo, expected {expected:?}, received {got:?}")]
DigestMismatch { expected: Digest, got: Digest }, DigestMismatch { expected: [u8; 16], got: [u8; 16] },
} }
fn main() -> Result<(), MainError> { fn main() -> Result<(), MainError> {

View file

@ -1,4 +1,4 @@
use md5::{Context, Digest}; use md5::Context;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::{ErrorKind, Read, Write}; use std::io::{ErrorKind, Read, Write};
@ -15,7 +15,7 @@ impl Store {
} }
} }
pub fn store(&self, name: &str, data: &mut impl Read) -> std::io::Result<Digest> { pub fn store(&self, name: &str, data: &mut impl Read) -> std::io::Result<[u8; 16]> {
let path = self.generate_path(name); let path = self.generate_path(name);
fs::create_dir_all(path.parent().unwrap())?; fs::create_dir_all(path.parent().unwrap())?;
@ -27,7 +27,7 @@ impl Store {
// copy the file and compute the digest was we go // copy the file and compute the digest was we go
loop { loop {
let len = match data.read(&mut buf) { let len = match data.read(&mut buf) {
Ok(0) => return Ok(context.compute()), Ok(0) => return Ok(context.compute().0),
Ok(len) => len, Ok(len) => len,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e), Err(e) => return Err(e),