From bea00f2196f42d5567cd1ee0087a2cb9b449347b Mon Sep 17 00:00:00 2001 From: Michael de Gans Date: Sun, 13 Oct 2024 16:45:49 -0700 Subject: [PATCH] add `Tool::parse` to convert from a `Serialize`able T to a `Tool` (#7) * add `Tool::from_serializable` to convert from a `Serialize`able T to a `Tool` --- Cargo.toml | 2 +- src/tool.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 31c7187..69ace02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "misanthropic" -version = "0.3.2" +version = "0.3.3" edition = "2021" authors = ["Michael de Gans "] description = "An async, ergonomic, client for Anthropic's Messages API" diff --git a/src/tool.rs b/src/tool.rs index 001095c..5d99451 100644 --- a/src/tool.rs +++ b/src/tool.rs @@ -255,6 +255,20 @@ impl<'a> Tool<'a> { pub fn is_cached(&self) -> bool { self.cache_control.is_some() } + + /// Try to convert from a serializable value to a [`Tool`]. + // A blanket impl for TryFrom where T: Serialize would be nice but it + // would conflict with the blanket impl for TryFrom where Value: + // Serialize. This is a bit of a hack but it works. + pub fn from_serializable( + value: T, + ) -> std::result::Result + where + T: Serialize, + { + let value = serde_json::to_value(value)?; + value.try_into() + } } impl TryFrom for Tool<'_> {