Struct Options

Source
pub struct Options<Extra: ExtraOptions = DefaultExtraOptions> { /* private fields */ }
Expand description

Options for deserialization.

The most important methods are:

Implementations§

Source§

impl Options

Source

pub fn new_json() -> Options<JsonExtraOptions>

Available on crate features rand and serde_json only.

Default config for JSON.

This will currently generate a short extra trailer on inputs for improved deserialization of incomplete JSON.

Source

pub fn new_nonce() -> Options<DefaultExtraOptions>

Available on crate feature rand only.

Basic config, suitable for any data format.

These options support adding a randomized trailer to the input. However, you should probably call Options::set_random_trailer to specify how this trailer should be removed from parsed strings.

Source

pub fn new_no_nonce() -> Options<DefaultExtraOptions>

Basic config, suitable for any data format. However, this config does not allow adding a randomized trailer to the input, which tends to benefit many formats.

  • For serde_json, this means you won’t get incomplete strings deserialized

  • For serde_yaml, this means that your output will flicker, as it seems to buffer lines somehow, and if a line has an unterminated string, then the whole line will be missing.

Source§

impl<Extra: ExtraOptions> Options<Extra>

Source

pub fn with_max_n_backtracks(self, max_n_backtracks: Option<usize>) -> Self

Source

pub fn deserialize_from_json_str<T>( self, json: Cow<'_, str>, ) -> Result<T, Error<Error>>
where T: for<'de> Deserialize<'de>,

Available on crate features rand and serde_json only.

Like crate::from_json_str, but with options. This applies the random trailer.

Source

pub fn deserialize_from_json_slice<T>( self, json: Cow<'_, [u8]>, ) -> Result<T, Error<Error>>
where T: for<'de> Deserialize<'de>,

Available on crate features rand and serde_json only.

Like crate::from_json_slice, but with options. This applies the random trailer.

Source

pub fn deserialize_from_json_slice_plain_return_borrowed<'de, T>( self, json: &'de impl AsRef<[u8]>, ) -> Result<T, Error<Error>>
where T: Deserialize<'de>,

Available on crate feature serde_json only.

Like Self::deserialize_from_json_slice, but can deserialize borrowed strings and return them directly.

This comes at the cost that we cannot use the random trailer technique that gives us access to the contents of incomplete strings.

If you need incomplete strings as well, then use Self::deserialize_from_json_slice_borrowed.

#[derive(Debug, Deserialize, PartialEq)]
struct TravelMode {
   #[serde(default)]
   mode: String,
   benefit: Option<String>
}

let json = r#"[{"mode": "foot", "benefit": "healthy"}, {"mode": "incomplete"#;
let modes: Vec<TravelMode> = deser_incomplete::Options::new_json().deserialize_from_json_slice_plain_return_borrowed(&json).unwrap();
assert_eq!(modes, [
   TravelMode { mode: "foot".to_string(), benefit: Some("healthy".to_string()) },
   TravelMode { mode: "".to_string(), benefit: None },
   // Note: this function fails on incomplete strings, because
   // the randomized trailer is needed for those.
]);
Source

pub fn deserialize_from_json_str_borrowed<'de, T>( self, InputPlusTrailer: &'de InputPlusTrailer<impl AsRef<str>>, ) -> Result<T, Error<Error>>
where T: Deserialize<'de>,

Available on crate feature serde_json only.

Advanced API. Lets you deserialize into borrowed types like &str, while supporting the random trailer that gives us access to the contents of incomplete strings.

(The difference is that this only needs T: serde::de::Deserialize<'de>, which is weaker.)

Note: This API is relatively likely to change (more unstable) compared to Self::deserialize_from_json_str.

/// Note: `&'a str` instead of `String`.
///
/// Like with serde_json, deserializing to &str can fail. Instead, you should probably
/// use `Cow<str>`, or just `String`.
#[derive(Debug, Deserialize, PartialEq)]
struct TravelMode<'a> {
   mode: &'a str,
   benefit: Option<&'a str>
}

