Skip to content

Commit

Permalink
✨ Implement TryInto for PyCharacterInterface
Browse files Browse the repository at this point in the history
- `PyDict` -> `Mona::character::CharacterConfig` does not work
- Next attempt will be defining PyCharacterConfig and use proc macro
  • Loading branch information
Billyzou0741326 committed Oct 25, 2023
1 parent 2ce6868 commit d69d92d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pyo3::prelude::*;
use crate::applications::input::common::PyCharacterInterface;
use pyo3::prelude::*;

#[pyclass]
pub struct CalculatorConfig {
Expand All @@ -9,7 +9,7 @@ pub struct CalculatorConfig {
#[pymethods]
impl CalculatorConfig {
#[new]
pub fn py_new( character: PyCharacterInterface) -> PyResult<Self> {
Ok(Self {character})
pub fn py_new(character: PyCharacterInterface) -> PyResult<Self> {
Ok(Self { character })
}
}
27 changes: 17 additions & 10 deletions python_genshin_artifact_core/src/applications/input/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::str::FromStr;
use anyhow::Context;
use mona::character::{CharacterConfig, CharacterName};
use mona_wasm::applications::common::CharacterInterface as MonaCharacterInterface;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use std::str::FromStr;

#[pyclass(name = "CharacterInterface")]
#[derive(Clone)]
Expand All @@ -14,6 +15,7 @@ pub struct PyCharacterInterface {
pub skill1: usize,
pub skill2: usize,
pub skill3: usize,
// todo : 自定义 CharacterConfig 并用 proc macro 实现 Into<Mona::character::CharacterInfo>
pub params: Option<Py<PyDict>>,
}

Expand Down Expand Up @@ -43,22 +45,27 @@ impl PyCharacterInterface {
}
}

impl PyCharacterInterface {
impl TryInto<MonaCharacterInterface> for PyCharacterInterface {
type Error = anyhow::Error;

pub fn to_mona(&self) -> MonaCharacterInterface {
let name = CharacterName::from_str(&self.name).unwrap();
// todo : params 转换为 CharacterConfig::NoConfig
MonaCharacterInterface {
fn try_into(self) -> Result<MonaCharacterInterface, Self::Error> {
let name = CharacterName::from_str(&self.name).context("Failed to deserialize json")?;
if let Some(params) = self.params {
Python::with_gil(|py| {
// PyDict 无法转换为 CharacterConfig 类
// todo : 自定义 CharacterConfig 并用 proc macro 实现 Into<Mona::character::CharacterConfig>
let _dict: &PyDict = params.as_ref(py);
})
}
Ok(MonaCharacterInterface {
name,
level: self.level,
ascend: self.ascend,
constellation: self.constellation,
skill1: self.skill1,
skill2: self.skill2,
skill3: self.skill3,
params: CharacterConfig::NoConfig
}
params: CharacterConfig::NoConfig,
})
}
}


2 changes: 1 addition & 1 deletion python_genshin_artifact_core/src/applications/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod calculator;
pub mod common;
pub mod common;
2 changes: 1 addition & 1 deletion python_genshin_artifact_core/src/applications/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod generate;
pub mod input;
pub mod wasm;
pub mod input;
2 changes: 1 addition & 1 deletion python_genshin_artifact_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
extern crate core;
mod applications;

use crate::applications::input::calculator::CalculatorConfig;
use applications::generate::artifact::gen_artifact_meta_as_json;
use applications::generate::character::gen_character_meta_as_json;
use applications::generate::locale::gen_generate_locale_as_json;
use applications::generate::weapon::gen_weapon_meta_as_json;
use applications::wasm::{get_damage_analysis, get_transformative_damage};
use pyo3::import_exception;
use pyo3::prelude::*;
use crate::applications::input::calculator::CalculatorConfig;

import_exception!(json, JSONDecodeError);

Expand Down

0 comments on commit d69d92d

Please sign in to comment.