From ed924a5a517eefd9c7c0f9557b31288e0b03e552 Mon Sep 17 00:00:00 2001 From: George Miao Date: Sat, 5 Nov 2022 14:56:42 -0400 Subject: [PATCH] feat: helper --- src/types/response.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/types/response.rs b/src/types/response.rs index de16d30..d5553ba 100644 --- a/src/types/response.rs +++ b/src/types/response.rs @@ -1,6 +1,8 @@ use serde::Deserialize; use serde_repr::*; +use crate::types::Id; + #[derive(Deserialize, Debug)] pub struct RpcResponse { pub arguments: T, @@ -123,6 +125,16 @@ pub struct Torrent { pub file_stats: Option>, } +impl Torrent { + /// Get either the ID or the hash string if exist, which are both unique and + /// can be pass to the API. + pub fn id(&self) -> Option { + self.id + .map(Id::Id) + .or_else(|| self.hash_string.clone().map(Id::Hash)) + } +} + #[derive(Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct Stats {