Skip to content

Commit

Permalink
refactor(core): apply lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Feb 1, 2024
1 parent b70e373 commit 57d24d3
Show file tree
Hide file tree
Showing 31 changed files with 234 additions and 306 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace.package]
version = "0.5.0"
description = "DAR to OAR Converter"
categories = ["compilers", "games", "parsing"]
repository = "https://github.com/SARDONYX-sard/dar-to-oar"

[workspace]
Expand Down
2 changes: 2 additions & 0 deletions dar2oar_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "dar2oar_cli"
version.workspace = true
description = "DAR to OAR Converter CLI"
authors = ["SARDONYX-sard"]
categories.workspace = true
keywords = ["converter", "skyrim", "cli"]
license = "MIT OR Apache-2.0"
repository.workspace = true
edition = "2021"
Expand Down
2 changes: 2 additions & 0 deletions dar2oar_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "dar2oar_core"
version.workspace = true
description = "DAR to OAR Converter Core"
authors = ["SARDONYX-sard"]
categories.workspace = true
keywords = ["converter", "skyrim"]
license = "MIT OR Apache-2.0"
repository.workspace = true
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion dar2oar_core/src/condition_parser/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ mod tests {
let condition_name = "IsActorBase";
let args = vec![FnArg::PluginValue {
plugin_name: "Skyrim.esm",
form_id: NumberLiteral::Hex(0x00000007),
form_id: NumberLiteral::Hex(0x0000_0007),
}];
let is_negated = true;

Expand Down
4 changes: 2 additions & 2 deletions dar2oar_core/src/condition_parser/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub(super) fn parse_compare(
"ValueLessThan" => create_compare(Cmp::Lt),
_ => {
return Err(ParseError::UnexpectedValue(
"ValueEqualTo or ValueLessThan".to_string(),
condition_name.to_string(),
"ValueEqualTo or ValueLessThan".into(),
condition_name.into(),
))
}
})
Expand Down
2 changes: 1 addition & 1 deletion dar2oar_core/src/condition_parser/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mod tests {
fn_name: "IsActorBase",
args: vec![FnArg::PluginValue {
plugin_name: "Skyrim.esm",
form_id: NumberLiteral::Hex(0x00000007),
form_id: NumberLiteral::Hex(0x0000_0007),
}],
};
let player = Expression {
Expand Down
18 changes: 9 additions & 9 deletions dar2oar_core/src/condition_parser/dar_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub enum ParseError {
impl From<NumberLiteral> for NumericLiteral {
fn from(value: NumberLiteral) -> Self {
match value {
NumberLiteral::Decimal(num) => NumericLiteral::Decimal(num),
NumberLiteral::Float(num) => NumericLiteral::Float(num),
NumberLiteral::Hex(num) => NumericLiteral::Hex(num),
NumberLiteral::Decimal(num) => Self::Decimal(num),
NumberLiteral::Float(num) => Self::Float(num),
NumberLiteral::Hex(num) => Self::Hex(num),
}
}
}
Expand All @@ -31,9 +31,9 @@ impl From<&NumberLiteral> for NumericLiteral {
fn from(value: &NumberLiteral) -> Self {
//! Note: omitting the definition using owned into will result in a circular loop.
match *value {
NumberLiteral::Decimal(num) => NumericLiteral::Decimal(num),
NumberLiteral::Float(num) => NumericLiteral::Float(num),
NumberLiteral::Hex(num) => NumericLiteral::Hex(num),
NumberLiteral::Decimal(num) => Self::Decimal(num),
NumberLiteral::Float(num) => Self::Float(num),
NumberLiteral::Hex(num) => Self::Hex(num),
}
}
}
Expand All @@ -42,7 +42,7 @@ impl TryFrom<FnArg<'_>> for NumericLiteral {
type Error = ParseError;

fn try_from(value: FnArg) -> Result<Self, Self::Error> {
NumericLiteral::try_from(&value)
Self::try_from(&value)
}
}

Expand Down Expand Up @@ -100,7 +100,7 @@ impl TryFrom<FnArg<'_>> for StaticValue {
type Error = ParseError;

fn try_from(value: FnArg) -> Result<Self, Self::Error> {
StaticValue::try_from(&value)
Self::try_from(&value)
}
}

