Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various cleanup things #13

Merged
merged 8 commits into from
Nov 2, 2023
Merged

various cleanup things #13

merged 8 commits into from
Nov 2, 2023

Conversation

calbaker
Copy link
Collaborator

@calbaker calbaker commented Nov 2, 2023

Changes

  • various cleanup of crate metadata
  • foundation for improved Python API experience

Python API foundation for cleaner API

These code blocks enable passing in enum variants (that have identically named structs) from Python. None of this code has been tested because there are no constructor methods for ConvnentionalLoco, HybridLoco, etc in Python -- I plan to fix this but not with any significant urgency. However, because this is Rust and compiles, then in probably works as expected.

  • This block is necessary because From<HybridLoco> for LocoType is not automatically generated by enum_dispatch because of the Box<HybridLoco>
    impl From<HybridLoco> for LocoType {
    fn from(value: HybridLoco) -> Self {
    Self::from(Box::new(value))
    }
    }
  • impl TryFrom<&PyAny> for LocoType {
    type Error = PyErr;
    /// This allows us to construct LocoType any struct that can be converted into LocoType
    fn try_from(value: &PyAny) -> std::result::Result<Self, Self::Error> {
    value
    .extract::<ConventionalLoco>()
    .map(LocoType::from)
    .or_else(|_| {
    value
    .extract::<HybridLoco>()
    .map(LocoType::from)
    .or_else(|_| {
    value
    .extract::<BatteryElectricLoco>()
    .map(LocoType::from)
    .or_else(|_| value.extract::<Dummy>().map(LocoType::from))
    })
    })
    .map_err(|_| {
    pyo3::PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!(
    "{}\nMust provide ConventionalLoco, HybridLoco, BatteryElectricLoco, or Dummy",
    format_dbg!()
    ))
    })
    }
    }
  • #[new]
    fn __new__(
    loco_type: &PyAny,
    loco_params: LocoParams,
    save_interval: Option<usize>,
    ) -> PyResult<Self> {
    Ok(Self {
    loco_type: LocoType::try_from(loco_type)?,
    state: Default::default(),
    save_interval,
    assert_limits: true,
    pwr_aux_offset: loco_params.pwr_aux_offset,
    pwr_aux_traction_coeff: loco_params.pwr_aux_traction_coeff,
    force_max: Some(loco_params.force_max),
    ..Default::default()
    })
    }

@calbaker calbaker marked this pull request as draft November 2, 2023 18:40
@calbaker calbaker changed the title Api cleanup various cleanup things Nov 2, 2023
@calbaker calbaker marked this pull request as ready for review November 2, 2023 20:59
@calbaker calbaker merged commit aceefad into main Nov 2, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant