Response

Enum Response 

Source
pub enum Response<'r> {
    Ok {
        from: &'r str,
        to: &'r str,
        res: Vec<SrcDst<'r>>,
    },
    Err(Error),
}
Expand description

响应的信息。要么返回翻译结果,要么返回错误信息。

Variants§

§

Ok

Fields

§from: &'r str
§to: &'r str
§res: Vec<SrcDst<'r>>

原文中被 \n 分隔的多条翻译文本。

§

Err(Error)

Implementations§

Source§

impl<'r> Response<'r>

Source

pub fn dst(&self) -> Result<impl Iterator<Item = &str>, Error>

提取翻译内容。无翻译内容时,返回错误。

TODO: [BaiduError] 会经过两次内存分配,这种设计的原因是 anyhow crate 要求错误的类型必须是 'static。 [BaiduError] 一次分配的例子见 tests/baidu.rs

Source

pub fn dst_owned(self) -> Result<Vec<String>, Error>

提取翻译内容。无翻译内容时,返回错误。

Source

pub fn is_borrowed(&self) -> Option<bool>

翻译内容(即 SrcDstdst字段)是否为 Cow::Borrowed 类型。 比如英译中时,中文为代码点:

{
  "from": "en",
  "to":   "zh",
  "trans_result":[
    {"src": "hello", "dst": "\u4f60\u597d"},
    {"src": "world", "dst": "\u4e16\u754c"}
  ]
}

必须使用 String 或者 Cow::Owned 类型。

而 dst 为英文时,使用 &str 或者 Cow::Borrowed 类型可以减少分配。

§注意

无翻译内容时,返回 None

Trait Implementations§

Source§

impl<'r> Debug for Response<'r>

Source§

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

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

impl<'de: 'r, 'r> Deserialize<'de> for Response<'r>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more

Auto Trait Implementations§

§

impl<'r> Freeze for Response<'r>

§

impl<'r> RefUnwindSafe for Response<'r>

§

impl<'r> Send for Response<'r>

§

impl<'r> Sync for Response<'r>

§

impl<'r> Unpin for Response<'r>

§

impl<'r> UnwindSafe for Response<'r>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,