From 086246694033e230aaaae71d09f9234feba08227 Mon Sep 17 00:00:00 2001 From: Cno Date: Thu, 2 May 2024 00:27:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=A0=A1=E9=AA=8C=20?= =?UTF-8?q?mirrorhello?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entrances/mirror.rs | 10 ++++++++-- src/types/mirror.rs | 16 ++++++++++++++++ src/utils/mirror.rs | 6 +++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/entrances/mirror.rs b/src/entrances/mirror.rs index c48bd5a5..ecc8a705 100644 --- a/src/entrances/mirror.rs +++ b/src/entrances/mirror.rs @@ -5,7 +5,10 @@ use reqwest::blocking::get; use toml::{to_string_pretty, Value}; use crate::{ - types::mirror::{MirrorHello, ServiceKeys}, + types::{ + mirror::{MirrorHello, ServiceKeys}, + verifiable::Verifiable, + }, utils::{ fs::{ensure_dir_exist, try_recycle}, get_path_mirror, @@ -24,6 +27,9 @@ pub fn mirror_add(url: &String, should_match_name: Option) -> Result<()> } } + // 校验 + res.verify_self(&"".to_string())?; + // 写 mirror 目录 let p = get_path_mirror()?.join(&res.name); ensure_dir_exist(&p)?; @@ -44,7 +50,7 @@ pub fn mirror_update(name: &String) -> Result<()> { } pub fn mirror_remove(name: &String) -> Result<()> { - // 获取 meta.toml 路径 + // 获取目录路径 let (_, p) = read_local_mirror_meta(name)?; // 移除目录 try_recycle(p) diff --git a/src/types/mirror.rs b/src/types/mirror.rs index e32240f3..75c291e7 100644 --- a/src/types/mirror.rs +++ b/src/types/mirror.rs @@ -1,5 +1,9 @@ +use crate::utils::mirror::filter_service_from_meta; +use anyhow::Result; use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use super::verifiable::Verifiable; + #[derive(Debug, PartialEq, Clone)] pub enum Locale { ZhCn, @@ -96,3 +100,15 @@ impl<'de> Deserialize<'de> for ServiceKeys { } } } + +impl Verifiable for MirrorHello { + fn verify_self(&self, _located: &String) -> Result<()> { + // 必须有 hello 服务 + let hello_res = filter_service_from_meta(self.clone(), ServiceKeys::Hello); + if let Err(e) = hello_res { + return Err(e); + } + + Ok(()) + } +} diff --git a/src/utils/mirror.rs b/src/utils/mirror.rs index 22b16afd..37e2c9e4 100644 --- a/src/utils/mirror.rs +++ b/src/utils/mirror.rs @@ -6,7 +6,10 @@ use toml::from_str; use crate::{ p2s, - types::mirror::{MirrorHello, Service, ServiceKeys}, + types::{ + mirror::{MirrorHello, Service, ServiceKeys}, + verifiable::Verifiable, + }, utils::get_path_mirror, }; @@ -20,6 +23,7 @@ pub fn read_local_mirror_meta(name: &String) -> Result<(MirrorHello, PathBuf)> { let text = read_to_string(&p)?; let meta: MirrorHello = from_str(&text) .map_err(|e| anyhow!("Error:Invalid meta content at '{fp}' : {e}", fp = p2s!(p)))?; + meta.verify_self(&"".to_string())?; Ok((meta, dir_path)) }