remove anyhow & bump version

This commit is contained in:
LordMZTE 2021-12-25 21:11:19 +01:00
parent fb10405a3b
commit d97c3dfb08
4 changed files with 58 additions and 18 deletions

View file

@ -1,13 +1,12 @@
[package]
name = "libjens"
version = "0.1.6"
version = "1.0.0"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.34"
env_logger = "0.8.2"
lazy_static = "1.4.0"
log = "0.4.11"
@ -15,6 +14,6 @@ once_cell = "1.5.2"
reqwest = "0.11.3"
serde = { version = "1.0.117", features = ["derive"] }
serde_json = "1.0.60"
thiserror = "1.0.23"
thiserror = "1.0.30"
tokio = { version = "1.7.0", features = ["macros", "fs", "rt-multi-thread"] }
url = "2.2.0"

View file

@ -1,9 +1,8 @@
use anyhow::{anyhow, Result};
use serde::{
de::{self, Visitor},
Deserialize,
Deserializer,
Deserialize, Deserializer,
};
use thiserror::Error;
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct UpResp {
@ -43,14 +42,22 @@ pub struct Meme {
}
impl Meme {
pub fn file_name(&self) -> Result<&str> {
self.link
.split('/')
.last()
.ok_or_else(|| anyhow!("failed to get file name. server response invalid"))
pub fn file_name(&self) -> Result<&str, MemeFileNameError> {
let fname = self.link.split('/').last().ok_or(MemeFileNameError)?;
// in a valid URL with a filename, there'll always be at least 4 slashes first.
if fname.is_empty() || self.link.split('/').count() < 4 {
return Err(MemeFileNameError);
}
Ok(fname)
}
}
#[derive(Debug, Error, PartialEq, Eq)]
#[error("Failed to get file name. Server response invalid!")]
pub struct MemeFileNameError;
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct User {
pub name: String,
@ -64,8 +71,8 @@ impl User {
pub fn get_id(&self) -> Option<&String> {
self.id
.as_ref()
.or_else(|| self.tokenhash.as_ref())
.or_else(|| self.userdir.as_ref())
.or(self.tokenhash.as_ref())
.or(self.userdir.as_ref())
}
}
@ -103,3 +110,29 @@ where
deserializer.deserialize_any(Vis)
}
#[cfg(test)]
mod tests {
use super::*;
fn meme_with_link(link: &str) -> Meme {
Meme {
id: "0".to_string(),
link: link.to_string(),
category: "test".to_string(),
user: "test".to_string(),
timestamp: 123,
ipfs: None,
}
}
#[test]
fn test_meme_file_name() {
assert!(meme_with_link("https://").file_name().is_err());
assert!(meme_with_link("https://test").file_name().is_err());
assert_eq!(
meme_with_link("https://test/some_meme").file_name(),
Ok("some_meme"),
);
}
}

View file

@ -22,7 +22,7 @@ macro_rules! init_cache {
None => {
let x = Arc::new($init_fn);
cell.get_or_init(|| x)
},
}
})
}};
}
@ -124,6 +124,7 @@ pub struct JMClientBuilder {
}
impl JMClientBuilder {
#[must_use]
pub fn build(self) -> JMClient {
JMClient {
http: self.client.unwrap_or_else(reqwest::Client::new),
@ -135,11 +136,13 @@ impl JMClientBuilder {
}
}
#[must_use]
pub fn client(mut self, client: reqwest::Client) -> Self {
self.client = Some(client);
self
}
#[must_use]
pub fn endpoint(mut self, endpoint: impl Into<Url>) -> Self {
self.endpoint = Some(endpoint.into());
self

View file

@ -1,5 +1,6 @@
use thiserror::Error;
use crate::api::Meme;
use anyhow::bail;
use std::str::FromStr;
pub mod consts {
@ -22,7 +23,7 @@ impl MemeSorting {
a.$field
.to_ascii_lowercase()
.cmp(&b.$field.to_ascii_lowercase())
});
})
};
}
@ -36,8 +37,12 @@ impl MemeSorting {
}
}
#[derive(Debug, Error, PartialEq, Eq)]
#[error("Invalid Meme sorting! Options are id, link, category and user!")]
pub struct InvalidMemeSortingError;
impl FromStr for MemeSorting {
type Err = anyhow::Error;
type Err = InvalidMemeSortingError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
@ -46,7 +51,7 @@ impl FromStr for MemeSorting {
"category" => Ok(Self::Category),
"user" => Ok(Self::User),
"timestamp" => Ok(Self::Timestamp),
_ => bail!("Invalid Meme sorting! options are id, link, category and user!"),
_ => Err(InvalidMemeSortingError),
}
}
}