pub struct Content<'a> { /* private fields */ }Expand description
Text, or a JSON object or array.
The lifetime is the input the value borrows from: Content<'static> owns
everything it holds, which is what a decoded response produces, while a
request can be built from a &str the caller already has without copying
it.
Text is written out as a JSON string by any serializer. A JSON object or
array is spliced in byte for byte by the SDK and written out as data by
every other serializer, which is described on RawJson.
Reading a Content back always takes the value as JSON text. A JSON codec
(sonic-rs, serde_json) hands over that text, so any Content round-trips
through one; a non-JSON serde format hands over a text value as a bare
string, which is then parsed as JSON, so plain text such as low fails to
read back there.
use typesafe_sdk::Content;
let borrowed = Content::text("payments or invoices");
assert_eq!(borrowed.as_text(), Some("payments or invoices"));
let structured = Content::json(&[1, 2, 3])?;
assert_eq!(structured.as_json().map(|raw| raw.as_str()), Some("[1,2,3]"));Implementations§
Source§impl<'a> Content<'a>
impl<'a> Content<'a>
Sourcepub fn text(text: impl Into<Cow<'a, str>>) -> Self
pub fn text(text: impl Into<Cow<'a, str>>) -> Self
Wraps text, borrowing it when the caller owns it elsewhere.
A caller who wants the JSON shape of a value calls Content::json.
Sourcepub fn json<T>(value: &T) -> Result<Self, ContentError>
pub fn json<T>(value: &T) -> Result<Self, ContentError>
Encodes value and keeps the result.
A value that encodes to a JSON string becomes text, so
Content::json(&"hello") and Content::text("hello") are equal.
§Errors
Returns ContentError::Shape when the value encodes to a number, a
boolean or null, and ContentError::Encode when it cannot be
encoded at all.
Sourcepub fn into_owned(self) -> Content<'static>
pub fn into_owned(self) -> Content<'static>
Detaches the value from whatever it borrows, copying only if it has to.
Trait Implementations§
Source§impl<'de: 'a, 'a> Deserialize<'de> for Content<'a>
impl<'de: 'a, 'a> Deserialize<'de> for Content<'a>
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>,
impl<'a> Eq for Content<'a>
Source§impl<'a> From<&'a str> for Content<'a>
Builds the text shape, borrowing text; see Content::text.
impl<'a> From<&'a str> for Content<'a>
Builds the text shape, borrowing text; see Content::text.
Source§impl<'a> From<Cow<'a, str>> for Content<'a>
Builds the text shape, borrowing or owning as text does; see
Content::text.
impl<'a> From<Cow<'a, str>> for Content<'a>
Builds the text shape, borrowing or owning as text does; see
Content::text.
impl<'a> StructuralPartialEq for Content<'a>
Auto Trait Implementations§
impl<'a> Freeze for Content<'a>
impl<'a> RefUnwindSafe for Content<'a>
impl<'a> Send for Content<'a>
impl<'a> Sync for Content<'a>
impl<'a> Unpin for Content<'a>
impl<'a> UnsafeUnpin for Content<'a>
impl<'a> UnwindSafe for Content<'a>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.