Skip to content

Commit

Permalink
--no-default-features fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarow committed Mar 8, 2024
1 parent 7d6ea86 commit 2fb5d17
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions rust/fastsim-core/src/imports.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) use anyhow::{anyhow, bail, ensure, Context};
#[cfg(feature = "bincode")]
pub(crate) use bincode;
#[cfg(feature = "logging")]
pub(crate) use log;
pub(crate) use ndarray::{array, s, Array, Array1, Axis};
pub(crate) use serde::{Deserialize, Serialize};
Expand Down
1 change: 1 addition & 0 deletions rust/fastsim-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub use fastsim_proc_macros as proc_macros;
#[cfg_attr(feature = "pyo3", pyo3imports::pyfunction)]
#[allow(clippy::vec_init_then_push)]
pub fn enabled_features() -> Vec<String> {
#[allow(unused_mut)]
let mut enabled = vec![];

#[cfg(feature = "default")]
Expand Down
2 changes: 2 additions & 0 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
/// 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.
#[cfg(feature = "default")]
fn to_cache<P: AsRef<Path>>(&self, file_path: P) -> anyhow::Result<()> {
let file_name = file_path
.as_ref()
Expand Down Expand Up @@ -266,6 +267,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
/// 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.
#[cfg(feature = "default")]
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);
Expand Down
13 changes: 9 additions & 4 deletions rust/fastsim-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use itertools::Itertools;
use lazy_static::lazy_static;
use ndarray::*;
use regex::Regex;
use std::{collections::HashSet, io::Write};
use url::Url;
use std::collections::HashSet;
#[cfg(feature = "default")]
use std::io::Write;
#[cfg(feature = "default")]
use curl::easy::Easy;

Expand Down Expand Up @@ -519,6 +520,7 @@ pub fn tire_code_to_radius<S: AsRef<str>>(tire_code: S) -> anyhow::Result<f64> {
}

/// Assumes the parent directory exists. Assumes file doesn't exist (i.e., newly created) or that it will be truncated if it does.
#[cfg(feature = "default")]
pub fn download_file_from_url(url: &str, file_path: &Path) -> anyhow::Result<()> {
let mut handle = Easy::new();
handle.follow_location(true)?;
Expand Down Expand Up @@ -555,8 +557,8 @@ pub fn download_file_from_url(url: &str, file_path: &Path) -> anyhow::Result<()>
Ok(())
}

#[cfg(feature = "default")]
/// Creates/gets an OS-specific data directory and returns the path.
#[cfg(feature = "default")]
pub fn create_project_subdir<P: AsRef<Path>>(subpath: P) -> anyhow::Result<PathBuf> {
let proj_dirs = ProjectDirs::from("gov", "NREL", "fastsim").ok_or_else(|| {
anyhow!("Could not build path to project directory: \"gov.NREL.fastsim\"")
Expand All @@ -567,6 +569,7 @@ pub fn create_project_subdir<P: AsRef<Path>>(subpath: P) -> anyhow::Result<PathB
}

/// Returns the path to the OS-specific data directory, if it exists.
#[cfg(feature = "default")]
pub fn path_to_cache() -> anyhow::Result<PathBuf> {
let proj_dirs = ProjectDirs::from("gov", "NREL", "fastsim").ok_or_else(|| {
anyhow!("Could not build path to project directory: \"gov.NREL.fastsim\"")
Expand All @@ -588,6 +591,7 @@ pub fn path_to_cache() -> anyhow::Result<PathBuf> {
/// directories. If a single file needs deleting, the path_to_cache() function
/// can be used to find the FASTSim data directory location. The file can then
/// be found and manually deleted.
#[cfg(feature = "default")]
pub fn clear_cache<P: AsRef<Path>>(subpath: P) -> anyhow::Result<()> {
let path = path_to_cache()?.join(subpath);
Ok(std::fs::remove_dir_all(path)?)
Expand All @@ -606,8 +610,9 @@ pub fn clear_cache<P: AsRef<Path>>(subpath: P) -> anyhow::Result<()> {
/// "rust_objects" for other Rust objects.
/// Note: In order for the file to be save in the proper format, the URL needs
/// to be a URL pointing directly to a file, for example a raw github URL.
#[cfg(feature = "default")]
pub fn url_to_cache<S: AsRef<str>, P: AsRef<Path>>(url: S, subpath: P) -> anyhow::Result<()> {
let url = Url::parse(url.as_ref())?;
let url = url::Url::parse(url.as_ref())?;
let file_name = url
.path_segments()
.and_then(|segments| segments.last())
Expand Down
8 changes: 6 additions & 2 deletions rust/fastsim-core/src/vehicle_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
use argmin::core::{CostFunction, Executor, State};
#[cfg(feature = "default")]
use argmin::solver::neldermead::NelderMead;
use ndarray::{array, Array1};
use std::{result::Result, thread, time::Duration};
use ureq::{Error as OtherError, Error::Status, Response};

#[cfg(feature = "default")]
use crate::air::*;
#[cfg(feature = "default")]
use crate::cycle::RustCycle;
use crate::imports::*;
#[cfg(feature = "default")]
use crate::params::*;
#[cfg(feature = "pyo3")]
#[cfg(all(feature = "pyo3", feature = "default"))]
use crate::pyo3imports::*;
#[cfg(feature = "default")]
use crate::simdrive::RustSimDrive;
#[cfg(feature = "default")]
use crate::vehicle::RustVehicle;

#[allow(non_snake_case)]
Expand Down
2 changes: 1 addition & 1 deletion rust/fastsim-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn fastsimrust(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<vehicle_thermal::VehicleThermal>()?;
m.add_class::<thermal::ThermalState>()?;
m.add_class::<vehicle_thermal::HVACModel>()?;
m.add_class::<vehicle_import::OtherVehicleInputs>()?;

cycle::register(py, m)?;

Expand All @@ -50,6 +49,7 @@ fn fastsimrust(py: Python, m: &PyModule) -> PyResult<()> {
}
#[cfg(feature = "vehicle-import")]
{
m.add_class::<vehicle_import::OtherVehicleInputs>()?;
m.add_function(wrap_pyfunction!(
vehicle_import::get_options_for_year_make_model,
m
Expand Down

0 comments on commit 2fb5d17

Please sign in to comment.