Struct http_api_problem::HttpApiProblem
source · pub struct HttpApiProblem {
pub type_url: Option<String>,
pub status: Option<StatusCode>,
pub title: Option<String>,
pub detail: Option<String>,
pub instance: Option<String>,
/* private fields */
}
Expand description
Description of a problem that can be returned by an HTTP API based on RFC7807
Example
{
"type": "https://example.com/probs/out-of-credit",
"title": "You do not have enough credit.",
"detail": "Your current balance is 30, but that costs 50.",
"instance": "/account/12345/msgs/abc",
}
Purpose
The purpose of HttpApiProblem is to generate a meaningful response
for clients. It is not intended to be used as a replacement
for a proper Error
struct within applications.
For a struct which can be returned by HTTP handlers use ApiError which
can be enabled with the feature toggle api-error
. ApiError can be directly
converted into HttpApiProblem.
Status Codes and Responses
Prefer to use one of the constructors which
ensure that a StatusCode is set. If no StatusCode is
set and a transformation to a response of a web framework
is made a StatusCode becomes mandatory which in this case will
default to 500
.
When receiving an HttpApiProblem there might be an invalid
StatusCode contained. In this case the status
field will be empty.
This is a trade off so that the recipient does not have to deal with
another error and can still have access to the remaining fields of the
struct.
Fields§
§type_url: Option<String>
A URI reference RFC3986 that identifies the problem type. This specification encourages that, when dereferenced, it provide human-readable documentation for the problem type (e.g., using HTML [W3C.REC-html5-20141028]). When this member is not present, its value is assumed to be “about:blank”.
status: Option<StatusCode>
The HTTP status code RFC7231, Section 6 generated by the origin server for this occurrence of the problem.
title: Option<String>
A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization (e.g., using proactive content negotiation; see RFC7231, Section 3.4.
detail: Option<String>
A human-readable explanation specific to this occurrence of the problem.
instance: Option<String>
A URI reference that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced.
Implementations§
source§impl HttpApiProblem
impl HttpApiProblem
sourcepub fn new<T: Into<StatusCode>>(status: T) -> Self
pub fn new<T: Into<StatusCode>>(status: T) -> Self
Creates a new instance with the given StatusCode.
#Example
use http_api_problem::*;
let p = HttpApiProblem::new(StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(Some(StatusCode::INTERNAL_SERVER_ERROR), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn try_new<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode>
pub fn try_new<T: TryInto<StatusCode>>( status: T ) -> Result<Self, InvalidStatusCode>
Creates a new instance with the given StatusCode.
Fails if the argument can not be converted into a StatusCode.
#Example
use http_api_problem::*;
let p = HttpApiProblem::try_new(500).unwrap();
assert_eq!(Some(StatusCode::INTERNAL_SERVER_ERROR), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn with_title<T: Into<StatusCode>>(status: T) -> Self
pub fn with_title<T: Into<StatusCode>>(status: T) -> Self
Creates a new instance with title
derived from a StatusCode.
#Example
use http_api_problem::*;
let p = HttpApiProblem::with_title(StatusCode::NOT_FOUND);
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Not Found"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn try_with_title<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode>
pub fn try_with_title<T: TryInto<StatusCode>>( status: T ) -> Result<Self, InvalidStatusCode>
Creates a new instance with title
derived from a StatusCode.
Fails if the argument can not be converted into a StatusCode.
#Example
use http_api_problem::*;
let p = HttpApiProblem::try_with_title(404).unwrap();
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Not Found"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn with_title_and_type<T: Into<StatusCode>>(status: T) -> Self
pub fn with_title_and_type<T: Into<StatusCode>>(status: T) -> Self
Creates a new instance with the title
and type_url
derived from the
StatusCode.
#Example
use http_api_problem::*;
let p = HttpApiProblem::with_title_and_type(StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(Some(StatusCode::SERVICE_UNAVAILABLE), p.status);
assert_eq!(Some("Service Unavailable"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(Some("https://httpstatuses.com/503".to_string()), p.type_url);
assert_eq!(None, p.instance);
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let problem = HttpApiProblem::with_title_and_type(StatusCode::INTERNAL_SERVER_ERROR)
.value("error", &"this sucks")
.value("everything", &42)
.value(
"person",
&Person {
name: "Peter".into(),
age: 77,
},
);
let json = problem.json_string();
println!("{}", json);
let parsed: HttpApiProblem = serde_json::from_str(&json).unwrap();
println!("\n\n{:#?}", parsed);
}
sourcepub fn try_with_title_and_type<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode>
pub fn try_with_title_and_type<T: TryInto<StatusCode>>( status: T ) -> Result<Self, InvalidStatusCode>
Creates a new instance with the title
and type_url
derived from the
StatusCode.
Fails if the argument can not be converted into a StatusCode.
#Example
use http_api_problem::*;
let p = HttpApiProblem::try_with_title_and_type(503).unwrap();
assert_eq!(Some(StatusCode::SERVICE_UNAVAILABLE), p.status);
assert_eq!(Some("Service Unavailable"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(Some("https://httpstatuses.com/503".to_string()), p.type_url);
assert_eq!(None, p.instance);
sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates a new instance without any field set.
Prefer to use one of the other constructors which
ensure that a StatusCode is set. If no StatusCode is
set and a transformation to a response of a web framework
is made a StatusCode becomes mandatory which in this case will
default to 500
.
sourcepub fn status<T: Into<StatusCode>>(self, status: T) -> Self
pub fn status<T: Into<StatusCode>>(self, status: T) -> Self
Sets the status
#Example
use http_api_problem::*;
let p = HttpApiProblem::new(StatusCode::NOT_FOUND).title("Error");
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Error"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn type_url<T: Into<String>>(self, type_url: T) -> Self
pub fn type_url<T: Into<String>>(self, type_url: T) -> Self
Sets the type_url
#Example
use http_api_problem::*;
let p = HttpApiProblem::new(StatusCode::NOT_FOUND).type_url("http://example.com/my/real_error");
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(Some("http://example.com/my/real_error".to_string()), p.type_url);
assert_eq!(None, p.instance);
sourcepub fn try_status<T: TryInto<StatusCode>>(
self,
status: T
) -> Result<Self, InvalidStatusCode>
pub fn try_status<T: TryInto<StatusCode>>( self, status: T ) -> Result<Self, InvalidStatusCode>
Tries to set the status
Fails if the argument can not be converted into a StatusCode.
#Example
use http_api_problem::*;
let p = HttpApiProblem::try_new(404).unwrap().title("Error");
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Error"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn title<T: Into<String>>(self, title: T) -> Self
pub fn title<T: Into<String>>(self, title: T) -> Self
Sets the title
#Example
use http_api_problem::*;
let p = HttpApiProblem::new(StatusCode::NOT_FOUND).title("Another Error");
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Another Error"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn detail<T: Into<String>>(self, detail: T) -> HttpApiProblem
pub fn detail<T: Into<String>>(self, detail: T) -> HttpApiProblem
Sets the detail
#Example
use http_api_problem::*;
let p = HttpApiProblem::new(StatusCode::NOT_FOUND).detail("a detailed description");
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(None, p.title);
assert_eq!(Some("a detailed description".to_string()), p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);
sourcepub fn instance<T: Into<String>>(self, instance: T) -> HttpApiProblem
pub fn instance<T: Into<String>>(self, instance: T) -> HttpApiProblem
Sets the instance
#Example
use http_api_problem::*;
let p = HttpApiProblem::new(StatusCode::NOT_FOUND).instance("/account/1234/withdraw");
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(Some("/account/1234/withdraw".to_string()), p.instance);
sourcepub fn try_value<K, V>(
self,
key: K,
value: &V
) -> Result<Self, Box<dyn Error + Send + Sync + 'static>>
pub fn try_value<K, V>( self, key: K, value: &V ) -> Result<Self, Box<dyn Error + Send + Sync + 'static>>
Add a value that must be serializable.
The key must not be one of the field names of this struct.
These values get serialized into the JSON on top level.
sourcepub fn value<K, V>(self, key: K, value: &V) -> Self
pub fn value<K, V>(self, key: K, value: &V) -> Self
Add a value that must be serializable.
The key must not be one of the field names of this struct. If the key is a field name or the value is not serializable nothing happens.
These values get serialized into the JSON on top level.
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let problem = HttpApiProblem::with_title_and_type(StatusCode::INTERNAL_SERVER_ERROR)
.value("error", &"this sucks")
.value("everything", &42)
.value(
"person",
&Person {
name: "Peter".into(),
age: 77,
},
);
let json = problem.json_string();
println!("{}", json);
let parsed: HttpApiProblem = serde_json::from_str(&json).unwrap();
println!("\n\n{:#?}", parsed);
}
sourcepub fn set_value<K, V>(&mut self, key: K, value: &V)
pub fn set_value<K, V>(&mut self, key: K, value: &V)
Add a value that must be serializable.
The key must not be one of the field names of this struct. If the key is a field name or the value is not serializable nothing happens.
These values get serialized into the JSON on top level.
sourcepub fn get_value<K, V>(&self, key: &str) -> Option<V>where
V: DeserializeOwned,
pub fn get_value<K, V>(&self, key: &str) -> Option<V>where
V: DeserializeOwned,
Returns the deserialized field for the given key.
If the key does not exist or the field is not deserializable to
the target type None
is returned
pub fn try_set_value<K, V>( &mut self, key: K, value: &V ) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
sourcepub fn additional_fields(&self) -> &HashMap<String, Value>
pub fn additional_fields(&self) -> &HashMap<String, Value>
Returns a reference to the serialized fields
If the key does not exist or the field is not deserializable to
the target type None
is returned
sourcepub fn additional_fields_mut(&mut self) -> &mut HashMap<String, Value>
pub fn additional_fields_mut(&mut self) -> &mut HashMap<String, Value>
Returns a mutable reference to the serialized fields
If the key does not exist or the field is not deserializable to
the target type None
is returned
pub fn keys<K, V>(&self) -> impl Iterator<Item = &String>where
V: DeserializeOwned,
sourcepub fn json_value(&self, key: &str) -> Option<&Value>
pub fn json_value(&self, key: &str) -> Option<&Value>
Returns the serde_json::Value
for the given key if the key exists.
sourcepub fn json_bytes(&self) -> Vec<u8> ⓘ
pub fn json_bytes(&self) -> Vec<u8> ⓘ
Serialize to a JSON Vec<u8>
sourcepub fn json_string(&self) -> String
pub fn json_string(&self) -> String
Serialize to a JSON String
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let problem = HttpApiProblem::with_title_and_type(StatusCode::INTERNAL_SERVER_ERROR)
.value("error", &"this sucks")
.value("everything", &42)
.value(
"person",
&Person {
name: "Peter".into(),
age: 77,
},
);
let json = problem.json_string();
println!("{}", json);
let parsed: HttpApiProblem = serde_json::from_str(&json).unwrap();
println!("\n\n{:#?}", parsed);
}
sourcepub fn to_hyper_response(&self) -> Response<Body>
pub fn to_hyper_response(&self) -> Response<Body>
Creates a hyper response.
If status is None
500 - Internal Server Error
is the
default.
Requires the hyper
feature
sourcepub fn to_axum_response(&self) -> Response
pub fn to_axum_response(&self) -> Response
Creates an axum Response.
If status is None
500 - Internal Server Error
is the
default.
Requires the axum
feature
sourcepub fn to_actix_response(&self) -> HttpResponse
pub fn to_actix_response(&self) -> HttpResponse
Creates an actix
response.
If status is None
or not convertible
to an actix status 500 - Internal Server Error
is the
default.
Requires the actix-web
feature
sourcepub fn to_rocket_response(&self) -> Response<'static>
pub fn to_rocket_response(&self) -> Response<'static>
Creates a rocket
response.
If status is None
500 - Internal Server Error
is the
default.
Requires the rocket
feature
sourcepub fn to_salvo_response(&self) -> Response
pub fn to_salvo_response(&self) -> Response
Creates a salvo response.
If status is None
500 - Internal Server Error
is the
default.
Requires the salvo
feature
sourcepub fn to_tide_response(&self) -> Response
pub fn to_tide_response(&self) -> Response
Creates a tide response.
If status is None
500 - Internal Server Error
is the
default.
Requires the tide
feature
pub fn with_title_from_status<T: Into<StatusCode>>(status: T) -> Self
with_title
insteadpub fn with_title_and_type_from_status<T: Into<StatusCode>>(status: T) -> Self
with_title_and_type
insteadpub fn set_status<T: Into<StatusCode>>(self, status: T) -> Self
status
insteadpub fn set_title<T: Into<String>>(self, title: T) -> Self
title
insteadpub fn set_detail<T: Into<String>>(self, detail: T) -> Self
detail
insteadpub fn set_type_url<T: Into<String>>(self, type_url: T) -> Self
type_url
insteadpub fn set_instance<T: Into<String>>(self, instance: T) -> Self
instance
insteadTrait Implementations§
source§impl Clone for HttpApiProblem
impl Clone for HttpApiProblem
source§fn clone(&self) -> HttpApiProblem
fn clone(&self) -> HttpApiProblem
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for HttpApiProblem
impl Debug for HttpApiProblem
source§impl<'de> Deserialize<'de> for HttpApiProblem
impl<'de> Deserialize<'de> for HttpApiProblem
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>,
source§impl Display for HttpApiProblem
impl Display for HttpApiProblem
source§impl Error for HttpApiProblem
impl Error for HttpApiProblem
source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
source§impl From<ApiError> for HttpApiProblem
impl From<ApiError> for HttpApiProblem
source§impl From<HttpApiProblem> for HttpResponse
impl From<HttpApiProblem> for HttpResponse
source§fn from(problem: HttpApiProblem) -> HttpResponse
fn from(problem: HttpApiProblem) -> HttpResponse
source§impl From<HttpApiProblem> for Response
impl From<HttpApiProblem> for Response
source§fn from(problem: HttpApiProblem) -> Response
fn from(problem: HttpApiProblem) -> Response
source§impl From<HttpApiProblem> for Response<'static>
impl From<HttpApiProblem> for Response<'static>
source§fn from(problem: HttpApiProblem) -> Response<'static>
fn from(problem: HttpApiProblem) -> Response<'static>
source§impl From<HttpApiProblem> for Response
impl From<HttpApiProblem> for Response
source§fn from(problem: HttpApiProblem) -> Response
fn from(problem: HttpApiProblem) -> Response
source§impl From<HttpApiProblem> for Response
impl From<HttpApiProblem> for Response
source§fn from(problem: HttpApiProblem) -> Response
fn from(problem: HttpApiProblem) -> Response
source§impl From<Infallible> for HttpApiProblem
impl From<Infallible> for HttpApiProblem
source§fn from(error: Infallible) -> HttpApiProblem
fn from(error: Infallible) -> HttpApiProblem
source§impl From<StatusCode> for HttpApiProblem
impl From<StatusCode> for HttpApiProblem
source§fn from(status: StatusCode) -> HttpApiProblem
fn from(status: StatusCode) -> HttpApiProblem
source§impl IntoResponse for HttpApiProblem
impl IntoResponse for HttpApiProblem
source§fn into_response(self) -> Response
fn into_response(self) -> Response
source§impl JsonSchema for HttpApiProblem
impl JsonSchema for HttpApiProblem
source§fn schema_name() -> String
fn schema_name() -> String
source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moresource§impl OpenApiResponderInner for HttpApiProblem
impl OpenApiResponderInner for HttpApiProblem
source§fn responses(gen: &mut OpenApiGenerator) -> Result<Responses>
fn responses(gen: &mut OpenApiGenerator) -> Result<Responses>
openapi.json
format.source§impl PartialEq for HttpApiProblem
impl PartialEq for HttpApiProblem
source§fn eq(&self, other: &HttpApiProblem) -> bool
fn eq(&self, other: &HttpApiProblem) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'r> Responder<'r, 'static> for HttpApiProblem
impl<'r> Responder<'r, 'static> for HttpApiProblem
source§impl Serialize for HttpApiProblem
impl Serialize for HttpApiProblem
impl Eq for HttpApiProblem
impl Reject for HttpApiProblem
impl StructuralEq for HttpApiProblem
impl StructuralPartialEq for HttpApiProblem
Auto Trait Implementations§
impl RefUnwindSafe for HttpApiProblem
impl Send for HttpApiProblem
impl Sync for HttpApiProblem
impl Unpin for HttpApiProblem
impl UnwindSafe for HttpApiProblem
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
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.§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.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
self
into a collection.fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where
F: FnMut(T) -> U,
A: Array<Item = U>,
source§impl<'a, 'r, T> OpenApiResponder<'a, 'r> for Twhere
'r: 'a,
T: OpenApiResponderInner + Responder<'a, 'r>,
impl<'a, 'r, T> OpenApiResponder<'a, 'r> for Twhere
'r: 'a,
T: OpenApiResponderInner + Responder<'a, 'r>,
source§fn responses(gen: &mut OpenApiGenerator) -> Result<Responses, OpenApiError>
fn responses(gen: &mut OpenApiGenerator) -> Result<Responses, OpenApiError>
openapi.json
format.§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute
] value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
[Quirk
] value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition
] value
applies. Replaces any previous condition.
See the crate level docs for more details.
Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);