All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalaz.http.response.Body.scala Maven / Gradle / Ivy

The newest version!
package scalaz
package http
package response

/**
 * Type constructors that can be used in request and response bodies.
 *
 * @author Tony Morris
 */
sealed trait Body[OUT[_], A] {
  /**
   * Take the given value to a request/response body value.
   */
  def apply(a: A): OUT[Byte]
}

import Scalaz._
import scala.xml.{Elem, NodeSeq}

trait Bodys {
  import Body._
  /**
   * The body implementation for stream identity.
   */
  implicit val StreamIdentityBody = identityBody[Stream]

  /**
   * The body implementation for a stream of characters to a stream.
   */
  implicit def CharStreamBody(implicit c: CharSet) = body[Stream]((_: Stream[Char]).mkString.getBytes(c.value).toStream)

  /**
   * The body implementation for a list to a stream.
   */
  implicit val ByteListStreamBody: Body[Stream, List[Byte]] = body[Stream](_.toStream)

  /**
   * The body implementation for a list of characters to a stream.
   */
  implicit def CharListStreamBody(implicit c: CharSet): Body[Stream, List[Char]] = body[Stream](_.mkString.getBytes(c.value).toStream)

  /**
   * The body implementation for a list of characters to a stream.
   */
  implicit def StringStreamBody(implicit c: CharSet): Body[Stream, String] = body[Stream](_.getBytes(c.value).toStream)

  /**
   * The body implementation for an XML element to a stream.
   */
  implicit def ElemStreamBody(implicit c: CharSet): Body[Stream, Elem] = body[Stream](_.toString.getBytes(c.value).toStream)

  /**
   * The body implementation for an XML element to a stream.
   */
  implicit def NodeSeqStreamBody(implicit c: CharSet): Body[Stream, NodeSeq] = body[Stream](_.toString.getBytes(c.value).toStream)


  /**
   * The body implementation for an XHTML doctype to a stream.
   */
  implicit def XhtmlDoctypeStreamBody(implicit c: CharSet): Body[Stream, xhtml.Doctype] = body[Stream](_.asString.getBytes(c.value).toStream)

  
//  /**
//   * The contravariant implementation for a body.
//   */
//  def bodyContravariant[OUT[_]]: Contravariant[PartialType2[Body, OUT]#Apply] = new Contravariant[PartialType2[Body, OUT]#Apply] {
//    def contramap[A, B](body: PartialType2[Body, OUT]#Apply[A], f: B => A): Body[OUT, B] = new Body[OUT, B] {
//      def apply(b: B) = body(f(b))
//    }
//  }

  /**
   * The contravariant wrapper implementation for a body.
   */
  //def bodyContravariantW[OUT[_], A](c: PartialType2[Body, OUT]#Apply[A]): scalaz.ContravariantW[PartialType2[Body, OUT]#Apply, A] = contravariant[PartialType2[Body, OUT]#Apply](c)(bodyContravariant[OUT])

  /**
   * The contravariant implementation for a stream body.
   */
//  implicit val StreamBodyContravariant = bodyContravariant[Stream]

  /**
   * The contravariant wrapper implementation for a stream body.
   */
  //implicit def StreamBodyContravariantW[A](c: PartialType2[Body, Stream]#Apply[A]) = bodyContravariantW[Stream, A](c)
}
/**
 * Type constructors that can be used in request and response bodies.
 */
object Body extends Bodys {
  /**
   * Construct a body implementation from the given function.
   */
  def body[OUT[_]] = new {
    def apply[A](f: A => OUT[Byte]) = new Body[OUT, A] {
      def apply(a: A) = f(a)
    }
  }

  /**
   * The body implementation for identity.
   */
  def identityBody[OUT[_]] = body[OUT](identity[OUT[Byte]])

  /**
   * The body implementation for stream.
   */
  def streamBody[A](f: A => Stream[Byte]) = body[Stream](f)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy