Skip to main content

ResponseBody

Enum ResponseBody 

Source
#[non_exhaustive]
pub enum ResponseBody { Download { path: PathBuf, filename: String, }, Preview { path: PathBuf, }, Redirect { url: String, }, Raw { body: String, content_type: String, }, }
Expand description

Action 的特殊业务响应体:文件下载、文件预览、重定向、原始响应体。

Action 将本类型作为 type Output 返回时,派发边界 (DynAction::dispatch)会识别并把它转为 ApiResponse::attachment,普通输出的 JSON 线格式不受影响。

标注 #[non_exhaustive]:未来新增变体不再构成破坏性变更,下游穷尽 match 请保留通配臂。

§示例

ⓘ
use yang_base::action::ResponseBody;

async fn index(&self, ctx: ActionContext, input: Self::Input) -> Result<Self::Output, BaseError> {
    // 文件下载
    Ok(ResponseBody::download("/data/report.pdf", "report.pdf"))
    // 文件预览
    // Ok(ResponseBody::preview("/data/a.png"))
    // 重定向
    // Ok(ResponseBody::redirect("https://example.com/next"))
    // 原始 JSON 响应体(不套 ApiResponse 包络)
    // ResponseBody::raw(r#"{"code":0,"msg":"ok"}"#, "application/json")
}

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Download

文件下载:传输层以 Content-Disposition: attachment 返回文件字节。

Fields

§path: PathBuf

服务器本地文件路径。

§filename: String

下载文件名(写入 Content-Disposition 的 filename 参数)。

§

Preview

文件预览:传输层以 Content-Disposition: inline 返回文件字节。

Fields

§path: PathBuf

服务器本地文件路径。

§

Redirect

重定向:传输层返回 302 状态码与 Location 头。

Fields

§url: String

目标地址。

§

Raw

原始响应体:传输层直接以 content_type 返回 body,不套 ApiResponse 包络。

供外部系统要求固定响应形状(顶层键名、层级与框架包络不一致)时使用。 content_type 只允许 application/json;body 必须是该类型下的完整响应文本。

请优先用 ResponseBody::raw 构造,它会校验 content_type;派发边界 wrap_dispatch_output 会对直接构造的本变体再执行同一白名单校验。

Fields

§body: String

完整响应体文本(UTF-8)。

§content_type: String

响应 Content-Type;当前只允许 application/json。

Implementations§

Source§

impl ResponseBody

Source

pub fn download(path: impl Into<PathBuf>, filename: impl Into<String>) -> Self

构造文件下载响应。

Source

pub fn preview(path: impl Into<PathBuf>) -> Self

构造文件预览响应。

Source

pub fn redirect(url: impl Into<String>) -> Self

构造重定向响应。

Source

pub fn raw( body: impl Into<String>, content_type: impl Into<String>, ) -> Result<Self, BaseError>

构造原始响应体,传输层将原样返回 body 而不套 ApiResponse 包络。

这是给「外部系统契约与框架包络不兼容」用的逃生口,不是通用响应通道—— content_type 被限死为 application/json,防止它被用来返回 text/html。 该白名单与派发边界 wrap_dispatch_output 的运行期再校验完全一致; 直接构造 ResponseBody::Raw 的非法 content_type 会在派发时被拒绝。

§错误

content_type 不是 application/json 时返回 BaseError::ConfigError。

§示例
use yang_base::action::ResponseBody;

let body = ResponseBody::raw(r#"{"code":0}"#, "application/json")?;
assert!(matches!(body, ResponseBody::Raw { .. }));

// 非 JSON 类型被拒绝
assert!(ResponseBody::raw("<html></html>", "text/html").is_err());

Trait Implementations§

Source§

impl Clone for ResponseBody

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ResponseBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ResponseBody

Source§

impl From<ResponseBody> for ResponseAttachment

Source§

fn from(body: ResponseBody) -> Self

Converts to this type from the input type.
Source§

impl JsonSchema for ResponseBody

Source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
Source§

impl PartialEq for ResponseBody

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for ResponseBody

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ResponseBody

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more