let json = r#"[{"mode": "foot", "benefit": "healthy"}, {"mode": "aeropl"#;
let options = deser_incomplete::Options::new_json();
let prepared = options.prepare_str_for_borrowed_deserialization(json.into());
let modes: Vec<TravelMode> = options.deserialize_from_json_str_borrowed(&prepared).unwrap();
assert_eq!(modes, [
   TravelMode { mode: "foot", benefit: Some("healthy") },
   TravelMode { mode: "aeropl", benefit: None }
]);
Source

pub fn deserialize_from_json_slice_borrowed<'de, T>( self, InputPlusTrailer: &'de InputPlusTrailer<impl AsRef<[u8]>>, ) -> Result<T, Error<Error>>
where T: Deserialize<'de>,

Available on crate feature serde_json only.

Advanced API. See Self::deserialize_from_json_str_borrowed, or use Self::deserialize_from_json_slice for a simpler API.

Note: This API is relatively likely to change (more unstable) compared to Self::deserialize_from_json_slice.

Source

pub fn prepare_str_for_borrowed_deserialization<'a>( &self, input: Cow<'a, str>, ) -> InputPlusTrailer<Cow<'a, str>>

Available on crate feature rand only.

Prepare a string for borrowed deserialization with a method like Self::deserialize_from_json_str_borrowed, by appending the random trailer.

This returns a newtype wrapper, so you can undo the effects yourself.

Source

pub fn prepare_slice_for_borrowed_deserialization<'a>( &self, input: Cow<'a, [u8]>, ) -> InputPlusTrailer<Cow<'a, [u8]>>

Available on crate feature rand only.

Prepare a slice for borrowed deserialization with a method like Self::deserialize_from_json_slice_borrowed, by appending the random trailer.

This returns a newtype wrapper, so you can undo the effects yourself.

Source

pub fn disable_random_tag(self) -> Self

Available on crate feature rand only.

Don’t use a random tag. This can make deserialization a tiny bit cheaper, because the input does not have to be reallocated.

Source§

impl<R, F, RT> Options<ExtraOptionsStruct<R, F, RT>>
where R: MakeReporter, F: MakeFallbackProvider, RT: RandomTrailer,

Source

pub fn set_random_trailer<RT2>( self, random_trailer: RT2, ) -> Options<ExtraOptionsStruct<R, F, RT2>>
where RT2: RandomTrailer,

Available on crate feature rand only.

Set a different method for randomized trailers.

Source§

impl<Extra: ExtraOptions> Options<Extra>

Source

pub fn deserialize_source<'de, T, S>( self, source: S, ) -> Result<T, Error<S::Error>>
where T: Deserialize<'de>, S: Source<'de>,

Deserialize from a generic Source.

Unlike Self::deserialize_from_json_str etc, this method does not automatically append a random trailer. If you want that, then you can use Self::prepare_str_for_borrowed_deserialization.

You can use Options::deserialize_seed instead if you need to pass a seed.

Source

pub fn deserialize_seed<'de, T, S>( self, seed: T, source: S, ) -> Result<T::Value, Error<S::Error>>
where T: DeserializeSeed<'de> + Clone, S: Source<'de>,

Deserialize from a seed.

Unlike Self::deserialize_from_json_str etc, this method does not automatically append a random trailer. If you want that, then you can use Self::prepare_str_for_borrowed_deserialization.

If you don’t need a seed, then you can use Options::deserialize_source.

Trait Implementations§

Source§

impl<Extra: Clone + ExtraOptions> Clone for Options<Extra>

Source§

fn clone(&self) -> Options<Extra>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Extra: Debug + ExtraOptions> Debug for Options<Extra>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Extra> Freeze for Options<Extra>
where Extra: Freeze,

§

impl<Extra> RefUnwindSafe for Options<Extra>
where Extra: RefUnwindSafe,

§

impl<Extra> Send for Options<Extra>
where Extra: Send,

§

impl<Extra> Sync for Options<Extra>
where Extra: Sync,

§

impl<Extra> Unpin for Options<Extra>
where Extra: Unpin,

§

impl<Extra> UnwindSafe for Options<Extra>
where Extra: UnwindSafe,

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = 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<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