Expand All @@ -111,7 +111,7 @@ impl TryFrom<&FnArg<'_>> for StaticValue {
match value {
FnArg::Number(num) => Ok(num.into()),
other @ FnArg::PluginValue { .. } => Err(ParseError::UnexpectedValue(
"StaticValue(e.g. 3.0)".to_string(),
"StaticValue(e.g. 3.0)".to_owned(),
format!("{other:?}",),
)),
}
Expand Down
23 changes: 10 additions & 13 deletions dar2oar_core/src/condition_parser/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub(super) trait GetArg {
///
/// Use [Generic Associated Types(GATs)](https://blog.rust-lang.org/2022/10/28/gats-stabilization.html#what-are-gats)
/// for the `&T` in [`Vec<T>`] because it has the same lifeTime as [Vec].
type Output<'a>
type Output<'this>
where
Self: 'a;
Self: 'this;

/// Access the element at the specified index of the vector.
///
Expand All @@ -30,27 +30,24 @@ pub(super) trait GetArg {
/// A result containing a reference to the desired element or a `Error` with detailed information if the index is out of bounds.
fn try_get_real<T>(&self, index: usize, expected: T, actual: T) -> Self::Output<'_>
where
T: ToString;
T: Into<String>;
}

impl GetArg for Vec<FnArg<'_>> {
type Output<'a> = Result<&'a FnArg<'a>, ParseError> where Self: 'a;
type Output<'this> = Result<&'this FnArg<'this>, ParseError> where Self: 'this;

fn try_get(&self, index: usize, expected: impl ToString) -> Self::Output<'_> {
self.get(index).ok_or(ParseError::UnexpectedValue(
expected.to_string(),
format!("None in args[{}]", index),
))
self.get(index).ok_or_else(|| {
ParseError::UnexpectedValue(expected.to_string(), format!("None in args[{index}]"))
})
}

fn try_get_real<T>(&self, index: usize, expected: T, actual: T) -> Self::Output<'_>
where
T: ToString,
T: Into<String>,
{
self.get(index).ok_or(ParseError::UnexpectedValue(
expected.to_string(),
actual.to_string(),
))
self.get(index)
.ok_or_else(|| ParseError::UnexpectedValue(expected.into(), actual.into()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion dar2oar_core/src/conditions/faction_rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod tests {

let expected = FactionRank {
faction: PluginValue {
plugin_name: "".into(),
plugin_name: String::new(),
form_id: "".into(),
},
comparison: Cmp::Eq,
Expand Down
10 changes: 5 additions & 5 deletions dar2oar_core/src/conditions/namespace_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
use serde::{Deserialize, Serialize};

/// Represents the configuration structure for the 'config.json' namespace.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct MainConfig<'a> {
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MainConfig<'config> {
/// The name associated with the configuration.
#[serde(default)]
pub name: &'a str,
pub name: &'config str,

/// The description associated with the configuration.
#[serde(default)]
pub description: &'a str,
pub description: &'config str,

/// The author associated with the configuration.
#[serde(default)]
pub author: &'a str,
pub author: &'config str,
}
16 changes: 8 additions & 8 deletions dar2oar_core/src/dar_syntax/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ pub fn convert_error<I: core::ops::Deref<Target = str>>(input: I, e: VerboseErro
for (i, (substring, kind)) in e.errors.iter().enumerate() {
let offset = input.offset(substring);

if input.is_empty() {
// Safety:
// [`Result`] to this variable does not return `Err`,
// Because `write!` to a `String` is infallible, this `unwrap` is fine.
let _ = if input.is_empty() {
match kind {
VerboseErrorKind::Char(c) => {
write!(&mut result, "{}: expected '{}', got empty input\n\n", i, c)
write!(&mut result, "{i}: expected '{c}', got empty input\n\n")
}
VerboseErrorKind::Context(s) => {
write!(&mut result, "{}: in {}, got empty input\n\n", i, s)
write!(&mut result, "{i}: in {s}, got empty input\n\n")
}
VerboseErrorKind::Nom(e) => {
write!(&mut result, "{}: in {:?}, got empty input\n\n", i, e)
write!(&mut result, "{i}: in {e:?}, got empty input\n\n")
}
}
} else {
Expand Down Expand Up @@ -110,10 +113,7 @@ pub fn convert_error<I: core::ops::Deref<Target = str>>(input: I, e: VerboseErro
column = column_number,
),
}
}
.expect(
"Unreachable, Because `write!` to a `String` is infallible, this `unwrap` is fine.",
);
};
}

result
Expand Down
40 changes: 20 additions & 20 deletions dar2oar_core/src/dar_syntax/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ use std::fmt;
/// - Plugin e.g. Skyrim.esm | 0x007
/// - Literal e.g. 1.0
#[derive(Debug, Clone, PartialEq)]
pub enum FnArg<'a> {
pub enum FnArg<'input> {
/// e.g. "Skyrim.esm" | 0x007
PluginValue {
/// e.g. "Skyrim.esm"
plugin_name: &'a str,
plugin_name: &'input str,
/// e.g. 1
form_id: NumberLiteral,
},
Expand Down Expand Up @@ -91,26 +91,26 @@ pub enum NumberLiteral {
impl fmt::Display for NumberLiteral {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NumberLiteral::Hex(hex) => write!(f, "0x{hex:x}"),
NumberLiteral::Decimal(decimal) => write!(f, "{decimal}"),
NumberLiteral::Float(float) => write!(f, "{float}"),
Self::Hex(hex) => write!(f, "0x{hex:x}"),
Self::Decimal(decimal) => write!(f, "{decimal}"),
Self::Float(float) => write!(f, "{float}"),
}
}
}

/// DAR One line representation
#[derive(Debug, Clone, PartialEq)]
pub struct Expression<'a> {
pub struct Expression<'input> {
/// not condition
pub negated: bool,
/// function name == condition name
pub fn_name: &'a str,
pub fn_name: &'input str,
/// arguments
pub args: Vec<FnArg<'a>>,
pub args: Vec<FnArg<'input>>,
}

/// AND | OR
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Operator {
/// AND
And,
Expand All @@ -120,21 +120,21 @@ pub enum Operator {

/// Represents a high-level condition, which can be an AND combination, OR combination, or a leaf expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Condition<'a> {
pub enum Condition<'input> {
/// Represents an AND combination of multiple conditions.
And(Vec<Condition<'a>>),
And(Vec<Condition<'input>>),
/// Represents an OR combination of multiple conditions.
Or(Vec<Condition<'a>>),
Or(Vec<Condition<'input>>),
/// Represents a leaf expression within the condition hierarchy.
Exp(Expression<'a>),
Exp(Expression<'input>),
}

impl<'a> Condition<'a> {
impl<'input> Condition<'input> {
/// push to inner vec
///
/// # panics
/// If push to [`Self::Exp`]
fn push(&mut self, expression: Condition<'a>) {
fn push(&mut self, expression: Condition<'input>) {
match self {
Condition::And(inner) | Condition::Or(inner) => inner.push(expression),
Condition::Exp(_) => panic!("Expression cannot push"),
Expand All @@ -143,7 +143,7 @@ impl<'a> Condition<'a> {
}

/// Type alias for a result type using [`nom::IResult`], wrapping potential errors with [`nom::error::VerboseError`]
type IResult<'a, I, O, E = nom::error::VerboseError<&'a str>> = nom::IResult<I, O, E>;
type IResult<'input, I, O, E = nom::error::VerboseError<&'input str>> = nom::IResult<I, O, E>;

use nom::error::ParseError; // To use from_error_kind
/// A macro for returning an error with a specific error kind in the `nom::error::VerboseError` variant.
Expand Down Expand Up @@ -503,7 +503,7 @@ NOT IsActorValueLessThan(30, 60)
fn_name: "IsActorBase",
args: vec![FnArg::PluginValue {
plugin_name: "Skyrim.esm",
form_id: NumberLiteral::Hex(0x00BCDEF7),
form_id: NumberLiteral::Hex(0x00BC_DEF7),
}],
};
let player = Expression {
Expand Down Expand Up @@ -538,7 +538,7 @@ NOT IsActorValueLessThan(30, 60)
fn_name: "IsActorBase",
args: vec![FnArg::PluginValue {
plugin_name: "Skyrim.esm",
form_id: NumberLiteral::Hex(0x00000007),
form_id: NumberLiteral::Hex(0x0000_0007),
}],
})]);
assert_eq!(parse_condition(input), Ok(("", expected)));
Expand All @@ -552,7 +552,7 @@ NOT IsActorValueLessThan(30, 60)
fn_name: "IsActorBase",
args: vec![FnArg::PluginValue {
plugin_name: "Skyrim.esm",
form_id: NumberLiteral::Hex(0x00000007),
form_id: NumberLiteral::Hex(0x0000_0007),
}],
})])]);

Expand All @@ -567,7 +567,7 @@ NOT IsActorValueLessThan(30, 60)
fn_name: "IsActorBase",
args: vec![FnArg::PluginValue {
plugin_name: "Skyrim.esm",
form_id: NumberLiteral::Hex(0x00000007),
form_id: NumberLiteral::Hex(0x0000_0007),
}],
})]);

