Async validate trait to hide the mutating verify fn

This commit is contained in:
projectmoon 2024-04-01 14:17:25 +02:00
parent 62d219284c
commit 89559edd9e
5 changed files with 302 additions and 8 deletions

283
Cargo.lock generated
View File

@ -70,6 +70,15 @@ version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@ -88,12 +97,55 @@ version = "3.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"
[[package]]
name = "bytecount"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1e5f035d16fc623ae5f74981db80a439803888314e3a555fd6f04acd51a3205"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "bytes"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "camino"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c"
dependencies = [
"serde",
]
[[package]]
name = "cargo-platform"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24b1f0365a6c6bb4020cd05806fd0d33c44d38046b8bd7f0e40814b9763cabfc"
dependencies = [
"serde",
]
[[package]]
name = "cargo_metadata"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4acbb09d9ee8e23699b9634375c72795d095bf268439da88562cf9b501f181fa"
dependencies = [
"camino",
"cargo-platform",
"semver",
"serde",
"serde_json",
]
[[package]]
name = "cc"
version = "1.0.90"
@ -115,6 +167,31 @@ dependencies = [
"powerfmt",
]
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "error-chain"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc"
dependencies = [
"version_check",
]
[[package]]
name = "fastrand"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984"
[[package]]
name = "fluffer"
version = "4.0.0"
@ -192,6 +269,8 @@ dependencies = [
"async-trait",
"fluffer",
"germ",
"native_db",
"native_model",
"thiserror",
"tokio",
"url",
@ -227,6 +306,12 @@ version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -243,6 +328,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "js-sys"
version = "0.3.69"
@ -264,6 +355,12 @@ version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "linux-raw-sys"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
[[package]]
name = "lock_api"
version = "0.4.11"
@ -328,6 +425,57 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "native_db"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76960432093260203b73b1740426362abac4883c638f0f6c2648eb60b9e07cfe"
dependencies = [
"native_db_macro",
"native_model",
"redb",
"serde",
"skeptic",
"thiserror",
]
[[package]]
name = "native_db_macro"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdacf1ab4dfa5e0d5dfdd84f4ffde4e2f6943570855d1f9f95d93bede1633afd"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "native_model"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72e5ce1c150701ce50e65ed2a522c2993dac12022b6395812c2fff25f5fcfddf"
dependencies = [
"anyhow",
"bincode",
"native_model_macro",
"serde",
"skeptic",
"thiserror",
"zerocopy",
]
[[package]]
name = "native_model_macro"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cab429fe0bfba9238e3ee725211430f3235603dc535f84ca85263427e91e235"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "num-conv"
version = "0.1.0"
@ -469,6 +617,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "pulldown-cmark"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57206b407293d2bcd3af849ce869d52068623f19e1b5ff8e8778e3309439682b"
dependencies = [
"bitflags 2.5.0",
"memchr",
"unicase",
]
[[package]]
name = "quote"
version = "1.0.35"
@ -490,6 +649,15 @@ dependencies = [
"yasna",
]
[[package]]
name = "redb"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd7f82ecd6ba647a39dd1a7172b8a1cd9453c0adee6da20cb553d83a9a460fa5"
dependencies = [
"libc",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
@ -564,6 +732,19 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.38.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89"
dependencies = [
"bitflags 2.5.0",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
]
[[package]]
name = "rustls"
version = "0.21.10"
@ -586,6 +767,21 @@ dependencies = [
"untrusted 0.9.0",
]
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "sanitize-filename"
version = "0.5.0"
@ -612,6 +808,15 @@ dependencies = [
"untrusted 0.9.0",
]
[[package]]
name = "semver"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca"
dependencies = [
"serde",
]
[[package]]
name = "serde"
version = "1.0.197"
@ -632,6 +837,17 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
@ -641,6 +857,21 @@ dependencies = [
"libc",
]
[[package]]
name = "skeptic"
version = "0.13.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16d23b015676c90a0f01c197bfdc786c20342c73a0afdda9025adb0bc42940a8"
dependencies = [
"bytecount",
"cargo_metadata",
"error-chain",
"glob",
"pulldown-cmark",
"tempfile",
"walkdir",
]
[[package]]
name = "smallvec"
version = "1.13.2"
@ -680,6 +911,18 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
dependencies = [
"cfg-if",
"fastrand",
"rustix",
"windows-sys 0.52.0",
]
[[package]]
name = "thiserror"
version = "1.0.58"
@ -872,6 +1115,16 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
@ -964,6 +1217,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
@ -1110,3 +1372,24 @@ checksum = "e17bb3549cc1321ae1296b9cdc2698e2b6cb1992adfa19a8c72e5b7a738f44cd"
dependencies = [
"time",
]
[[package]]
name = "zerocopy"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
dependencies = [
"byteorder",
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@ -10,6 +10,8 @@ anyhow = "1.0.81"
async-trait = "0.1.79"
fluffer = "4"
germ = "0.4"
native_db = "0.5"
native_model = "0.4"
thiserror = "1.0.58"
tokio = {version = "1.37", features = [ "full" ] }
url = "2.5.0"

View File

@ -1,8 +1,7 @@
use crate::error::GementionError;
use crate::models::mentions::{C2SMentionRequest, Mention};
use crate::models::web::MentionResponse;
use crate::validation::Validation;
use crate::verification::verify;
use crate::validation::{AsyncValidation, Validation};
use fluffer::Client;
/// Receive a client-to-server Gemention: i.e. a user posting a
@ -12,8 +11,7 @@ pub(crate) async fn receive_c2s_gemention(
client: Client,
) -> Result<MentionResponse, GementionError> {
let mention_upload = C2SMentionRequest::try_from(&client)?.validate()?;
let mut mention = Mention::try_from(mention_upload)?;
verify(&mut mention).await?;
let mention = Mention::try_from(mention_upload)?.validate_async().await?;
Ok(MentionResponse::from(mention))
}
@ -25,8 +23,7 @@ pub(crate) async fn receive_s2s_gemention(
client: Client,
) -> Result<MentionResponse, GementionError> {
let mention_upload = C2SMentionRequest::try_from(&client)?;
let mut mention = Mention::try_from(mention_upload)?;
verify(&mut mention).await?;
let mention = Mention::try_from(mention_upload)?.validate_async().await?;
Ok(MentionResponse::from(mention))
}

View File

@ -5,3 +5,9 @@ pub trait Validation {
fn validate(self) -> Result<Self, Self::Error> where Self: Sized;
}
pub trait AsyncValidation {
type Error;
async fn validate_async(self) -> Result<Self, Self::Error> where Self: Sized;
}

View File

@ -6,6 +6,7 @@ use url::Url;
use crate::error::GementionError;
use crate::models::mentions::Mention;
use crate::models::verification::*;
use crate::validation::AsyncValidation;
/// Normalize URL by making sure it starts with the right protocol and
/// has a port number.
@ -109,6 +110,11 @@ async fn verify_mention(mention: &mut Mention) -> Result<(), GementionError> {
Ok(())
}
pub(crate) async fn verify(mention: &mut Mention) -> Result<(), GementionError> {
verify_mention(mention).await
impl AsyncValidation for Mention {
type Error = GementionError;
async fn validate_async(mut self) -> Result<Self, Self::Error> {
verify_mention(&mut self).await?;
Ok(self)
}
}