Fmt + Clippy

main
Joey Hines 2021-10-23 13:13:28 -06:00
parent d8de648395
commit b3c0e2dcb0
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
12 changed files with 90 additions and 59 deletions

View File

@ -2,13 +2,13 @@ use crate::commands::{Command, RequestType};
use crate::context::Context;
use crate::helper::get_player_from_req;
use crate::Result;
use geoffrey_models::models::locations::{LocationDb, Location};
use geoffrey_db::helper::load_location;
use geoffrey_models::models::locations::{Location, LocationDb};
use geoffrey_models::models::parameters::add_location_params::AddLocationParams;
use geoffrey_models::models::parameters::CommandRequest;
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use geoffrey_models::models::CommandLevel;
use std::sync::Arc;
use geoffrey_db::helper::load_location;
pub struct AddLocation {}
@ -39,11 +39,9 @@ impl Command for AddLocation {
args.loc_type.into(),
);
let location = ctx.db
.insert(location)?;
let location = ctx.db.insert(location)?;
load_location(&ctx.db, &location).map_err(|err| err.into())
} else {
Err(GeoffreyAPIError::PlayerNotRegistered)
}

View File

@ -1,14 +1,14 @@
use crate::commands::{Command, RequestType};
use crate::context::Context;
use crate::Result;
use geoffrey_models::models::locations::{LocationDb, Location};
use geoffrey_models::models::player::Player;
use geoffrey_db::helper::load_location;
use geoffrey_models::models::locations::{Location, LocationDb};
use geoffrey_models::models::parameters::find_params::FindParams;
use geoffrey_models::models::parameters::CommandRequest;
use geoffrey_models::models::player::Player;
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use geoffrey_models::models::CommandLevel;
use std::sync::Arc;
use geoffrey_db::helper::load_location;
pub struct FindCommand {}
@ -36,22 +36,29 @@ impl Command for FindCommand {
let player_name = player.name.to_lowercase();
player_name.contains(&query)
})?
})?
.map(|player| player.id.unwrap())
.collect();
let locations: Vec<LocationDb> = ctx
let locations: Vec<LocationDb> = ctx
.db
.filter(|_, loc: &LocationDb| {
let name = loc.name.to_lowercase();
name.contains(&query) || loc.owners().iter().any(|owner_id| players.contains(owner_id))
}).map_err(|err| GeoffreyAPIError::from(err))?.collect();
name.contains(&query)
|| loc
.owners()
.iter()
.any(|owner_id| players.contains(owner_id))
})
.map_err(GeoffreyAPIError::from)?
.collect();
let locations: Result<Vec<Location>> = locations.iter().map(|loc| load_location(&ctx.db, loc).map_err(|err| GeoffreyAPIError::from(err))).collect();
let locations: Result<Vec<Location>> = locations
.iter()
.map(|loc| load_location(&ctx.db, loc).map_err(GeoffreyAPIError::from))
.collect();
locations
}
}

View File

