Skip to content

Commit

Permalink
Merge pull request #1 from WFCD/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mettwasser authored Jan 26, 2024
2 parents b050b48 + 39d2672 commit b205bdc
Show file tree
Hide file tree
Showing 20 changed files with 606 additions and 151 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ jobs:
- name: Rust Cache
uses: Swatinem/rust-cache@v2

- name: Test Worldstate
run: cargo test -F worldstate

- name: Test Worldstate with all features
run: cargo test -F worldstate_full

- name: Cargo build
run: cargo build --release

Expand Down
79 changes: 79 additions & 0 deletions .vscode/test.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
// Place your warframe workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
"Test a model (RTObj)": {
"scope": "rust",
"prefix": "model_test",
"body": [
"#[cfg(test)]"
"mod test {"
" use super::${1:model};"
" use crate::worldstate::{client::Client, error::ApiError};"
""
" #[cfg(not(feature = \"multilangual\"))]"
" #[tokio::test]"
" async fn test_${1/(.*)/${1:/downcase}/}() -> Result<(), ApiError> {"
" let client = Client::new();"
""
" match client.fetch::<${1:model}>().await {"
" Ok(_${1/(.*)/${1:/downcase}/}) => Ok(()),"
" Err(why) => Err(why),"
" }"
" }"
""
" #[cfg(feature = \"multilangual\")]"
" #[tokio::test]"
" async fn test_${1/(.*)/${1:/downcase}/}() -> Result<(), ApiError> {"
" use crate::worldstate::prelude::Language;"
""
" let client = Client::new();"
""
" match client.fetch::<${1:model}>(Language::ZH).await {"
" Ok(_${1/(.*)/${1:/downcase}/}) => Ok(()),"
" Err(why) => Err(why),"
" }"
" }"
"}"
]
},
"Test a model (RTArray)": {
"scope": "rust",
"prefix": "model_test_array",
"body": [
"#[cfg(test)]"
"mod test {"
" use super::${1:model};"
" use crate::worldstate::{client::Client, error::ApiError};"
""
" #[cfg(not(feature = \"multilangual\"))]"
" #[tokio::test]"
" async fn test_${1/(.*)/${1:/downcase}/}() -> Result<(), ApiError> {"
" let client = Client::new();"
""
" match client.fetch_arr::<${1:model}>().await {"
" Ok(_${1/(.*)/${1:/downcase}/}s) => Ok(()),"
" Err(why) => Err(why),"
" }"
" }"
""
" #[cfg(feature = \"multilangual\")]"
" #[tokio::test]"
" async fn test_${1/(.*)/${1:/downcase}/}() -> Result<(), ApiError> {"
" use crate::worldstate::prelude::Language;"
""
" let client = Client::new();"
""
" match client.fetch_arr::<${1:model}>(Language::ZH).await {"
" Ok(_${1/(.*)/${1:/downcase}/}s) => Ok(()),"
" Err(why) => Err(why),"
" }"
" }"
"}"
]
}
}
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ homepage = "https://docs.rs/warframe"
repository = "https://github.com/Mettwasser/warframe.rs"
license = "MIT"



[features]
default = ["worldstate"]
worldstate_full = ["worldstate", "multilangual"]
worldstate = []
multilangual = ["worldstate"]

[dev-dependencies]
clippy = "0.0.302"
Expand All @@ -23,3 +27,5 @@ reqwest = { version = "0.11.22", features = ["json"] }
chrono = { version = "0.4.31", features = ["serde"] }
serde = { version = "1.0.190", features = ["derive"] }
serde_json = { version = "1.0.108" }
serde_flat_path = "0.1.2"
serde_repr = "0.1.18"
19 changes: 0 additions & 19 deletions src/main.rs

This file was deleted.

27 changes: 16 additions & 11 deletions src/worldstate/client.rs → src/worldstate/client/basic.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
use super::error::ApiError;
use super::models::base::{Endpoint, Model, RTArray, RTObject};
use crate::worldstate::error::ApiError;

#[derive(Default)]
pub struct Client {
session: reqwest::Client,
}

impl Client {
pub fn new() -> Self {
Default::default()
}
use crate::worldstate::models::base::{Endpoint, Model, RTArray, RTObject};

impl super::Client {
pub async fn fetch<T>(&self) -> Result<T, ApiError>
where
T: Model + Endpoint + RTObject,
Expand All @@ -33,3 +25,16 @@ impl Client {
}
}
}

#[cfg(not(feature = "multilangual"))]
#[tokio::test]
async fn test_fissure() -> Result<(), ApiError> {
use crate::worldstate::prelude::*;

let client = Client::new();

match client.fetch_arr::<Fissure>().await {
Ok(_fissures) => Ok(()),
Err(why) => Err(why),
}
}
16 changes: 16 additions & 0 deletions src/worldstate/client/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[cfg(not(feature = "multilangual"))]
pub mod basic;

#[cfg(feature = "multilangual")]
pub mod multilangual;

#[derive(Default)]
pub struct Client {
session: reqwest::Client,
}

impl Client {
pub fn new() -> Self {
Default::default()
}
}
59 changes: 59 additions & 0 deletions src/worldstate/client/multilangual.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::worldstate::{error::ApiError, prelude::Language};

use super::super::models::base::{Endpoint, Model, RTArray, RTObject};

impl super::Client {
pub async fn fetch<T>(&self, language: Language) -> Result<T, ApiError>
where
T: Model + Endpoint + RTObject,
{
let response = self
.session
.get(format!(
"{}?language={}",
T::endpoint(),
String::from(language)
))
.send()
.await
.unwrap();
match response.status().as_u16() {
200 => Ok(response.json::<T>().await.unwrap()), // unwrap should be safe - the API only responds with a JSON
_code => Err(ApiError::from(response).await),
}
}

pub async fn fetch_arr<T>(&self, language: Language) -> Result<Vec<T>, ApiError>
where
T: Model + Endpoint + RTArray,
{
let response = self
.session
.get(format!(
"{}?language={}",
T::endpoint(),
String::from(language)
))
.send()
.await
.unwrap();

match response.status().as_u16() {
200 => Ok(response.json::<Vec<T>>().await.unwrap()), // unwrap should be safe - the API only responds with a JSON
_code => Err(ApiError::from(response).await),
}
}
}

#[cfg(feature = "multilangual")]
#[tokio::test]
async fn test_fissure() -> Result<(), ApiError> {
use crate::worldstate::prelude::*;

let client = Client::new();

match client.fetch_arr::<Fissure>(Language::EN).await {
Ok(_fissures) => Ok(()),
Err(why) => Err(why),
}
}
52 changes: 52 additions & 0 deletions src/worldstate/language.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pub enum Language {
DE,
ES,
FR,
IT,
KO,
PL,
PT,
RU,
ZH,
EN,
UK,
}

impl From<Language> for String {
fn from(value: Language) -> Self {
use Language::*;
match value {
DE => "de",
ES => "es",
FR => "fr",
IT => "it",
KO => "ko",
PL => "pl",
PT => "pt",
RU => "ru",
ZH => "zh",
EN => "en",
UK => "uk",
}
.into()
}
}

impl From<Language> for &'static str {
fn from(value: Language) -> Self {
use Language::*;
match value {
DE => "de",
ES => "es",
FR => "fr",
IT => "it",
KO => "ko",
PL => "pl",
PT => "pt",
RU => "ru",
ZH => "zh",
EN => "en",
UK => "uk",
}
}
}
5 changes: 5 additions & 0 deletions src/worldstate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ pub mod client;
pub mod error;
pub mod models;

#[cfg(feature = "multilangual")]
pub mod language;

pub mod prelude {
pub use crate::worldstate::client::Client;
pub use crate::worldstate::error::ApiError;
#[cfg(feature = "multilangual")]
pub use crate::worldstate::language::Language;
pub use crate::worldstate::models::*;
}
30 changes: 30 additions & 0 deletions src/worldstate/models/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,33 @@ model_builder! {
:"The reward type of the alert"
pub reward_type: Option<RewardType>
}

#[cfg(test)]
mod test {
use super::Alert;
use crate::worldstate::{client::Client, error::ApiError};

#[cfg(not(feature = "multilangual"))]
#[tokio::test]
async fn test_alert() -> Result<(), ApiError> {
let client = Client::new();

match client.fetch_arr::<Alert>().await {
Ok(_alerts) => Ok(()),
Err(why) => Err(why),
}
}

#[cfg(feature = "multilangual")]
#[tokio::test]
async fn test_alert() -> Result<(), ApiError> {
use crate::worldstate::prelude::Language;

let client = Client::new();

match client.fetch_arr::<Alert>(Language::ZH).await {
Ok(_alerts) => Ok(()),
Err(why) => Err(why),
}
}
}
Loading

0 comments on commit b205bdc

Please sign in to comment.