As Shepanster said, this is a very general question; therefore there are many ways to do what you want. And, as already mentioned, irona great example of a modular structure.
However, I will try to give a useful example of how to implement such a plug-in system. As an example, I assume that there is some kind of main box that can load plugins and "configure" CMS. This means that plugins do not load dynamically!
Structure
First, let's say we have four drawers:
rustpress: , WordPress.rustpress-plugin: ( , , rustpress )rustpress-signature: ,my-blog: , -
1. /
Rust - trait s. . trait , rustpress-plugin:
pub trait Plugin {
fn name(&self) -> &str;
fn filter_title(&self, title: &mut String) {}
fn filter_body(&self, body: &mut String) {}
}
, filter_* , ({}). , , .
2.
, , . impl ( rustpress-signature):
extern crate rustpress_plugin;
use rustpress_plugin::Plugin;
pub struct Signature {
pub text: String,
}
impl Plugin for Signature {
fn name(&self) -> &str {
"Signature Plugin v0.1 by ferris"
}
fn filter_body(&self, body: &mut String) {
body.push_str("\n-------\n");
body.push_str(&self.text);
}
}
Signature, Plugin. name(), filter_body(). . filter_title(), .
3.
CMS . , CMS rustpress, . ( rustpress):
extern crate rustpress_plugin;
use rustpress_plugin::Plugin;
pub struct RustPress {
// ...
plugins: Vec<Box<Plugin>>,
}
impl RustPress {
pub fn new() -> RustPress {
RustPress {
// ...
plugins: Vec::new(),
}
}
/// Adds a plugin to the stack
pub fn add_plugin<P: Plugin + 'static>(&mut self, plugin: P) {
self.plugins.push(Box::new(plugin));
}
/// Internal function that prepares a post
fn serve_post(&self) {
let mut title = "dummy".to_string();
let mut body = "dummy body".to_string();
for p in &self.plugins {
p.filter_title(&mut title);
p.filter_body(&mut body);
}
// use the finalized title and body now ...
}
/// Starts the CMS ...
pub fn start(&self) {}
}
, Vec ( , , ). CMS -, .
4. CMS
- CMS ( ). my-blog:
extern crate rustpress;
extern crate rustpress_plugin;
extern crate rustpress_signature;
use rustpress::RustPress;
use rustpress_plugin::Plugin;
use rustpress_signature::Signature;
fn main() {
let mut rustpress = RustPress::new();
let sig = Signature { text: "Ferris loves you <3".into() };
rustpress.add_plugin(sig);
rustpress.start();
}
Cargo.toml. , .
, . , . .