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

Add custom modules support #3

Open
Hugo4IT opened this issue Nov 28, 2021 · 0 comments
Open

Add custom modules support #3

Hugo4IT opened this issue Nov 28, 2021 · 0 comments
Assignees
Labels
M-0 | preon_module_xml This issue applies solely to the preon_module_xml crate U-0 | feature request Would be cool if you added this, now... quickly... no pressure ofc

Comments

@Hugo4IT
Copy link
Owner

Hugo4IT commented Nov 28, 2021

preon_module_xml currently only supports PreonEngine's default components/configurations, support for custom modules could AFAIK relatively easily be added, taking a form similar to this:

// === preon_module_xml ===

/// For implementing custom components
pub trait XMLComponentModule<T: PreonCustomComponentStack> {
  /// Returns the builder, and a bool stating if the component was found in this module.
  fn try_build(
    builder: PreonComponentBuilder<T>,
    component: &[u8],
    attributes: &quick_xml::Attributes
  ) -> (PreonComponentBuilder<T>, bool);
}

/// For implementing custom configuration keys (#1)
pub trait XMLConfigModule {
  /// Return value indicates if the specified configuration key was found.
  fn try_configure(key: String, value: String) -> bool;
}

// === user code ===

enum MyComponentStack {
  BlueButton(String),
}

// PreonComponentBuilder methods for the BlueButton component
trait AddBlueButton { /* ... */ }
impl<T: PreonCustomComponentStack> AddBlueButton for PreonComponentBuilder<T> { /* ... */ }

// Component functionality
impl PreonCustomComponentStack for MyComponentStack { /* ... */ }

// XMLModule
impl XMLComponentModule for MyComponentStack {
  fn try_build(
    builder: PreonComponentBuilder<Self>,
    component: &[u8],
    attributes: &quick_xml::Attributes
  ) -> (PreonComponentBuilder<Self>, bool) {
    match component {
      b"blue-button" => (builder.start_blue_button(), true)
      _ => (builder, false)
    }
  }
}

fn main() {
  // -- snip --
  PreonModuleXML::new()
    .with_module::<MyComponentStack>()
    .get_engine_from_xml(include_str!("res/app.xml"));
}
@Hugo4IT Hugo4IT added U-0 | feature request Would be cool if you added this, now... quickly... no pressure ofc M-0 | preon_module_xml This issue applies solely to the preon_module_xml crate labels Nov 28, 2021
@Hugo4IT Hugo4IT added this to the preon_module_xml milestone Nov 28, 2021
@Hugo4IT Hugo4IT self-assigned this Nov 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
M-0 | preon_module_xml This issue applies solely to the preon_module_xml crate U-0 | feature request Would be cool if you added this, now... quickly... no pressure ofc
Projects
None yet
Development

No branches or pull requests

1 participant