Enum Format

Source
#[non_exhaustive]
pub enum Format { Json, Json5, Ron, Toml, Yaml, }
Expand description

An enum of file formats supported by this build of cfgfifo.

Each variant is only present if the corresponding Cargo feature of cfgfifo was enabled at compile time.

A Format can be displayed as a string containing its name in all-uppercase, and a Format can be parsed from its name in any case.

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.
§

Json

Available on crate feature json only.

The JSON format, (de)serialized with the serde_json crate.

Serialization uses multiline/“pretty” format.

§

Json5

Available on crate feature json5 only.

The JSON5 format, deserialized with the json5 crate.

Serialization uses multiline/“pretty” format, performed via serde_json, as json5’s serialization (which also uses serde_json) is single-line/“non-pretty.”

§

Ron

Available on crate feature ron only.

The RON format, (de)serialized with the ron crate.

Serialization uses multiline/“pretty” format.

§

Toml

Available on crate feature toml only.

The TOML format, (de)serialized with the toml crate.

Serialization uses “pretty” format, in which arrays are serialized on multiple lines.

§

Yaml

Available on crate feature yaml only.

The YAML format, (de)serialized with the serde_yaml crate.

Implementations§

Source§

impl Format

Source

pub fn iter() -> FormatIter

Returns an iterator over all Format variants

Source

pub fn extensions(&self) -> &'static [&'static str]

Returns an array of the recognized file extensions for the file format.

Each returned file extension is lowercase and does not start with a period. The array of file extensions for a given format is sorted in lexicographical order.

§Example
use cfgfifo::Format;

assert_eq!(Format::Json.extensions(), &["json"]);
assert_eq!(Format::Yaml.extensions(), &["yaml", "yml"]);
Source

pub fn has_extension(&self, ext: &str) -> bool

Test whether a given file extension is associated with the format

The file extension is matched case-insensitively and may optionally start with a period.

§Example
use cfgfifo::Format;

assert!(Format::Json.has_extension(".json"));
assert!(Format::Json.has_extension("JSON"));
assert!(!Format::Json.has_extension("cfg"));
Source

pub fn from_extension(ext: &str) -> Option<Format>

Converts a file extension to the corresponding Format

File extensions are matched case-insensitively and may optionally start with a period. If the given file extension does not correspond to a known file format, None is returned.

§Example
use cfgfifo::Format;

assert_eq!(Format::from_extension(".json"), Some(Format::Json));
assert_eq!(Format::from_extension("YML"), Some(Format::Yaml));
assert_eq!(Format::from_extension("cfg"), None);
Source

pub fn identify<P: AsRef<Path>>(path: P) -> Result<Format, IdentifyError>

Determine the Format of a file path based on its file extension.

§Example
use cfgfifo::Format;

assert_eq!(Format::identify("path/to/file.json").unwrap(), Format::Json);
assert_eq!(Format::identify("path/to/file.RON").unwrap(), Format::Ron);
assert!(Format::identify("path/to/file.cfg").is_err());
assert!(Format::identify("path/to/file").is_err());
§Errors

Returns an error if the given file path does not have an extension, the extension is not valid Unicode, or the extension is unknown to this build.

Source

pub fn dump_to_string<T: Serialize>( &self, value: &T, ) -> Result<String, SerializeError>

Serialize a value to a string in this format

§Example
use cfgfifo::Format;
use serde::Serialize;

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
struct Data {
    name: String,
    size: u32,
    enabled: bool,
}

let datum = Data {
    name: String::from("Example"),
    size: 42,
    enabled: true,
};

let s = Format::Json.dump_to_string(&datum).unwrap();

assert_eq!(
    s,
    concat!(
        "{\n",
        "  \"name\": \"Example\",\n",
        "  \"size\": 42,\n",
        "  \"enabled\": true\n",
        "}"
    )
);
§Errors

Returns an error if the underlying serializer returns an error.

Source

pub fn load_from_str<T: DeserializeOwned>( &self, s: &str, ) -> Result<T, DeserializeError>

Deserialize a string in this format

§Example
use cfgfifo::Format;
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
struct Data {
    name: String,
    size: u32,
    enabled: bool,
}

let s = concat!(
    "name: Example\n",
    "size: 42\n",
    "enabled: true\n",
);

let datum: Data = Format::Yaml.load_from_str(s).unwrap();

assert_eq!(
    datum,
    Data {
        name: String::from("Example"),
        size: 42,
        enabled: true,
    }
);
§Errors

Returns an error if the underlying deserializer returns an error.

Source

pub fn dump_to_writer<W: Write, T: Serialize>( &self, writer: W, value: &T, ) -> Result<(), SerializeError>

Serialize a value to a writer in this format.

If the format’s serializer does not normally end its output with a newline, one is appended so that the written text always ends in a newline.

§Errors

Returns an error if an I/O error occurs or if the underlying serializer returns an error.

Source

pub fn load_from_reader<R: Read, T: DeserializeOwned>( &self, reader: R, ) -> Result<T, DeserializeError>

Deserialize a value in this format from a reader.

§Errors

Returns an error if an I/O error occurs or if the underlying deserializer returns an error.

Trait Implementations§

Source§

impl Clone for Format

Source§

fn clone(&self) -> Format

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 Debug for Format

Source§

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

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

impl Display for Format

Source§

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

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

impl FromStr for Format

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Format, <Self as FromStr>::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Format

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoEnumIterator for Format

Source§

impl Ord for Format

Source§

fn cmp(&self, other: &Format) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Format

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Format

Source§

fn partial_cmp(&self, other: &Format) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<&str> for Format

Source§

type Error = ParseError

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

fn try_from(s: &str) -> Result<Format, <Self as TryFrom<&str>>::Error>

Performs the conversion.
Source§

impl Copy for Format

Source§

impl Eq for Format

Source§

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 UnwindSafe for Format

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

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

Source§

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

Checks if this value is equivalent to the given key. Read more
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, 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<T> RuleType for T
where T: Copy + Debug + Eq + Hash + Ord,