Compare commits
2 commits
d4a8b33dd0
...
d97c3dfb08
Author | SHA1 | Date | |
---|---|---|---|
LordMZTE | d97c3dfb08 | ||
LordMZTE | fb10405a3b |
|
@ -1,13 +1,12 @@
|
||||||
[package]
|
[package]
|
||||||
name = "libjens"
|
name = "libjens"
|
||||||
version = "0.1.6"
|
version = "1.0.0"
|
||||||
authors = ["LordMZTE <lord@mzte.de>"]
|
authors = ["LordMZTE <lord@mzte.de>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.34"
|
|
||||||
env_logger = "0.8.2"
|
env_logger = "0.8.2"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
log = "0.4.11"
|
log = "0.4.11"
|
||||||
|
@ -15,6 +14,6 @@ once_cell = "1.5.2"
|
||||||
reqwest = "0.11.3"
|
reqwest = "0.11.3"
|
||||||
serde = { version = "1.0.117", features = ["derive"] }
|
serde = { version = "1.0.117", features = ["derive"] }
|
||||||
serde_json = "1.0.60"
|
serde_json = "1.0.60"
|
||||||
thiserror = "1.0.23"
|
thiserror = "1.0.30"
|
||||||
tokio = { version = "1.7.0", features = ["macros", "fs", "rt-multi-thread"] }
|
tokio = { version = "1.7.0", features = ["macros", "fs", "rt-multi-thread"] }
|
||||||
url = "2.2.0"
|
url = "2.2.0"
|
||||||
|
|
55
src/api.rs
55
src/api.rs
|
@ -1,9 +1,8 @@
|
||||||
use anyhow::{anyhow, Result};
|
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{self, Visitor},
|
de::{self, Visitor},
|
||||||
Deserialize,
|
Deserialize, Deserializer,
|
||||||
Deserializer,
|
|
||||||
};
|
};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct UpResp {
|
pub struct UpResp {
|
||||||
|
@ -35,22 +34,30 @@ pub struct Category {
|
||||||
pub struct Meme {
|
pub struct Meme {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub link: String,
|
pub link: String,
|
||||||
pub path: String,
|
|
||||||
pub category: String,
|
pub category: String,
|
||||||
pub user: String,
|
pub user: String,
|
||||||
#[serde(deserialize_with = "deserialize_timestamp")]
|
#[serde(deserialize_with = "deserialize_timestamp")]
|
||||||
pub timestamp: i64,
|
pub timestamp: i64,
|
||||||
|
pub ipfs: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Meme {
|
impl Meme {
|
||||||
pub fn file_name(&self) -> Result<&str> {
|
pub fn file_name(&self) -> Result<&str, MemeFileNameError> {
|
||||||
self.path
|
let fname = self.link.split('/').last().ok_or(MemeFileNameError)?;
|
||||||
.split('/')
|
|
||||||
.last()
|
// in a valid URL with a filename, there'll always be at least 4 slashes first.
|
||||||
.ok_or_else(|| anyhow!("failed to get file name. server response invalid"))
|
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)]
|
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
@ -64,8 +71,8 @@ impl User {
|
||||||
pub fn get_id(&self) -> Option<&String> {
|
pub fn get_id(&self) -> Option<&String> {
|
||||||
self.id
|
self.id
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.or_else(|| self.tokenhash.as_ref())
|
.or(self.tokenhash.as_ref())
|
||||||
.or_else(|| self.userdir.as_ref())
|
.or(self.userdir.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,3 +110,29 @@ where
|
||||||
|
|
||||||
deserializer.deserialize_any(Vis)
|
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"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ macro_rules! init_cache {
|
||||||
None => {
|
None => {
|
||||||
let x = Arc::new($init_fn);
|
let x = Arc::new($init_fn);
|
||||||
cell.get_or_init(|| x)
|
cell.get_or_init(|| x)
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -124,6 +124,7 @@ pub struct JMClientBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JMClientBuilder {
|
impl JMClientBuilder {
|
||||||
|
#[must_use]
|
||||||
pub fn build(self) -> JMClient {
|
pub fn build(self) -> JMClient {
|
||||||
JMClient {
|
JMClient {
|
||||||
http: self.client.unwrap_or_else(reqwest::Client::new),
|
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 {
|
pub fn client(mut self, client: reqwest::Client) -> Self {
|
||||||
self.client = Some(client);
|
self.client = Some(client);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn endpoint(mut self, endpoint: impl Into<Url>) -> Self {
|
pub fn endpoint(mut self, endpoint: impl Into<Url>) -> Self {
|
||||||
self.endpoint = Some(endpoint.into());
|
self.endpoint = Some(endpoint.into());
|
||||||
self
|
self
|
||||||
|
|
13
src/util.rs
13
src/util.rs
|
@ -1,5 +1,6 @@
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::api::Meme;
|
use crate::api::Meme;
|
||||||
use anyhow::bail;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
pub mod consts {
|
pub mod consts {
|
||||||
|
@ -22,7 +23,7 @@ impl MemeSorting {
|
||||||
a.$field
|
a.$field
|
||||||
.to_ascii_lowercase()
|
.to_ascii_lowercase()
|
||||||
.cmp(&b.$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 {
|
impl FromStr for MemeSorting {
|
||||||
type Err = anyhow::Error;
|
type Err = InvalidMemeSortingError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
match s.to_lowercase().as_ref() {
|
match s.to_lowercase().as_ref() {
|
||||||
|
@ -46,7 +51,7 @@ impl FromStr for MemeSorting {
|
||||||
"category" => Ok(Self::Category),
|
"category" => Ok(Self::Category),
|
||||||
"user" => Ok(Self::User),
|
"user" => Ok(Self::User),
|
||||||
"timestamp" => Ok(Self::Timestamp),
|
"timestamp" => Ok(Self::Timestamp),
|
||||||
_ => bail!("Invalid Meme sorting! options are id, link, category and user!"),
|
_ => Err(InvalidMemeSortingError),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue