Skip to content

Commit bea00f2

Browse files
authored
add Tool::parse to convert from a Serializeable T to a Tool (#7)
* add `Tool::from_serializable` to convert from a `Serialize`able T to a `Tool`
1 parent a2a1135 commit bea00f2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "misanthropic"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
edition = "2021"
55
authors = ["Michael de Gans <[email protected]>"]
66
description = "An async, ergonomic, client for Anthropic's Messages API"

src/tool.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@ impl<'a> Tool<'a> {
255255
pub fn is_cached(&self) -> bool {
256256
self.cache_control.is_some()
257257
}
258+
259+
/// Try to convert from a serializable value to a [`Tool`].
260+
// A blanket impl for TryFrom<T> where T: Serialize would be nice but it
261+
// would conflict with the blanket impl for TryFrom<Value> where Value:
262+
// Serialize. This is a bit of a hack but it works.
263+
pub fn from_serializable<T>(
264+
value: T,
265+
) -> std::result::Result<Self, serde_json::Error>
266+
where
267+
T: Serialize,
268+
{
269+
let value = serde_json::to_value(value)?;
270+
value.try_into()
271+
}
258272
}
259273

260274
impl TryFrom<serde_json::Value> for Tool<'_> {

0 commit comments

Comments
 (0)