Skip to content

Commit

Permalink
refactor: add nursery rules
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa committed Dec 8, 2024
1 parent df3b091 commit 2057efd
Show file tree
Hide file tree
Showing 33 changed files with 298 additions and 328 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ module_name_repetitions = {level = "allow", priority = 2}
cast_possible_truncation = {level = "allow", priority = 2}
cast_precision_loss = {level = "allow", priority = 2}

future_not_send = {level = "allow", priority = 2}

pedantic = {level = "warn", priority = 1}
#restriction = "warn"
# nursery = "warn"
nursery = {level= "warn", priority = 1}


[dependencies]
Expand Down
3 changes: 1 addition & 2 deletions src/db/bestiary_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ pub async fn get_creatures_passing_all_filters(
let mut creature_vec = Vec::new();
let level_vec = key_value_filters
.get(&CreatureFilter::Level)
.unwrap_or(&HashSet::new())
.clone();
.map_or_else(HashSet::new, std::clone::Clone::clone);
let modified_filters =
prepare_filters_for_db_communication(key_value_filters, fetch_weak, fetch_elite);
for core in
Expand Down
2 changes: 1 addition & 1 deletion src/db/cr_core_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn update_creature_core_table(conn: &Pool<Sqlite>) -> Result<()> {
hp: cr.hp,
base_level: cr.level,
size: cr.size,
family: cr.family.unwrap_or(String::from("-")),
family: cr.family.unwrap_or_else(|| String::from("-")),
rarity: cr.rarity,
license: cr.license,
remaster: cr.remaster,
Expand Down
28 changes: 8 additions & 20 deletions src/db/data_providers/creature_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,72 +334,60 @@ async fn fetch_creature_items(conn: &Pool<Sqlite>, creature_id: i64) -> Result<V
/// It needs to be fetched from the association table.
/// It defaults to 1 if error are found
async fn fetch_item_quantity(conn: &Pool<Sqlite>, creature_id: i64, item_id: i64) -> i64 {
match sqlx::query!(
sqlx::query!(
"SELECT quantity FROM ITEM_CREATURE_ASSOCIATION_TABLE WHERE
creature_id == ($1) AND item_id == ($2)",
creature_id,
item_id
)
.fetch_one(conn)
.await
{
Ok(r) => r.quantity,
Err(_) => 1,
}
.map_or(1, |q| q.quantity)
}

/// Quantities are present ONLY for creature's weapons.
/// It needs to be fetched from the association table.
/// It defaults to 1 if error are found
async fn fetch_weapon_quantity(conn: &Pool<Sqlite>, creature_id: i64, weapon_id: i64) -> i64 {
match sqlx::query!(
sqlx::query!(
"SELECT quantity FROM WEAPON_CREATURE_ASSOCIATION_TABLE WHERE
creature_id == ($1) AND weapon_id == ($2)",
creature_id,
weapon_id
)
.fetch_one(conn)
.await
{
Ok(r) => r.quantity,
Err(_) => 1,
}
.map_or(1, |r| r.quantity)
}

/// Quantities are present ONLY for creature's shields.
/// It needs to be fetched from the association table.
/// It defaults to 1 if error are found
async fn fetch_shield_quantity(conn: &Pool<Sqlite>, creature_id: i64, shield_id: i64) -> i64 {
match sqlx::query!(
sqlx::query!(
"SELECT quantity FROM SHIELD_CREATURE_ASSOCIATION_TABLE WHERE
creature_id == ($1) AND shield_id == ($2)",
creature_id,
shield_id
)
.fetch_one(conn)
.await
{
Ok(r) => r.quantity,
Err(_) => 1,
}
.map_or(1, |r| r.quantity)
}

/// Quantities are present ONLY for creature's armors.
/// It needs to be fetched from the association table.
/// It defaults to 1 if error are found
async fn fetch_armor_quantity(conn: &Pool<Sqlite>, creature_id: i64, armor_id: i64) -> i64 {
match sqlx::query!(
sqlx::query!(
"SELECT quantity FROM ARMOR_CREATURE_ASSOCIATION_TABLE WHERE
creature_id == ($1) AND armor_id == ($2)",
creature_id,
armor_id
)
.fetch_one(conn)
.await
{
Ok(r) => r.quantity,
Err(_) => 1,
}
.map_or(1, |r| r.quantity)
}

async fn fetch_creature_actions(conn: &Pool<Sqlite>, creature_id: i64) -> Result<Vec<Action>> {
Expand Down
15 changes: 3 additions & 12 deletions src/db/data_providers/shop_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ pub async fn fetch_weapons(
el.item_core.traits = fetch_item_traits(conn, el.item_core.id).await?;
el.weapon_data.property_runes = fetch_weapon_runes(conn, el.weapon_data.id).await?;
el.weapon_data.damage_data = fetch_weapon_damage_data(conn, el.weapon_data.id).await?;
result_vec.push(Weapon {
item_core: el.item_core,
weapon_data: el.weapon_data,
});
result_vec.push(el);
}
Ok(result_vec)
}
Expand All @@ -187,10 +184,7 @@ pub async fn fetch_armors(conn: &Pool<Sqlite>, cursor: u32, page_size: i16) -> R
for mut el in x {
el.item_core.traits = fetch_item_traits(conn, el.item_core.id).await?;
el.armor_data.property_runes = fetch_armor_runes(conn, el.armor_data.id).await?;
result_vec.push(Armor {
item_core: el.item_core,
armor_data: el.armor_data,
});
result_vec.push(el);
}
Ok(result_vec)
}
Expand Down Expand Up @@ -219,10 +213,7 @@ pub async fn fetch_shields(
let mut result_vec = Vec::new();
for mut el in x {
el.item_core.traits = fetch_item_traits(conn, el.item_core.id).await?;
result_vec.push(Shield {
item_core: el.item_core,
shield_data: el.shield_data,
});
result_vec.push(el);
}
Ok(result_vec)
}
Expand Down
23 changes: 11 additions & 12 deletions src/db/shop_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,17 @@ async fn get_list(app_state: &AppState) -> Vec<ResponseItem> {
/// Gets all the runtime sources. It will cache the result
#[once(sync_writes = true)]
pub async fn get_all_sources(app_state: &AppState) -> Vec<String> {
match get_all_items_from_db(app_state).await {
Ok(v) => v
.into_iter()
.map(|x| x.source)
.unique()
.filter(|x| !x.is_empty())
.sorted()
.collect(),
Err(_) => {
vec![]
}
}
get_all_items_from_db(app_state).await.map_or_else(
|_| vec![],
|v| {
v.into_iter()
.map(|x| x.source)
.unique()
.filter(|x| !x.is_empty())
.sorted()
.collect()
},
)
}

/// Gets all the runtime traits. It will cache the result
Expand Down
22 changes: 9 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ fn get_service_startup_state() -> StartupState {
}

fn get_service_port() -> u16 {
match env::var("SERVICE_PORT").ok() {
None => 25566,
Some(port) => port.parse::<u16>().unwrap_or(25566),
}
env::var("SERVICE_PORT")
.ok()
.map_or(25566, |port| port.parse().unwrap_or(25566))
}

fn get_service_workers() -> usize {
let available_cpus =
usize::from(std::thread::available_parallelism().unwrap_or(NonZero::new(1).unwrap()));
match env::var("N_OF_SERVICE_WORKERS").ok() {
None => available_cpus,
Some(n_of_workers) => n_of_workers.parse::<usize>().unwrap_or(available_cpus),
}
env::var("N_OF_SERVICE_WORKERS")
.ok()
.map_or(available_cpus, |n_of_workers| {
n_of_workers.parse().unwrap_or(available_cpus)
})
}

fn init_docs(openapi: &mut utoipa::openapi::OpenApi) {
Expand All @@ -108,11 +108,7 @@ pub async fn start(
}
InitializeLogResponsibility::Delegated => {} // do nothing, someone else has already initialized them
}
let db_url = if let Some(x) = db_location {
x
} else {
get_service_db_url()
};
let db_url = db_location.map_or_else(get_service_db_url, |x| x);
let service_ip = get_service_ip();
let service_port = get_service_port();
let startup_state: StartupState = get_service_startup_state();
Expand Down
8 changes: 4 additions & 4 deletions src/models/creature/creature_component/creature_combat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct CreatureCombatData {
}

impl CreatureCombatData {
fn add_mod_to_saving_throws_and_ac_and_wp_to_hit(self, modifier: i64) -> CreatureCombatData {
fn add_mod_to_saving_throws_and_ac_and_wp_to_hit(self, modifier: i64) -> Self {
let mut com_data = self;
let weapons: Vec<Weapon> = com_data
.weapons
Expand All @@ -57,7 +57,7 @@ impl CreatureCombatData {
com_data
}

fn add_mod_to_dmg(self, modifier: i64) -> CreatureCombatData {
fn add_mod_to_dmg(self, modifier: i64) -> Self {
let mut com_data = self;
let weapons: Vec<Weapon> = com_data
.weapons
Expand All @@ -83,7 +83,7 @@ impl CreatureCombatData {
}

/// Lowers saving throws, weapon to hit bonus, and ac by the given `pwl_mod`
pub fn convert_from_base_to_pwl(self, pwl_mod: u64) -> CreatureCombatData {
pub fn convert_from_base_to_pwl(self, pwl_mod: u64) -> Self {
self.add_mod_to_saving_throws_and_ac_and_wp_to_hit(
-i64::try_from(pwl_mod).unwrap_or(i64::MAX),
)
Expand All @@ -93,7 +93,7 @@ impl CreatureCombatData {
/// If the creature has limits on how many times or how often it can use an ability
/// (such as a spellcaster’s spells or a dragon’s breath), decrease the damage by 4 instead.
/// Increase/Decrease the creature’s AC, attack modifiers, DCs, saving throws by 2.
pub fn convert_from_base_to_variant(self, variant: CreatureVariant) -> CreatureCombatData {
pub fn convert_from_base_to_variant(self, variant: CreatureVariant) -> Self {
let modifier = variant.to_adjustment_modifier();
self.add_mod_to_saving_throws_and_ac_and_wp_to_hit(modifier)
.add_mod_to_dmg(modifier)
Expand Down
8 changes: 4 additions & 4 deletions src/models/creature/creature_component/creature_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ impl<'r> FromRow<'r, SqliteRow> for EssentialData {
let rarity: String = row.try_get("rarity")?;
let size: String = row.try_get("size")?;
let alignment: String = row.try_get("alignment")?;
Ok(EssentialData {
Ok(Self {
id: row.try_get("id")?,
aon_id: row.try_get("aon_id").ok(),
name: row.try_get("name")?,
hp: row.try_get("hp")?,
base_level: row.try_get("level")?,
size: SizeEnum::from(size),
family: row.try_get("family").unwrap_or(String::from("-")),
family: row.try_get("family").unwrap_or_else(|_| String::from("-")),
rarity: RarityEnum::from(rarity),
license: row.try_get("license")?,
remaster: row.try_get("remaster")?,
Expand All @@ -81,7 +81,7 @@ impl<'r> FromRow<'r, SqliteRow> for EssentialData {

impl<'r> FromRow<'r, SqliteRow> for DerivedData {
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
Ok(DerivedData {
Ok(Self {
archive_link: row.try_get("archive_link").ok(),
is_melee: row.try_get("is_melee")?,
is_ranged: row.try_get("is_ranged")?,
Expand All @@ -99,7 +99,7 @@ impl<'r> FromRow<'r, SqliteRow> for DerivedData {

impl<'r> FromRow<'r, SqliteRow> for CreatureCoreData {
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
Ok(CreatureCoreData {
Ok(Self {
essential: EssentialData::from_row(row)?,
derived: DerivedData::from_row(row)?,
traits: vec![],
Expand Down
6 changes: 3 additions & 3 deletions src/models/creature/creature_component/creature_extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct CreatureExtraData {
}

impl CreatureExtraData {
fn add_mod_to_perception_and_skill_mods(self, modifier: i64) -> CreatureExtraData {
fn add_mod_to_perception_and_skill_mods(self, modifier: i64) -> Self {
let mut ex_data = self;
// we should never have a pwl much greater than perception (pwl=lvl)
ex_data.perception = (i64::from(ex_data.perception) + modifier) as i8;
Expand All @@ -62,12 +62,12 @@ impl CreatureExtraData {
ex_data
}
/// Lowers skill and perception by the given `pwl_mod`
pub fn convert_from_base_to_pwl(self, pwl_mod: u64) -> CreatureExtraData {
pub fn convert_from_base_to_pwl(self, pwl_mod: u64) -> Self {
self.add_mod_to_perception_and_skill_mods(-i64::try_from(pwl_mod).unwrap_or(i64::MAX))
}

/// Increase/Decrease Perception, and skill modifiers by 2.
pub fn convert_from_base_to_variant(self, variant: CreatureVariant) -> CreatureExtraData {
pub fn convert_from_base_to_variant(self, variant: CreatureVariant) -> Self {
self.add_mod_to_perception_and_skill_mods(variant.to_adjustment_modifier())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct CreatureSpellCasterData {
}

impl CreatureSpellCasterData {
pub fn add_mod_to_spellcaster_atk_and_dc(self, modifier: i64) -> CreatureSpellCasterData {
pub fn add_mod_to_spellcaster_atk_and_dc(self, modifier: i64) -> Self {
let mut spell_data = self;

spell_data.spell_caster_entry.spell_casting_atk_mod = spell_data
Expand All @@ -28,14 +28,14 @@ impl CreatureSpellCasterData {
}

/// Lowers spell caster atk and dc
pub fn convert_from_base_to_pwl(self, pwl_mod: u64) -> CreatureSpellCasterData {
pub fn convert_from_base_to_pwl(self, pwl_mod: u64) -> Self {
self.add_mod_to_spellcaster_atk_and_dc(-i64::try_from(pwl_mod).unwrap_or(i64::MAX))
}

/// Increase/Decrease the damage of its Strikes and other offensive abilities by 2.
/// If the creature has limits on how many times or how often it can use an ability
/// (such as a spellcaster’s spells or a dragon’s breath), decrease the damage by 4 instead.
pub fn convert_from_base_to_variant(self, variant: CreatureVariant) -> CreatureSpellCasterData {
pub fn convert_from_base_to_variant(self, variant: CreatureVariant) -> Self {
self.add_mod_to_spellcaster_atk_and_dc(variant.to_adjustment_modifier())
}
}
26 changes: 13 additions & 13 deletions src/models/creature/creature_filter_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,43 @@ pub enum CreatureFilter {
impl fmt::Display for CreatureFilter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CreatureFilter::Level => {
Self::Level => {
write!(f, "level")
}
CreatureFilter::Family => {
Self::Family => {
write!(f, "family")
}
CreatureFilter::Size => {
Self::Size => {
write!(f, "size")
}
CreatureFilter::Rarity => {
Self::Rarity => {
write!(f, "rarity")
}
CreatureFilter::Melee => {
Self::Melee => {
write!(f, "is_melee")
}
CreatureFilter::Ranged => {
Self::Ranged => {
write!(f, "is_ranged")
}
CreatureFilter::SpellCaster => {
Self::SpellCaster => {
write!(f, "is_spell_caster")
}
CreatureFilter::Traits => {
Self::Traits => {
write!(f, "traits")
}
CreatureFilter::CreatureTypes => {
Self::CreatureTypes => {
write!(f, "cr_type")
}
CreatureFilter::CreatureRoles => {
Self::CreatureRoles => {
write!(f, "creature_roles")
}
CreatureFilter::Alignment => {
Self::Alignment => {
write!(f, "alignment")
}
CreatureFilter::PathfinderVersion => {
Self::PathfinderVersion => {
write!(f, "remaster")
}
CreatureFilter::Sources => {
Self::Sources => {
write!(f, "sources")
}
}
Expand Down
Loading

0 comments on commit 2057efd

Please sign in to comment.