#[non_exhaustive]pub enum Format {
Json,
Json5,
Ron,
Toml,
Yaml,
}
Expand description
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Json
json
only.The JSON format, (de)serialized with the
serde_json
crate.
Serialization uses multiline/“pretty” format.
Json5
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
ron
only.Toml
toml
only.The TOML format, (de)serialized with the toml
crate.
Serialization uses “pretty” format, in which arrays are serialized on multiple lines.
Yaml
yaml
only.The YAML format, (de)serialized with the
serde_yaml
crate.
Implementations§
Source§impl Format
impl Format
Sourcepub fn iter() -> FormatIter ⓘ
pub fn iter() -> FormatIter ⓘ
Returns an iterator over all Format
variants
Sourcepub fn extensions(&self) -> &'static [&'static str]
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"]);
Sourcepub fn has_extension(&self, ext: &str) -> bool
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"));
Sourcepub fn from_extension(ext: &str) -> Option<Format>
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);
Sourcepub fn identify<P: AsRef<Path>>(path: P) -> Result<Format, IdentifyError>
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.
Sourcepub fn dump_to_string<T: Serialize>(
&self,
value: &T,
) -> Result<String, SerializeError>
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.
Sourcepub fn load_from_str<T: DeserializeOwned>(
&self,
s: &str,
) -> Result<T, DeserializeError>
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.
Sourcepub fn dump_to_writer<W: Write, T: Serialize>(
&self,
writer: W,
value: &T,
) -> Result<(), SerializeError>
pub fn dump_to_writer<W: Write, T: Serialize>( &self, writer: W, value: &T, ) -> Result<(), SerializeError>
Sourcepub fn load_from_reader<R: Read, T: DeserializeOwned>(
&self,
reader: R,
) -> Result<T, DeserializeError>
pub fn load_from_reader<R: Read, T: DeserializeOwned>( &self, reader: R, ) -> Result<T, DeserializeError>
Trait Implementations§
Source§impl IntoEnumIterator for Format
impl IntoEnumIterator for Format
type Iterator = FormatIter
fn iter() -> FormatIter ⓘ
Source§impl Ord for Format
impl Ord for Format
Source§impl PartialOrd for Format
impl PartialOrd for Format
impl Copy for Format
impl Eq for Format
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> 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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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.