ModuleSetup returns a concrete type via a generic type parameter

This commit is contained in:
selfhoster selfhoster 2023-04-20 12:44:48 +02:00
parent 863f48604c
commit 7019a0a071
4 changed files with 12 additions and 12 deletions

View File

@ -137,6 +137,6 @@ impl<Condition: Into<Option<SomeCondition>>> CommandBuilder<Cmd, Condition> {
}
pub fn run(self) -> Result<CommandStatus, CommandError> {
self.build().run().into()
CommandModule::run(self.build()).into()
}
}

View File

@ -30,9 +30,9 @@ impl CommandModule {
}
}
impl ModuleSetup<CommandArgs, CommandStatus, CommandError> for CommandModule {
fn with_facts(self, _facts: &Facts) -> Box<dyn Module<CommandArgs, CommandStatus, CommandError>> {
Box::new(self)
impl ModuleSetup<CommandModule, CommandArgs, CommandStatus, CommandError> for CommandModule {
fn with_facts(self, _facts: &Facts) -> CommandModule {
self
}
}

View File

@ -69,12 +69,12 @@ impl PlaybookRun {
println!("{}", serde_json::to_string_pretty(&self).unwrap());
}
pub fn run<MS: ModuleSetup<A, S, E>, A: Serialize, S: Serialize, E: Serialize>(&mut self, cmd: MS) -> Result<S, E> {
pub fn run<T: Module<A, S, E>, MS: ModuleSetup<T, A, S, E>, A: Serialize, S: Serialize, E: Serialize>(&mut self, cmd: MS) -> Result<S, E> {
let module = cmd.with_facts(&self.facts);
self.run_inner(module)
}
pub fn run_inner<A: Serialize, S: Serialize, E: Serialize>(&mut self, cmd: Box<dyn Module<A, S, E>>) -> Result<S, E> {
pub fn run_inner<T: Module<A, S, E>, A: Serialize, S: Serialize, E: Serialize>(&mut self, cmd: T) -> Result<S, E> {
// TODO: Check conditions for skip
let module_name = cmd.module_name().to_string();
@ -108,10 +108,10 @@ impl PlaybookRun {
// }
/// A trait for modules to be run in the context of a playbook, by gathering facts
pub trait ModuleSetup<A, S, E> {
pub trait ModuleSetup<T: Module<A, S, E>, A: Serialize, S: Serialize, E: Serialize> {
// type module: Module<A, S, E>;
fn with_facts(self, facts: &Facts) -> Box<dyn Module<A, S, E>>;
fn with_facts(self, facts: &Facts) -> T;
}
/// A Module takes some arguments which can be serialized back to the playbook run, and can be run to produce

View File

@ -53,8 +53,8 @@ impl Module<PackageArgs, (), PackageError> for PackageModule<Box<dyn SpecificPac
}
}
impl ModuleSetup<PackageArgs, (), PackageError> for PackageModule<NoManager> {
fn with_facts(self, facts: &Facts) -> Box<dyn Module<PackageArgs, (), PackageError>> {
impl ModuleSetup<PackageModule<Box<dyn SpecificPackageManager>>, PackageArgs, (), PackageError> for PackageModule<NoManager> {
fn with_facts(self, facts: &Facts) -> PackageModule<Box<dyn SpecificPackageManager>> {
let Self { args, .. } = self;
let manager: Box<dyn SpecificPackageManager> = match facts.os.family() {
@ -62,10 +62,10 @@ impl ModuleSetup<PackageArgs, (), PackageError> for PackageModule<NoManager> {
OsFamily::Archlinux => Box::new(ArchlinuxPackageManager),
};
Box::new(PackageModule {
PackageModule {
args,
manager,
})
}
}
}