pub enum Content {
Text(String),
Object(Map<String, Value>),
Array(Vec<Value>),
}Expand description
A string, JSON object or JSON array.
The API accepts exactly these three shapes for request state, question
instructions, Choice option descriptions and Score levels. A string suits simple
text; an object with named fields suits content that has several parts.
Object fields are kept in serde_json::Map order: sorted by key, unless your
application enables serde_json’s preserve_order feature.
use serde_json::json;
use typesafe_client::Content;
let text = Content::from("My card was charged twice.");
let record = Content::from(json!({ "message": "My card was charged twice.", "order_id": "A-104" }));
assert_eq!(text.as_text(), Some("My card was charged twice."));
assert!(matches!(record, Content::Object(_)));Variants§
Text(String)
Plain text.
Object(Map<String, Value>)
A JSON object with named fields.
Array(Vec<Value>)
A JSON array, such as a sequence of messages.
Implementations§
Source§impl Content
impl Content
Sourcepub fn json<T: Serialize + ?Sized>(value: &T) -> Result<Self, Error>
pub fn json<T: Serialize + ?Sized>(value: &T) -> Result<Self, Error>
Serializes value into content, for example an application struct used as state.
Numbers, booleans and null become their JSON text, because the API only accepts
strings, objects and arrays. The error converts into Error with ?.
Sourcepub fn as_text(&self) -> Option<&str>
pub fn as_text(&self) -> Option<&str>
The text, if this is Content::Text.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Content
impl<'de> Deserialize<'de> for Content
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Content
Auto Trait Implementations§
impl Freeze for Content
impl RefUnwindSafe for Content
impl Send for Content
impl Sync for Content
impl Unpin for Content
impl UnsafeUnpin for Content
impl UnwindSafe for Content
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more