MCPcopy Index your code
hub / github.com/aws/aws-lambda-rust-runtime / convert_to_text

Function convert_to_text

lambda-http/src/response.rs:394–420  ·  view source on GitHub ↗
(body: B, content_type: &str)

Source from the content-addressed store, hash-verified

392}
393
394fn convert_to_text<B>(body: B, content_type: &str) -> BodyFuture
395where
396 B: HttpBody + Unpin + Send + 'static,
397 B::Data: Send,
398 B::Error: fmt::Debug,
399{
400 let mime_type = content_type.parse::<Mime>();
401
402 let encoding = match mime_type.as_ref() {
403 Ok(mime) => mime.get_param(CHARSET).unwrap_or(mime::UTF_8),
404 Err(_) => mime::UTF_8,
405 };
406
407 let label = encoding.as_ref().as_bytes();
408 let encoding = Encoding::for_label(label).unwrap_or(encoding_rs::UTF_8);
409
410 // assumes utf-8
411 Box::pin(async move {
412 let bytes = body.collect().await.expect("unable to read bytes from body").to_bytes();
413 let (content, _, _) = encoding.decode(&bytes);
414
415 match content {
416 Cow::Borrowed(content) => Body::from(content),
417 Cow::Owned(content) => Body::from(content),
418 }
419 })
420}
421
422pub type BodyFuture = Pin<Box<dyn Future<Output = Body> + Send>>;
423

Callers 1

convertMethod · 0.85

Calls 2

as_refMethod · 0.80
collectMethod · 0.80

Tested by

no test coverage detected