Skip to content

Commit

Permalink
add Tool::parse to convert from a Serializeable T to a Tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegans committed Oct 13, 2024
1 parent a2a1135 commit 22eb812
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "misanthropic"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
authors = ["Michael de Gans <[email protected]>"]
description = "An async, ergonomic, client for Anthropic's Messages API"
Expand Down
12 changes: 12 additions & 0 deletions src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ 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<T> where T: Serialize would be nice but it
// would conflict with the blanket impl for TryFrom<Value> where Value:
// Serialize. This is a bit of a hack but it works.
pub fn parse<T>(value: T) -> std::result::Result<Self, serde_json::Error>
where
T: Serialize,
{
let value = serde_json::to_value(value)?;
value.try_into()
}
}

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

0 comments on commit 22eb812

Please sign in to comment.