Skip to content

Commit

Permalink
refactor(core): apply lint fix
Browse files Browse the repository at this point in the history
- add docs
- remove unused dependencies
- remove `unwrap` from test code
  • Loading branch information
SARDONYX-sard committed Jan 31, 2024
1 parent 86f61e1 commit ba98e00
Show file tree
Hide file tree
Showing 61 changed files with 1,012 additions and 416 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cspell.jsonc
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{
"words": [
"appender",
"bools",
"chrono",
"Couprie",
"ctypes",
"ebnf",
"expl",
"fullscreen",
"Geoffroy",
"Greatsword",
"icns",
"idents",
"jwalk",
"linkedlist",
"Mincho",
"mohidden",
"mpsc",
Expand All @@ -30,7 +35,9 @@
"thiserror",
"Unhide",
"unlisten",
"unnested",
"unoptimized",
"unseparated",
"Usize",
"walkdir",
"Warhammer",
Expand Down
1 change: 0 additions & 1 deletion dar2oar_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ criterion = { version = "0.5.1", features = [
"async_tokio",
"html_reports",
] }
once_cell = "1.18.0"
pretty_assertions = "1.4.0"
temp-dir = "0.1.11"
tracing-appender = "0.2"
Expand Down
4 changes: 4 additions & 0 deletions dar2oar_core/src/condition_parser/actor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Parses an actor-related condition based on the provided arguments and condition name.
use super::dar_interface::ParseError;
use super::macros::get_try_into;
use crate::{
Expand All @@ -6,6 +7,9 @@ use crate::{
values::{ActorValue, ActorValueType, Cmp, NumericValue},
};

/// Parses an actor-related condition based on the provided arguments and condition name.
/// # Errors
/// If parsing fails.
pub(super) fn parse_actor(
condition_name: &str,
args: Vec<FnArg<'_>>,
Expand Down
7 changes: 6 additions & 1 deletion dar2oar_core/src/condition_parser/compare.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Parses a comparison-based condition for plugin values.
use super::dar_interface::ParseError;
use super::macros::get_try_into;
use crate::{
Expand All @@ -6,7 +7,11 @@ use crate::{
values::{Cmp, NumericValue, PluginValue},
};

/// condition_name: "ValueEqualTo" | "ValueLessThan"
/// Parses a comparison-based condition for plugin values.
/// `ValueEqualTo` | `ValueLessThan`
///
/// # Errors
/// Parsing failed.
pub(super) fn parse_compare(
condition_name: &str,
args: Vec<FnArg<'_>>,
Expand Down
7 changes: 7 additions & 0 deletions dar2oar_core/src/condition_parser/conditions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Parses a high-level condition set based on the provided syntax.
use super::actor::parse_actor;
use super::compare::parse_compare;
use super::dar_interface::ParseError;
Expand All @@ -13,6 +14,9 @@ use crate::conditions::{
use crate::dar_syntax::syntax::{self, Expression};
use crate::values::{Cmp, DirectionValue};

/// Parses a high-level condition set based on the provided syntax.
/// # Errors
/// Parsing failed.
pub fn parse_conditions(input: syntax::Condition) -> Result<ConditionSet, ParseError> {
Ok(match input {
syntax::Condition::And(conditions) => {
Expand All @@ -39,6 +43,9 @@ pub fn parse_conditions(input: syntax::Condition) -> Result<ConditionSet, ParseE
})
}

/// Parses a conditional expression and translates it into a corresponding [`ConditionSet`].
/// # Errors
/// Parsing failed.
fn parse_condition(condition: Expression<'_>) -> Result<ConditionSet, ParseError> {
let Expression {
negated,
Expand Down
7 changes: 4 additions & 3 deletions dar2oar_core/src/condition_parser/dar_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
},
};

/// Couldn't parse in DAR to OAR processing Errors
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum ParseError {
/// - 1st arg: Expected value
Expand Down Expand Up @@ -51,7 +52,7 @@ impl TryFrom<&FnArg<'_>> for NumericLiteral {
fn try_from(value: &FnArg) -> Result<Self, Self::Error> {
match value {
FnArg::Number(num) => Ok(num.into()),
other => Err(ParseError::UnexpectedValue(
other @ FnArg::PluginValue { .. } => Err(ParseError::UnexpectedValue(
"Number(e.g. 3.0)".into(),
format!("{other:?}",),
)),
Expand Down Expand Up @@ -109,7 +110,7 @@ impl TryFrom<&FnArg<'_>> for StaticValue {
fn try_from(value: &FnArg) -> Result<Self, Self::Error> {
match value {
FnArg::Number(num) => Ok(num.into()),
other => Err(ParseError::UnexpectedValue(
other @ FnArg::PluginValue { .. } => Err(ParseError::UnexpectedValue(
"StaticValue(e.g. 3.0)".to_string(),
format!("{other:?}",),
)),
Expand Down Expand Up @@ -192,7 +193,7 @@ impl TryFrom<&FnArg<'_>> for Direction {
.try_into()
.map_err(|e: &str| ParseError::UnexpectedValue(e.into(), "0..=4".into()))?,
}),
other => Err(ParseError::UnexpectedValue(
other @ FnArg::PluginValue { .. } => Err(ParseError::UnexpectedValue(
"1..=4(in Cast &FnArg to Direction)".into(),
format!("{other:?}"),
)),
Expand Down
7 changes: 6 additions & 1 deletion dar2oar_core/src/condition_parser/equip.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Parses equipment-related conditions based on the provided arguments and condition name.
use super::macros::{gen_cond, get_try_into};
use super::{dar_interface::ParseError, macros::GetArg as _};
use crate::{
Expand All @@ -6,6 +7,10 @@ use crate::{
values::{NumericLiteral, TypeValue},
};

/// Parses equipment-related conditions based on the provided arguments and condition name.
///
/// # Errors
/// If parsing fails.
pub(super) fn parse_equip(
condition_name: &str,
args: Vec<FnArg<'_>>,
Expand All @@ -21,7 +26,7 @@ pub(super) fn parse_equip(
"IsEquippedRightType" | "IsEquippedLeftType" => {
let numeric_value: NumericLiteral = get_try_into!(args[0], "WeaponType -1..18")?;
let type_value = TypeValue {
value: numeric_value.try_into().map_err(|_: &str| {
value: numeric_value.try_into().map_err(|_err| {
ParseError::UnexpectedValue("-1..18".into(), "Unknown value".into())
})?,
};
Expand Down
5 changes: 5 additions & 0 deletions dar2oar_core/src/condition_parser/faction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Parses faction-related conditions based on the provided arguments and condition name.
use super::dar_interface::ParseError;
use super::macros::{get_try_into, GetArg as _};
use crate::{
Expand All @@ -6,6 +7,10 @@ use crate::{
values::Cmp,
};

/// Parses faction-related conditions based on the provided arguments and condition name.
///
/// # Errors
/// If parsing fails.
pub(super) fn parse_faction(
condition_name: &str,
args: Vec<FnArg<'_>>,
Expand Down
5 changes: 5 additions & 0 deletions dar2oar_core/src/condition_parser/has.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Parses has-prefix conditions based on the provided arguments and condition name.
use super::dar_interface::ParseError;
use super::macros::{gen_cond, get_try_into, GetArg as _};
use crate::conditions::{
Expand All @@ -6,6 +7,10 @@ use crate::conditions::{
};
use crate::dar_syntax::syntax::FnArg;

/// Parses has-prefix conditions based on the provided arguments and condition name.
///
/// # Errors
/// If parsing fails.
pub(super) fn parse_has(
condition_name: &str,
args: Vec<FnArg<'_>>,
Expand Down
7 changes: 5 additions & 2 deletions dar2oar_core/src/condition_parser/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Macros for type conversion of parsed DAR structures into easily serializable OAR structures
use super::ParseError;
use crate::dar_syntax::syntax::FnArg;

Expand Down Expand Up @@ -53,7 +54,7 @@ impl GetArg for Vec<FnArg<'_>> {
}
}

/// Access [Vec::get](https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html#method.get)(index) & try_into()
/// [`Vec::get(index)`](https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html#method.get) & [`TryInto`]
macro_rules! get_try_into {
($args:ident[$index:literal], $expected:literal) => {
<Vec<crate::dar_syntax::syntax::FnArg<'_>> as $crate::condition_parser::macros::GetArg>::try_get(
Expand All @@ -70,7 +71,9 @@ macro_rules! get_try_into {
}
pub(super) use get_try_into;

/// Generate `ConditionSet` & [Vec]::get(index) & try_into() (can use `into` if you need)
/// Generate `ConditionSet` &
/// [`Vec::get`](https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html#method.get)(index) &
/// [`TryInto`] (can use `into` if you need)
macro_rules! gen_cond {
($id:ident($field_name:ident, $negated:ident), $args:ident, $expected:literal) => {
ConditionSet::$id($id {
Expand Down
9 changes: 7 additions & 2 deletions dar2oar_core/src/condition_parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Module to convert a parsed DAR into a serializable OAR structure.
mod actor;
mod compare;
mod conditions;
Expand All @@ -13,6 +14,11 @@ use crate::conditions::ConditionSet;
use crate::dar_syntax::{convert_error, syntax::parse_condition};
use crate::error::{ConvertError, Result};

/// Parse a DAR string and convert it into a vector of [`ConditionSets`] representing an OAR structure.
///
/// This function takes a DAR string as input and parses it into a serializable OAR structure.
/// It returns a [`Result`] containing a vector of [`ConditionSet`] if successful,
/// or a [`ConvertError`] if any parsing or conversion error occurs.
pub fn parse_dar2oar(input: &str) -> Result<Vec<ConditionSet>> {
let (remain, dar_syn) = match parse_condition(input) {
Ok(syn) => {
Expand All @@ -22,8 +28,7 @@ pub fn parse_dar2oar(input: &str) -> Result<Vec<ConditionSet>> {
Err(err) => {
let err = match err {
nom::Err::Incomplete(_) => return Err(ConvertError::IncompleteConversion),
nom::Err::Error(err) => err,
nom::Err::Failure(err) => err,
nom::Err::Error(err) | nom::Err::Failure(err) => err,
};

tracing::trace!("Entered ConvertError::InvalidDarSyntax");
Expand Down
18 changes: 12 additions & 6 deletions dar2oar_core/src/conditions/and.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
//! Represents a logical AND condition set.
use super::{condition::default_required_version, is_false, ConditionSet};
use compact_str::CompactString;
use serde::{Deserialize, Serialize};

/// Represents a logical AND condition set.
///
/// - OAR: AND
/// - DAR: fn_name() AND
/// - DAR: `fn_name() AND`
///
/// - NOTE:
/// Fields other than Fields other than conditions are never used in DAR to OAR.
/// In DAR, AND is pushed up to the root conditions.
/// The non-conditions definitions exist in anticipation of future OAR parsing.
/// # NOTE
/// Fields other than conditions are never used in DAR to OAR.
/// In DAR, AND is pushed up to the root conditions.
/// The non-conditions definitions exist in anticipation of future OAR parsing.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct And {
/// Condition name "AND"
/// The name of the condition, which is "AND".
pub condition: CompactString,
/// The required version for this condition.
#[serde(default = "default_required_version")]
#[serde(rename = "requiredVersion")]
pub required_version: CompactString,
/// Indicates whether the condition is negated or not.
#[serde(default)]
#[serde(skip_serializing_if = "is_false")]
pub negated: bool,

/// The list of conditions forming the logical AND.
#[serde(rename = "Conditions")]
pub conditions: Vec<ConditionSet>,
}
Expand Down
Loading

0 comments on commit ba98e00

Please sign in to comment.