Expand Down
10 changes: 4 additions & 6 deletions dar2oar_core/src/fs/converter/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,40 @@ where
{
let path = path.as_ref();
let ConvertOptions {
dar_dir: _,
oar_dir,
mod_name,
author,
section_table,
section_1person_table,
run_parallel: _,
hide_dar,
..
} = options;

let ParsedPath {
dar_root: _,
oar_root,
is_1st_person,
mod_name: parsed_mod_name,
priority,
remain_dir,
..
} = parsed_path;

let mut parsed_mod_name = mod_name
.as_deref()
.unwrap_or(parsed_mod_name.as_deref().unwrap_or("Unknown"))
.unwrap_or_else(|| parsed_mod_name.as_deref().unwrap_or("Unknown"))
.to_owned();
if *is_1st_person {
parsed_mod_name.push_str("_1st_person");
};

let oar_name_space = oar_dir
.as_deref()
.map(|oar_mod_root| match is_1st_person {
.map_or(oar_root.clone(), |oar_mod_root| match is_1st_person {
true => Path::new(oar_mod_root)
.join("meshes/actors/character/_1stperson/animations/OpenAnimationReplacer"),
false => Path::new(oar_mod_root)
.join("meshes/actors/character/animations/OpenAnimationReplacer"),
})
.unwrap_or(oar_root.clone())
.join(&parsed_mod_name);

let file_name = path
Expand Down
Loading

0 comments on commit 57d24d3

Please sign in to comment.