Skip to content

Commit

Permalink
fix From<String> for Part
Browse files Browse the repository at this point in the history
The `#[from]` tag did not work as expected. Need to read `derive_more` docs on it.
  • Loading branch information
mdegans committed Aug 28, 2024
1 parent 9b1f05b commit abc1c52
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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.1.3"
version = "0.1.4"
edition = "2021"
authors = ["Michael de Gans <[email protected]>"]
description = "An async, ergonomic, client for Anthropic's Messages API"
Expand Down
22 changes: 17 additions & 5 deletions src/request/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ impl Content {
/// A [`Content`] [`Part`] of a [`Message`], either [`Text`] or [`Image`].
///
/// [`Text`]: Part::Text
#[derive(
Debug, Serialize, Deserialize, derive_more::From, derive_more::Display,
)]
#[derive(Debug, Serialize, Deserialize, derive_more::Display)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum Part {
Expand All @@ -122,7 +120,6 @@ pub enum Part {
#[display("{}", text)]
Text {
/// The actual text content.
#[from]
text: String,
/// Use prompt caching. The [`text`] needs to be at least 1024 tokens
/// for Sonnet 3.5 and Opus 3.0 or 2048 for Haiku 3.0 or this will be
Expand All @@ -135,7 +132,6 @@ pub enum Part {
},
/// Image content.
Image {
#[from]
#[serde(rename = "source")]
/// An base64 encoded image.
image: Image,
Expand Down Expand Up @@ -198,6 +194,22 @@ impl From<&str> for Part {
}
}

impl From<String> for Part {
fn from(text: String) -> Self {
Self::Text {
text,
#[cfg(feature = "prompt-caching")]
cache_control: None,
}
}
}

impl From<Image> for Part {
fn from(image: Image) -> Self {
Self::Image { image }
}
}

/// Cache control for prompt caching.
#[cfg(feature = "prompt-caching")]
#[derive(Default, Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit abc1c52

Please sign in to comment.