1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
use crate::imports::*;
use std::collections::HashMap;
use ureq;
pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
const ACCEPTED_BYTE_FORMATS: &'static [&'static str] = &["yaml", "json", "bin"];
const ACCEPTED_STR_FORMATS: &'static [&'static str] = &["yaml", "json"];
const CACHE_FOLDER: &'static str = &"";
/// Specialized code to execute upon initialization
fn init(&mut self) -> anyhow::Result<()> {
Ok(())
}
/// Read (deserialize) an object from a resource file packaged with the `fastsim-core` crate
///
/// # Arguments:
///
/// * `filepath` - Filepath, relative to the top of the `resources` folder, from which to read the object
///
fn from_resource<P: AsRef<Path>>(filepath: P) -> anyhow::Result<Self> {
let filepath = filepath.as_ref();
let extension = filepath
.extension()
.and_then(OsStr::to_str)
.with_context(|| format!("File extension could not be parsed: {filepath:?}"))?;
let file = crate::resources::RESOURCES_DIR
.get_file(filepath)
.with_context(|| format!("File not found in resources: {filepath:?}"))?;
Self::from_reader(file.contents(), extension)
}
/// Write (serialize) an object to a file.
/// Supported file extensions are listed in [`ACCEPTED_BYTE_FORMATS`](`SerdeAPI::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_file<P: AsRef<Path>>(&self, filepath: P) -> anyhow::Result<()> {
let filepath = filepath.as_ref();
let extension = filepath
.extension()
.and_then(OsStr::to_str)
.with_context(|| format!("File extension could not be parsed: {filepath:?}"))?;
self.to_writer(File::create(filepath)?, extension)
}
fn to_writer<W: std::io::Write>(&self, wtr: W, format: &str) -> anyhow::Result<()> {
match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => serde_yaml::to_writer(wtr, self)?,
"json" => serde_json::to_writer(wtr, self)?,
"bin" => bincode::serialize_into(wtr, self)?,
_ => bail!(
"Unsupported format {format:?}, must be one of {:?}",
Self::ACCEPTED_BYTE_FORMATS
),
}
Ok(())
}
/// Read (deserialize) an object from a file.
/// Supported file extensions are listed in [`ACCEPTED_BYTE_FORMATS`](`SerdeAPI::ACCEPTED_BYTE_FORMATS`).
///
/// # Arguments:
///
/// * `filepath`: The filepath from which to read the object
///
fn from_file<P: AsRef<Path>>(filepath: P) -> anyhow::Result<Self> {
let filepath = filepath.as_ref();
let extension = filepath
.extension()
.and_then(OsStr::to_str)
.with_context(|| format!("File extension could not be parsed: {filepath:?}"))?;
let file = File::open(filepath).with_context(|| {
if !filepath.exists() {
format!("File not found: {filepath:?}")
} else {
format!("Could not open file: {filepath:?}")
}
})?;
Self::from_reader(file, extension)
}
/// Write (serialize) an object into a string
///
/// # Arguments:
///
/// * `format` - The target format, any of those listed in [`ACCEPTED_STR_FORMATS`](`SerdeAPI::ACCEPTED_STR_FORMATS`)
///
fn to_str(&self, format: &str) -> anyhow::Result<String> {
match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => self.to_yaml(),
"json" => self.to_json(),
_ => bail!(
"Unsupported format {format:?}, must be one of {:?}",
Self::ACCEPTED_STR_FORMATS
),
}
}
/// Read (deserialize) an object from a string
///
/// # Arguments:
///
/// * `contents` - The string containing the object data
/// * `format` - The source format, any of those listed in [`ACCEPTED_STR_FORMATS`](`SerdeAPI::ACCEPTED_STR_FORMATS`)
///
fn from_str<S: AsRef<str>>(contents: S, format: &str) -> anyhow::Result<Self> {
Ok(
match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => Self::from_yaml(contents)?,
"json" => Self::from_json(contents)?,
_ => bail!(
"Unsupported format {format:?}, must be one of {:?}",
Self::ACCEPTED_STR_FORMATS
),
},
)
}
/// Deserialize an object from anything that implements [`std::io::Read`]
///
/// # Arguments:
///
/// * `rdr` - The reader from which to read object data
/// * `format` - The source format, any of those listed in [`ACCEPTED_BYTE_FORMATS`](`SerdeAPI::ACCEPTED_BYTE_FORMATS`)
///
fn from_reader<R: std::io::Read>(rdr: R, format: &str) -> anyhow::Result<Self> {
let mut deserialized: Self = match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => serde_yaml::from_reader(rdr)?,
"json" => serde_json::from_reader(rdr)?,
"bin" => bincode::deserialize_from(rdr)?,
_ => bail!(
"Unsupported format {format:?}, must be one of {:?}",
Self::ACCEPTED_BYTE_FORMATS
),
};
deserialized.init()?;
Ok(deserialized)
}
/// Write (serialize) an object to a JSON string
fn to_json(&self) -> anyhow::Result<String> {
Ok(serde_json::to_string(&self)?)
}
/// Read (deserialize) an object to a JSON string
///
/// # Arguments
///
/// * `json_str` - JSON-formatted string to deserialize from
///
fn from_json<S: AsRef<str>>(json_str: S) -> anyhow::Result<Self> {
let mut json_de: Self = serde_json::from_str(json_str.as_ref())?;
json_de.init()?;
Ok(json_de)
}
/// Write (serialize) an object to a YAML string
fn to_yaml(&self) -> anyhow::Result<String> {
Ok(serde_yaml::to_string(&self)?)
}
/// Read (deserialize) an object from a YAML string
///
/// # Arguments
///
/// * `yaml_str` - YAML-formatted string to deserialize from
///
fn from_yaml<S: AsRef<str>>(yaml_str: S) -> anyhow::Result<Self> {
let mut yaml_de: Self = serde_yaml::from_str(yaml_str.as_ref())?;
yaml_de.init()?;
Ok(yaml_de)
}
/// Write (serialize) an object to bincode-encoded bytes
fn to_bincode(&self) -> anyhow::Result<Vec<u8>> {
Ok(bincode::serialize(&self)?)
}
/// Read (deserialize) an object from bincode-encoded bytes
///
/// # Arguments
///
/// * `encoded` - Encoded bytes to deserialize from
///
fn from_bincode(encoded: &[u8]) -> anyhow::Result<Self> {
let mut bincode_de: Self = bincode::deserialize(encoded)?;
bincode_de.init()?;
Ok(bincode_de)
}
/// 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.
fn from_url<S: AsRef<str>>(url: S) -> anyhow::Result<Self> {
let url = url::Url::parse(url.as_ref())?;
let format = url
.path_segments()
.and_then(|segments| segments.last())
.and_then(|filename| Path::new(filename).extension())
.and_then(OsStr::to_str)
.with_context(|| "Could not parse file format from URL: {url:?}")?;
let response = ureq::get(url.as_ref()).call()?.into_reader();
Self::from_reader(response, format)
}
/// 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.
fn to_cache<P: AsRef<Path>>(&self, file_path: P) -> anyhow::Result<()> {
let file_name = file_path
.as_ref()
.file_name()
.with_context(|| "Could not determine file name")?
.to_str()
.context("Could not determine file name.")?;
let file_path_internal = file_path
.as_ref()
.to_str()
.context("Could not determine file name.")?;
let subpath = if file_name == file_path_internal {
PathBuf::from(Self::CACHE_FOLDER)
} else {
Path::new(Self::CACHE_FOLDER).join(
file_path_internal
.strip_suffix(file_name)
.context("Could not determine path to subdirectory.")?,
)
};
let data_subdirectory = create_project_subdir(subpath)
.with_context(|| "Could not find or build Fastsim data subdirectory.")?;
let file_path = data_subdirectory.join(file_name);
self.to_file(file_path)
}
/// 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.
fn from_cache<P: AsRef<Path>>(file_path: P) -> anyhow::Result<Self> {
let full_file_path = Path::new(Self::CACHE_FOLDER).join(file_path);
let path_including_directory = path_to_cache()?.join(full_file_path);
Self::from_file(path_including_directory)
}
}
pub trait ApproxEq<Rhs = Self> {
fn approx_eq(&self, other: &Rhs, tol: f64) -> bool;
}
macro_rules! impl_approx_eq_for_strict_eq_types {
($($strict_eq_type: ty),*) => {
$(
impl ApproxEq for $strict_eq_type {
fn approx_eq(&self, other: &$strict_eq_type, _tol: f64) -> bool {
return self == other;
}
}
)*
}
}
impl_approx_eq_for_strict_eq_types!(
u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, bool, &str, String
);
macro_rules! impl_approx_eq_for_floats {
($($float_type: ty),*) => {
$(
impl ApproxEq for $float_type {
fn approx_eq(&self, other: &$float_type, tol: f64) -> bool {
return (((other - self) / (self + other)).abs() as f64) < tol || ((other - self).abs() as f64) < tol;
}
}
)*
}
}
impl_approx_eq_for_floats!(f32, f64);
impl<T> ApproxEq for Vec<T>
where
T: ApproxEq,
{
fn approx_eq(&self, other: &Vec<T>, tol: f64) -> bool {
return self
.iter()
.zip(other.iter())
.all(|(x, y)| x.approx_eq(y, tol));
}
}
impl<T> ApproxEq for Array1<T>
where
T: ApproxEq + std::clone::Clone,
{
fn approx_eq(&self, other: &Array1<T>, tol: f64) -> bool {
self.to_vec().approx_eq(&other.to_vec(), tol)
}
}
impl<T> ApproxEq for Option<T>
where
T: ApproxEq,
{
fn approx_eq(&self, other: &Option<T>, tol: f64) -> bool {
if self.is_none() && other.is_none() {
true
} else if self.is_some() && other.is_some() {
self.as_ref()
.unwrap()
.approx_eq(other.as_ref().unwrap(), tol)
} else {
false
}
}
}
impl<K, V, S> ApproxEq for HashMap<K, V, S>
where
K: Eq + std::hash::Hash,
V: ApproxEq,
S: std::hash::BuildHasher,
{
fn approx_eq(&self, other: &HashMap<K, V, S>, tol: f64) -> bool {
if self.len() != other.len() {
return false;
}
return self
.iter()
.all(|(key, value)| other.get(key).map_or(false, |v| value.approx_eq(v, tol)));
}
}