|
1 | 1 | use crate::core::mc_server::base::McServer; |
| 2 | +use anyhow::{Result, anyhow}; |
| 3 | +use async_trait::async_trait; |
2 | 4 |
|
3 | | -// 计划的插件功能 |
4 | | -trait McServerPlugin: McServer { |
5 | | - // fn plugin()->(); |
| 5 | +/// 插件管理器 |
| 6 | +#[async_trait] |
| 7 | +pub trait McServerPlugin: McServer { |
| 8 | + fn get_repo(&self) -> &'static [&'static dyn ServerPluginRepo]; |
| 9 | +} |
| 10 | + |
| 11 | +pub struct ServerPlugin { |
| 12 | + id: usize, |
| 13 | + name: String, |
| 14 | + version: String, |
| 15 | + description: String, |
| 16 | +} |
| 17 | + |
| 18 | +/// 插件仓库 |
| 19 | +#[async_trait] |
| 20 | +pub trait ServerPluginRepo: Sync { |
| 21 | + /// 仓库名称,此字符串同时用于标识仓库 |
| 22 | + fn name(&self) -> &'static str; |
| 23 | + /// 列出本地插件 |
| 24 | + async fn list(&self) -> Vec<ServerPlugin>; |
| 25 | + /// 查询插件 |
| 26 | + async fn search(&self, keyword: &str) -> Vec<ServerPlugin>; |
| 27 | + /// 安装插件 |
| 28 | + async fn install(&self, plugin: ServerPlugin) -> Result<()>; |
| 29 | + /// 查询最新版本 |
| 30 | + async fn latest(&self, plugin: ServerPlugin) -> Result<ServerPlugin>; |
| 31 | +} |
| 32 | + |
| 33 | +#[async_trait] |
| 34 | +pub trait TryMcServerPlugin: McServer { |
| 35 | + fn impl_plugin(&self) -> bool; |
| 36 | + /// 列出本地插件 |
| 37 | + async fn list(&self) -> Result<Vec<(&'static str, ServerPlugin)>>; |
| 38 | + /// 查询插件 |
| 39 | + async fn search(&self, keyword: &str) -> Result<Vec<(&'static str, ServerPlugin)>>; |
| 40 | + /// 安装插件 |
| 41 | + async fn install(&self, repo: &str, plugin: ServerPlugin) -> Result<()>; |
| 42 | + /// 查询最新版本 |
| 43 | + async fn latest(&self, repo: &str, plugin: ServerPlugin) -> Result<ServerPlugin>; |
| 44 | +} |
| 45 | + |
| 46 | +#[async_trait] |
| 47 | +impl<T> TryMcServerPlugin for T |
| 48 | +where |
| 49 | + T: McServer + Sync, |
| 50 | +{ |
| 51 | + default fn impl_plugin(&self) -> bool { |
| 52 | + false |
| 53 | + } |
| 54 | + |
| 55 | + default async fn list(&self) -> Result<Vec<(&'static str, ServerPlugin)>> { |
| 56 | + Err(anyhow!( |
| 57 | + "The plugin manager has not been implemented for this server." |
| 58 | + )) |
| 59 | + } |
| 60 | + |
| 61 | + default async fn search(&self, _: &str) -> Result<Vec<(&'static str, ServerPlugin)>> { |
| 62 | + Err(anyhow!( |
| 63 | + "The plugin manager has not been implemented for this server." |
| 64 | + )) |
| 65 | + } |
| 66 | + |
| 67 | + default async fn install(&self, _: &str, _: ServerPlugin) -> Result<()> { |
| 68 | + Err(anyhow!( |
| 69 | + "The plugin manager has not been implemented for this server." |
| 70 | + )) |
| 71 | + } |
| 72 | + |
| 73 | + default async fn latest(&self, _: &str, _: ServerPlugin) -> Result<ServerPlugin> { |
| 74 | + Err(anyhow!( |
| 75 | + "The plugin manager has not been implemented for this server." |
| 76 | + )) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[async_trait] |
| 81 | +impl<T> TryMcServerPlugin for T |
| 82 | +where |
| 83 | + T: McServerPlugin + Sync, |
| 84 | +{ |
| 85 | + default fn impl_plugin(&self) -> bool { |
| 86 | + false |
| 87 | + } |
| 88 | + |
| 89 | + default async fn list(&self) -> Result<Vec<(&'static str, ServerPlugin)>> { |
| 90 | + let mut list = Vec::new(); |
| 91 | + for repo in self.get_repo() { |
| 92 | + list.extend( |
| 93 | + repo.list() |
| 94 | + .await |
| 95 | + .into_iter() |
| 96 | + .map(|plugin| (repo.name(), plugin)), |
| 97 | + ) |
| 98 | + } |
| 99 | + Ok(list) |
| 100 | + } |
| 101 | + |
| 102 | + default async fn search(&self, keyword: &str) -> Result<Vec<(&'static str, ServerPlugin)>> { |
| 103 | + let mut list = Vec::new(); |
| 104 | + for repo in self.get_repo() { |
| 105 | + list.extend( |
| 106 | + repo.search(keyword) |
| 107 | + .await |
| 108 | + .into_iter() |
| 109 | + .map(|plugin| (repo.name(), plugin)), |
| 110 | + ) |
| 111 | + } |
| 112 | + Ok(list) |
| 113 | + } |
| 114 | + |
| 115 | + default async fn install(&self, repo_name: &str, plugin: ServerPlugin) -> Result<()> { |
| 116 | + if let Some(repo) = self |
| 117 | + .get_repo() |
| 118 | + .iter() |
| 119 | + .find(|&&repo| repo.name() == repo_name) |
| 120 | + { |
| 121 | + repo.install(plugin).await |
| 122 | + } else { |
| 123 | + // 静态插件仓库不应该找不到 |
| 124 | + unreachable!("No such plugin repository: {}", repo_name) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + default async fn latest(&self, repo_name: &str, plugin: ServerPlugin) -> Result<ServerPlugin> { |
| 129 | + if let Some(repo) = self |
| 130 | + .get_repo() |
| 131 | + .iter() |
| 132 | + .find(|&&repo| repo.name() == repo_name) |
| 133 | + { |
| 134 | + repo.latest(plugin).await |
| 135 | + } else { |
| 136 | + // 静态插件仓库不应该找不到 |
| 137 | + unreachable!("No such plugin repository: {}", repo_name) |
| 138 | + } |
| 139 | + } |
6 | 140 | } |
0 commit comments