@ -7,10 +7,10 @@ use geoffrey_models::models::response::APIResponse;
use geoffrey_models::models::CommandLevel;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fmt::Debug;
use std::sync::Arc;
use warp::filters::BoxedFilter;
use warp::Filter;
use std::fmt::Debug;
pub mod add_location;
pub mod find;
@ -39,7 +39,6 @@ pub fn create_command_filter<T: Command>(ctx: Arc<Context>) -> BoxedFilter<(impl
.and(warp::any().map(move || ctx.clone()))
.and(warp::body::json())
.map(|ctx: Arc<Context>, req: T::Req| {
log::info!("Running command {}", T::command_name());
log::debug!("Request: {:?}", req);

View File

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use log::{LevelFilter, SetLoggerError};
use serde::{Deserialize, Serialize};
use simple_logger::SimpleLogger;
#[derive(Serialize, Deserialize, Clone, Debug)]
@ -7,10 +7,11 @@ pub enum LogLevel {
None,
Warn,
Info,
Debug
Debug,
}
impl From<&str> for LogLevel {
#[allow(clippy::wildcard_in_or_patterns)]
fn from(s: &str) -> Self {
let s = s.to_lowercase();
match s.as_str() {
@ -28,7 +29,7 @@ impl From<LogLevel> for LevelFilter {
LogLevel::None => LevelFilter::Off,
LogLevel::Warn => LevelFilter::Warn,
LogLevel::Info => LevelFilter::Info,
LogLevel::Debug => LevelFilter::Debug
LogLevel::Debug => LevelFilter::Debug,
}
}
}

View File

@ -7,12 +7,12 @@ mod logging;
use crate::commands::command_filter;
use crate::config::GeoffreyAPIConfig;
use crate::context::Context;
use crate::logging::{init_logging, LogLevel};
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use structopt::StructOpt;
use crate::logging::{init_logging, LogLevel};
pub type Result<T> = std::result::Result<T, GeoffreyAPIError>;
@ -21,11 +21,16 @@ pub type Result<T> = std::result::Result<T, GeoffreyAPIError>;
struct Args {
#[structopt(env = "GEOFFREY_CONFIG", parse(from_os_str))]
config: PathBuf,
#[structopt(short, long, env = "GEOFFREY_LOG_LEVEL", parse(from_str), default_value="Info")]
#[structopt(
short,
long,
env = "GEOFFREY_LOG_LEVEL",
parse(from_str),
default_value = "Info"
)]
log_level: LogLevel,
}
#[tokio::main]
async fn main() {
let args: Args = Args::from_args();
@ -55,7 +60,5 @@ async fn main() {
let api = command_filter(ctx.clone());
warp::serve(api)
.run(socket_addr)
.await;
warp::serve(api).run(socket_addr).await;
}

View File

@ -107,14 +107,14 @@ impl Database {
#[cfg(test)]
mod tests {
use crate::database::Database;
use geoffrey_models::models::locations::{LocationDb, LocationDataDb};
use geoffrey_models::models::locations::shop::Shop;
use geoffrey_models::models::locations::{LocationDataDb, LocationDb};
use geoffrey_models::models::player::{Player, UserID};
use geoffrey_models::models::{Dimension, Position};
use geoffrey_models::GeoffreyDatabaseModel;
use lazy_static::lazy_static;
use std::path::Path;
use std::time::Instant;
use geoffrey_models::models::locations::shop::Shop;
lazy_static! {
static ref DB: Database = Database::new(Path::new("../test_database")).unwrap();
@ -176,7 +176,7 @@ mod tests {
player.id.unwrap(),
None,
LocationDataDb::Shop(Shop {
item_listings: Default::default()
item_listings: Default::default(),
}),
);
let loc = DB.insert::<LocationDb>(loc.clone()).unwrap();

View File

@ -1,9 +1,9 @@
use geoffrey_models::models::locations::{LocationDb, LocationDataDb, LocationData, Location};
use crate::database::Database;
use geoffrey_models::models::player::Player;
use crate::error::Result;
use geoffrey_models::models::locations::town::Town;
use geoffrey_models::models::locations::market::Market;
use geoffrey_models::models::locations::town::Town;
use geoffrey_models::models::locations::{Location, LocationData, LocationDataDb, LocationDb};
use geoffrey_models::models::player::Player;
pub fn load_location_data(db: &Database, data: &LocationDataDb) -> Result<LocationData> {
Ok(match data {
@ -12,18 +12,23 @@ pub fn load_location_data(db: &Database, data: &LocationDataDb) -> Result<Locati
LocationDataDb::Attraction => LocationData::Attraction,
LocationDataDb::Town(town_data) => {
let town_data = Town {
residents: db.filter(|id, _: &Player| town_data.residents.contains(&id))?.collect()
residents: db
.filter(|id, _: &Player| town_data.residents.contains(&id))?
.collect(),
};
LocationData::Town(town_data)
}
LocationDataDb::Farm(farm_data) => LocationData::Farm(farm_data.clone()),
LocationDataDb::Market(market_data) => {
let shops: Result<Vec<Location>> = db.filter(|id, _: &LocationDb| market_data.shops.contains(&id))?.map(|loc| load_location(db, &loc)).collect();
let shops: Result<Vec<Location>> = db
.filter(|id, _: &LocationDb| market_data.shops.contains(&id))?
.map(|loc| load_location(db, &loc))
.collect();
let market_data = Market {
is_public: market_data.is_public,
shops: shops?
shops: shops?,
};
LocationData::Market(market_data)
@ -31,9 +36,15 @@ pub fn load_location_data(db: &Database, data: &LocationDataDb) -> Result<Locati
})
}
pub fn load_location(db: &Database, location: &LocationDb) -> Result<Location> {
let owners: Vec<Player> = db.filter(|id, _: &Player| location.owners().contains(&id))?.collect();
pub fn load_location(db: &Database, location: &LocationDb) -> Result<Location> {
let owners: Vec<Player> = db
.filter(|id, _: &Player| location.owners().contains(&id))?
.collect();
let loc_data = load_location_data(db, &location.loc_data)?;
Ok(Location::from_db_location(location.clone(), owners, loc_data))
Ok(Location::from_db_location(
location.clone(),
owners,
loc_data,
))
}

View File

@ -1,6 +1,6 @@
use crate::models::locations::Location;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use crate::models::locations::Location;
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct MarketDb {

View File

@ -1,13 +1,13 @@
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use crate::models::locations::farm::FarmData;
use crate::models::locations::market::{Market, MarketDb};
use crate::models::locations::shop::Shop;
use crate::models::locations::town::{Town, TownDb};
use crate::models::player::Player;
use crate::models::{Position, Tunnel};
use crate::GeoffreyDatabaseModel;
use crate::models::locations::shop::Shop;
use crate::models::locations::town::{TownDb, Town};
use crate::models::locations::farm::FarmData;
use crate::models::locations::market::{MarketDb, Market};
use crate::models::player::Player;
pub mod farm;
pub mod market;
@ -55,14 +55,13 @@ impl From<LocationType> for LocationDataDb {
LocationType::Attraction => LocationDataDb::Attraction,
LocationType::Town => LocationDataDb::Town(Default::default()),
LocationType::Farm => LocationDataDb::Farm(Default::default()),
LocationType::Market => LocationDataDb::Market(Default::default())
LocationType::Market => LocationDataDb::Market(Default::default()),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum LocationData {
pub enum LocationData {
Base,
Shop(Shop),
Attraction,
@ -144,22 +143,25 @@ pub struct Location {
}
impl Location {
pub fn from_db_location(location: LocationDb, owners: Vec<Player>, loc_data: LocationData) -> Self {
pub fn from_db_location(
location: LocationDb,
owners: Vec<Player>,
loc_data: LocationData,
) -> Self {
Self {
id: location.id.unwrap(),
name: location.name,
position: location.position,
owners,
tunnel: location.tunnel,
loc_data
loc_data,
}
}
}
#[cfg(test)]
mod tests {
use crate::models::locations::{LocationDb, LocationDataDb};
use crate::models::locations::{LocationDataDb, LocationDb};
use crate::models::{Dimension, Position};
use crate::GeoffreyDatabaseModel;

View File

@ -1,6 +1,6 @@
use crate::models::player::Player;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use crate::models::player::Player;
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct TownDb {

View File

@ -1,4 +1,4 @@
use crate::models::locations::{LocationType};
use crate::models::locations::LocationType;
use crate::models::{Position, Tunnel};
use serde::{Deserialize, Serialize};

View File

@ -57,19 +57,29 @@ impl GeoffreyDatabaseModel for Player {
#[cfg(test)]
mod tests {
use crate::models::player::{Player, UserID};
use crate::models::player::UserID::{DiscordUUID, MinecraftUUID};
use crate::models::player::{Player, UserID};
use crate::GeoffreyDatabaseModel;
#[test]
fn test_player_check_unique() {
let p1 = Player::new("CoolTest123", UserID::DiscordUUID {discord_uuid: 0u64});
let p2 = Player::new("NotCoolTest123", UserID::DiscordUUID {discord_uuid: 1u64});
let p1 = Player::new("CoolTest123", UserID::DiscordUUID { discord_uuid: 0u64 });
let p2 = Player::new("NotCoolTest123", UserID::DiscordUUID { discord_uuid: 1u64 });
assert!(p1.check_unique(&p2));
let p1 = Player::new("CoolTest123", UserID::MinecraftUUID{mc_uuid: "0".to_string()});
let p2 = Player::new("NotCoolTest123", UserID::MinecraftUUID{ mc_uuid: "0".to_string()});
let p1 = Player::new(
"CoolTest123",
UserID::MinecraftUUID {
mc_uuid: "0".to_string(),
},
);
let p2 = Player::new(
"NotCoolTest123",
UserID::MinecraftUUID {
mc_uuid: "0".to_string(),
},
);
assert!(!p1.check_unique(&p2));
}