Trait fastsim_core::traits::SerdeAPI
source · pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
const ACCEPTED_BYTE_FORMATS: &'static [&'static str] = _;
const ACCEPTED_STR_FORMATS: &'static [&'static str] = _;
const CACHE_FOLDER: &'static str = _;
Show 17 methods
// Provided methods
fn init(&mut self) -> Result<()> { ... }
fn from_resource<P: AsRef<Path>>(filepath: P) -> Result<Self> { ... }
fn to_file<P: AsRef<Path>>(&self, filepath: P) -> Result<()> { ... }
fn to_writer<W: Write>(&self, wtr: W, format: &str) -> Result<()> { ... }
fn from_file<P: AsRef<Path>>(filepath: P) -> Result<Self> { ... }
fn to_str(&self, format: &str) -> Result<String> { ... }
fn from_str<S: AsRef<str>>(contents: S, format: &str) -> Result<Self> { ... }
fn from_reader<R: Read>(rdr: R, format: &str) -> Result<Self> { ... }
fn to_json(&self) -> Result<String> { ... }
fn from_json<S: AsRef<str>>(json_str: S) -> Result<Self> { ... }
fn to_yaml(&self) -> Result<String> { ... }
fn from_yaml<S: AsRef<str>>(yaml_str: S) -> Result<Self> { ... }
fn to_bincode(&self) -> Result<Vec<u8>> { ... }
fn from_bincode(encoded: &[u8]) -> Result<Self> { ... }
fn from_url<S: AsRef<str>>(url: S) -> Result<Self> { ... }
fn to_cache<P: AsRef<Path>>(&self, file_path: P) -> Result<()> { ... }
fn from_cache<P: AsRef<Path>>(file_path: P) -> Result<Self> { ... }
}Provided Associated Constants§
const ACCEPTED_BYTE_FORMATS: &'static [&'static str] = _
const ACCEPTED_STR_FORMATS: &'static [&'static str] = _
const CACHE_FOLDER: &'static str = _
Provided Methods§
sourcefn from_resource<P: AsRef<Path>>(filepath: P) -> Result<Self>
fn from_resource<P: AsRef<Path>>(filepath: P) -> Result<Self>
Read (deserialize) an object from a resource file packaged with the fastsim-core crate
§Arguments:
filepath- Filepath, relative to the top of theresourcesfolder, from which to read the object
sourcefn to_file<P: AsRef<Path>>(&self, filepath: P) -> Result<()>
fn to_file<P: AsRef<Path>>(&self, filepath: P) -> Result<()>
Write (serialize) an object to a file.
Supported file extensions are listed in ACCEPTED_BYTE_FORMATS.
Creates a new file if it does not already exist, otherwise truncates the existing file.
§Arguments
filepath- The filepath at which to write the object
fn to_writer<W: Write>(&self, wtr: W, format: &str) -> Result<()>
sourcefn from_file<P: AsRef<Path>>(filepath: P) -> Result<Self>
fn from_file<P: AsRef<Path>>(filepath: P) -> Result<Self>
Read (deserialize) an object from a file.
Supported file extensions are listed in ACCEPTED_BYTE_FORMATS.
§Arguments:
filepath: The filepath from which to read the object
sourcefn to_str(&self, format: &str) -> Result<String>
fn to_str(&self, format: &str) -> Result<String>
Write (serialize) an object into a string
§Arguments:
format- The target format, any of those listed inACCEPTED_STR_FORMATS
sourcefn from_str<S: AsRef<str>>(contents: S, format: &str) -> Result<Self>
fn from_str<S: AsRef<str>>(contents: S, format: &str) -> Result<Self>
Read (deserialize) an object from a string
§Arguments:
contents- The string containing the object dataformat- The source format, any of those listed inACCEPTED_STR_FORMATS
sourcefn from_reader<R: Read>(rdr: R, format: &str) -> Result<Self>
fn from_reader<R: Read>(rdr: R, format: &str) -> Result<Self>
Deserialize an object from anything that implements std::io::Read
§Arguments:
rdr- The reader from which to read object dataformat- The source format, any of those listed inACCEPTED_BYTE_FORMATS
sourcefn from_json<S: AsRef<str>>(json_str: S) -> Result<Self>
fn from_json<S: AsRef<str>>(json_str: S) -> Result<Self>
Read (deserialize) an object to a JSON string
§Arguments
json_str- JSON-formatted string to deserialize from
sourcefn from_yaml<S: AsRef<str>>(yaml_str: S) -> Result<Self>
fn from_yaml<S: AsRef<str>>(yaml_str: S) -> Result<Self>
Read (deserialize) an object from a YAML string
§Arguments
yaml_str- YAML-formatted string to deserialize from
sourcefn to_bincode(&self) -> Result<Vec<u8>>
fn to_bincode(&self) -> Result<Vec<u8>>
Write (serialize) an object to bincode-encoded bytes
sourcefn from_bincode(encoded: &[u8]) -> Result<Self>
fn from_bincode(encoded: &[u8]) -> Result<Self>
Read (deserialize) an object from bincode-encoded bytes
§Arguments
encoded- Encoded bytes to deserialize from
sourcefn from_url<S: AsRef<str>>(url: S) -> Result<Self>
fn from_url<S: AsRef<str>>(url: S) -> Result<Self>
Instantiates an object from a url. Accepts yaml and json file types
§Arguments
- url: URL (either as a string or url type) to object
Note: The URL needs to be a URL pointing directly to a file, for example a raw github URL.
sourcefn to_cache<P: AsRef<Path>>(&self, file_path: P) -> Result<()>
fn to_cache<P: AsRef<Path>>(&self, file_path: P) -> Result<()>
Takes an instantiated Rust object and saves it in the FASTSim data directory in
a rust_objects folder.
WARNING: If there is a file already in the data subdirectory with the
same name, it will be replaced by the new file.
§Arguments
- self (rust object)
- file_path: path to file within subdirectory. If only the file name is listed, file will sit directly within the subdirectory of the FASTSim data directory. If a path is given, the file will live within the path specified, within the subdirectory CACHE_FOLDER of the FASTSim data directory.
sourcefn from_cache<P: AsRef<Path>>(file_path: P) -> Result<Self>
fn from_cache<P: AsRef<Path>>(file_path: P) -> Result<Self>
Instantiates a Rust object from the subdirectory within the FASTSim data directory corresponding to the Rust Object (“vehices” for a RustVehice, “cycles” for a RustCycle, and the root folder of the data directory for all other objects).
§Arguments
- file_path: subpath to object, including file name, within subdirectory.
If the file sits directly in the subdirectory, this will just be the
file name.
Note: This function will work for all objects cached using the to_cache() method. If a file has been saved manually to a different subdirectory than the correct one for the object type (for instance a RustVehicle saved within a subdirectory other than “vehicles” using the utils::url_to_cache() function), then from_cache() will not be able to find and instantiate the object. Instead, use the from_file method, and use the utils::path_to_cache() to find the FASTSim data directory location if needed.