Skip to content

Add custom modules support #3

@Hugo4IT

Description

@Hugo4IT

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"));
}

Metadata

Metadata

Assignees

Labels

M-0 | preon_module_xmlThis issue applies solely to the preon_module_xml crateU-0 | feature requestWould be cool if you added this, now... quickly... no pressure ofc

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions