pub enum Format {
Html,
Json,
Xml,
Text,
Markdown,
Other,
}Expand description
Response format a client asks for in its Accept header, for actions that answer HTML or JSON (Rails’ respond_to).
The media type with the highest q wins (first listed on ties):
text/html and application/xhtml+xml are Html,
application/json and any +json type Json,
application/xml, text/xml and +xml types Xml,
text/plain Text, text/markdown Markdown,
*/* and text/* the first format of the list above. A missing or empty
Accept header is Html, like
Rails; types Ocre does not know are Other, usually
answered with 406 Not Acceptable. Never rejects; no binding call.
§Examples
use axum::{extract::{Path, State}, response::{Html, IntoResponse, Response}};
use ocre::{Ctx, Format, Json, OptionExt, Result, params};
async fn show(State(ctx): State<Ctx>, format: Format, Path(id): Path<i64>) -> Result<Response> {
let post: serde_json::Value =
ctx.db()?.first("SELECT id, title FROM posts WHERE id = ?1", params![id]).await?.or_404()?;
Ok(match format {
Format::Json => Json(post).into_response(),
_ => Html(format!("<h1>{}</h1>", post["title"])).into_response(),
})
}Variants§
Html
text/html: a page.
Json
application/json.
Xml
application/xml or text/xml, e.g. an RSS or Atom feed.
Text
text/plain.
Markdown
text/markdown, e.g. for LLM clients (Rails’ format.md); answer with Markdown.
Other
Only types Ocre does not know, e.g. application/pdf.
Implementations§
Source§impl Format
impl Format
Sourcepub fn from_headers(headers: &HeaderMap) -> Self
pub fn from_headers(headers: &HeaderMap) -> Self
Reads the Accept header; see Format for the rules.
§Examples
use axum::http::HeaderMap;
use ocre::Format;
let mut headers = HeaderMap::new();
headers.insert("accept", "text/html;q=0.9, application/json".parse().unwrap());
assert_eq!(Format::from_headers(&headers), Format::Json);
assert_eq!(Format::from_headers(&HeaderMap::new()), Format::Html);Trait Implementations§
impl Copy for Format
impl Eq for Format
Source§impl<S: Send + Sync> FromRequestParts<S> for Format
impl<S: Send + Sync> FromRequestParts<S> for Format
Source§type Rejection = Infallible
type Rejection = Infallible
Source§async fn from_request_parts(
parts: &mut Parts,
_state: &S,
) -> Result<Self, Self::Rejection>
async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>
impl StructuralPartialEq for Format
Auto Trait Implementations§
impl Freeze for Format
impl RefUnwindSafe for Format
impl Send for Format
impl Sync for Format
impl Unpin for Format
impl UnsafeUnpin for Format
impl UnwindSafe for Format
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,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.