mirror of
https://codeberg.org/demostf/backup.git
synced 2026-06-03 09:54:18 +02:00
remove digest type from apis
This commit is contained in:
parent
cb8fbf51f7
commit
6a1851eae7
4 changed files with 11 additions and 16 deletions
13
src/api.rs
13
src/api.rs
|
|
@ -1,6 +1,5 @@
|
|||
use crate::Error;
|
||||
use chrono::{DateTime, Utc};
|
||||
use md5::Digest;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use smol_str::SmolStr;
|
||||
use std::fmt;
|
||||
|
|
@ -24,13 +23,13 @@ pub struct Demo {
|
|||
pub player_count: u8,
|
||||
pub uploader: u32,
|
||||
#[serde(deserialize_with = "hex_to_digest")]
|
||||
pub hash: Digest,
|
||||
pub hash: [u8; 16],
|
||||
pub backend: SmolStr,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
/// Deserializes a lowercase hex string to a `Vec<u8>`.
|
||||
pub fn hex_to_digest<'de, D>(deserializer: D) -> Result<Digest, D::Error>
|
||||
/// Deserializes a lowercase hex string to a `[u8; 16]`.
|
||||
pub fn hex_to_digest<'de, D>(deserializer: D) -> Result<[u8; 16], D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
|
|
@ -40,12 +39,10 @@ where
|
|||
let string = <&str>::deserialize(deserializer)?;
|
||||
|
||||
if string.len() == 0 {
|
||||
return Ok(Digest([0; 16]));
|
||||
return Ok([0; 16]);
|
||||
}
|
||||
|
||||
<[u8; 16]>::from_hex(string)
|
||||
.map_err(|err| Error::custom(err.to_string()))
|
||||
.map(Digest)
|
||||
<[u8; 16]>::from_hex(string).map_err(|err| Error::custom(err.to_string()))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
use crate::api::{list_demos, ListOrder, ListParams};
|
||||
use crate::store::Store;
|
||||
use crate::Error;
|
||||
use md5::Digest;
|
||||
|
||||
pub struct Backup {
|
||||
store: Store,
|
||||
|
|
@ -12,12 +11,12 @@ impl Backup {
|
|||
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 digest = self.store.store(name, &mut resp.into_reader())?;
|
||||
|
||||
if digest == hash || digest == Digest([0; 16]) {
|
||||
if digest == hash || digest == [0; 16] {
|
||||
Ok(())
|
||||
} else {
|
||||
let _ = self.store.remove(name);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ mod store;
|
|||
use crate::backup::Backup;
|
||||
use crate::store::Store;
|
||||
use main_error::MainError;
|
||||
use md5::Digest;
|
||||
use std::cmp::max;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
|
@ -17,7 +16,7 @@ pub enum Error {
|
|||
#[error("Request failed: {0}")]
|
||||
Request(#[from] std::io::Error),
|
||||
#[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> {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use md5::{Context, Digest};
|
||||
use md5::Context;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
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);
|
||||
fs::create_dir_all(path.parent().unwrap())?;
|
||||
|
||||
|
|
@ -27,7 +27,7 @@ impl Store {
|
|||
// copy the file and compute the digest was we go
|
||||
loop {
|
||||
let len = match data.read(&mut buf) {
|
||||
Ok(0) => return Ok(context.compute()),
|
||||
Ok(0) => return Ok(context.compute().0),
|
||||
Ok(len) => len,
|
||||
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
||||
Err(e) => return Err(e),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue