Skip to content

Use features to make some dependencies optional #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,24 @@ thiserror = "2.0"

[dependencies]
log = { workspace = true }
mapproj = { workspace = true } # Projection to wiew skymaps/MOMs
memmap2 = { workspace = true } # Skymaps/MOMs
mapproj = { workspace = true, optional = true } # Projection to wiew skymaps/MOMs
memmap2 = { workspace = true, optional = true } # Skymaps/MOMs
rayon = { workspace = true } # Parallelism (sort)
thiserror = { workspace = true } # Error handling
katex-doc = "0.1.0"
byteorder = "1.5"
# MOC specific
base64 = "0.22" # Added for Compressed MOC
num = "0.4" # Added for MOC
base64 = { version = "0.22", optional = true } # Added for Compressed MOC
num-traits = "0.2" # Added for MOC
# Skymaps
byteorder = "1.5"
colorous = "1.0"
png = "0.17"
colorous = { version = "1.0", optional = true }
png = { version = "0.17", optional = true }
# Compression/decompression
flate2 = "1.0"
# Serialization/Deserialization
serde = { version = "1.0", features = ["derive"] } # MOM serialization/deserialization
bincode = "1.3.3"
toml = "0.8"
serde = { version = "1.0", features = ["derive"], optional = true } # MOM serialization/deserialization
bincode = { version = "1.3.3", optional = true }
toml = { version = "0.8", optional = true }
# MOM specific
itertools = "0.14"
# For date in FITS written files
Expand All @@ -84,6 +83,13 @@ name = "zordercurve"
harness = false

[features]
default = ["base64", "memmap", "serde", "skymap", "sort"]

base64 = ["dep:base64"]
memmap = ["dep:memmap2"]
skymap = ["dep:colorous", "dep:mapproj", "dep:png"]
serde = ["dep:serde"]
sort = ["dep:bincode", "dep:toml", "memmap", "serde"]

# See opti here: https://github.com/johnthagen/min-sized-rust
[profile.release]
Expand Down
7 changes: 7 additions & 0 deletions src/nested/bmoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
vec::IntoIter,
};

#[cfg(feature = "base64")]
use base64::{engine::general_purpose::STANDARD, DecodeError, Engine};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use chrono::{DateTime, SecondsFormat, Utc};
Expand Down Expand Up @@ -2247,10 +2248,12 @@ impl CompressedMOC {
self.moc.len()
}

#[cfg(feature = "base64")]
pub fn to_b64(&self) -> String {
STANDARD.encode(&self.moc)
}

#[cfg(feature = "base64")]
pub fn from_b64(b64_encoded: String) -> Result<CompressedMOC, DecodeError> {
let decoded = STANDARD.decode(b64_encoded)?;
let depth_max = decoded[0];
Expand Down Expand Up @@ -2333,6 +2336,7 @@ mod tests {
}
}

#[cfg(feature = "base64")]
#[test]
fn testok_compressed_moc_empty_d0() {
let compressed = build_compressed_moc_empty(0);
Expand All @@ -2348,6 +2352,7 @@ mod tests {
);
}

#[cfg(feature = "base64")]
#[test]
fn testok_compressed_moc_empty_d1() {
let compressed = build_compressed_moc_empty(1);
Expand All @@ -2366,6 +2371,7 @@ mod tests {
);
}

#[cfg(feature = "base64")]
#[test]
fn testok_compressed_moc_full_d0() {
let compressed = build_compressed_moc_full(0);
Expand All @@ -2382,6 +2388,7 @@ mod tests {
);
}

#[cfg(feature = "base64")]
#[test]
fn testok_compressed_moc_full_d1() {
let compressed = build_compressed_moc_full(1);
Expand Down
3 changes: 2 additions & 1 deletion src/nested/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use num_traits::{AsPrimitive, PrimInt};

pub mod astrometry;
pub mod fits;
#[cfg(feature = "skymap")]
pub mod img;
pub mod mom;
pub mod skymap;
Expand Down Expand Up @@ -41,7 +42,7 @@ impl HHash for u64 {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "skymap"))]
mod tests {
use super::{
img::{ColorMapFunctionType, PosConversion},
Expand Down
43 changes: 26 additions & 17 deletions src/nested/map/mom/impls/bslice.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
use super::super::{
super::{
fits::{
error::FitsError,
read::{
check_keyword_and_parse_uint_val, check_keyword_and_str_val, check_keyword_and_val,
get_str_val_no_quote, next_36_chunks_of_80_bytes, parse_uint_val,
},
},
skymap::{SkyMap, SkyMapValue},
use crate::nested::map::{
skymap::{SkyMap, SkyMapValue},
mom::{LhsRhsBoth, Mom, ZUniqHashT},
};
#[cfg(feature = "memmap")]
use crate::nested::map::fits::{
error::FitsError,
read::{
check_keyword_and_parse_uint_val, check_keyword_and_str_val, check_keyword_and_val,
get_str_val_no_quote, next_36_chunks_of_80_bytes, parse_uint_val,
},
LhsRhsBoth, Mom, ZUniqHashT,
};
#[cfg(feature = "memmap")]
use crate::nested::map::mom::WritableMom;
#[cfg(feature = "memmap")]
use chrono::{DateTime, Utc};
#[cfg(feature = "memmap")]
use log::debug;
#[cfg(feature = "memmap")]
use memmap2::{Mmap, MmapOptions};
use num_traits::{FromBytes, ToBytes};
use std::io::{BufWriter, Write};
#[cfg(feature = "memmap")]
use std::{
fs::File,
io::{BufWriter, Write},
path::Path,
str,
time::SystemTime,
};
use std::{
array::TryFromSliceError,
cmp::Ordering,
convert::{TryFrom, TryInto},
fs::File,
iter::{Empty, Map},
marker::PhantomData,
ops::Range,
path::Path,
slice::{from_raw_parts, ChunksExact},
str,
time::SystemTime,
};

/// Defines the type of ZUniq Hash values that can be read/write from/to FITS files.
Expand Down Expand Up @@ -61,6 +66,7 @@ where
{
}

#[cfg(feature = "memmap")]
pub enum FITSMom {
U32U32(FitsMMappedCIndex<u32, u32>),
U32F32(FitsMMappedCIndex<u32, f32>),
Expand All @@ -69,6 +75,7 @@ pub enum FITSMom {
U64F32(FitsMMappedCIndex<u64, f32>),
U64F64(FitsMMappedCIndex<u64, f64>),
}
#[cfg(feature = "memmap")]
impl FITSMom {
// TODO: make a method loading everything from a reader a aking a zvec object!
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -227,6 +234,7 @@ impl FITSMom {
}

#[derive(Debug)]
#[cfg(feature = "memmap")]
pub struct FitsMMappedCIndex<Z: Z4FITS, V: V4FITS> {
fits_creation_date: Option<SystemTime>,
value_name: String,
Expand All @@ -237,6 +245,7 @@ pub struct FitsMMappedCIndex<Z: Z4FITS, V: V4FITS> {
_phantom_v: PhantomData<V>,
}

#[cfg(feature = "memmap")]
impl<Z: Z4FITS, V: V4FITS> FitsMMappedCIndex<Z, V> {
fn new(
fits_creation_date: Option<SystemTime>,
Expand Down
2 changes: 1 addition & 1 deletion src/nested/map/mom/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod bslice;
pub mod ported_from_mom_builder;
pub mod zvec;

#[cfg(test)]
#[cfg(all(test, feature = "skymap", feature = "memmap"))]
mod tests {
use std::f64::consts::PI;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::merge_is_valid::MergeIsValid;
use super::merge_states::MergeStates;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use num_traits::Float;

/// Leaf state with minimum, maximum and mean values.
///
/// It implements [Into] trait for [f32] and [f64], so it can be converted to its mean value.
/// [From] trait for `T` which just assigns the given value to all three fields.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MinMaxMeanState<T> {
/// Minimum value.
pub min: T,
Expand Down Expand Up @@ -54,7 +56,8 @@ where
}

/// Merges leaf states by taking minimum and maximum of the states and calculating the mean value.
#[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Merger<V> {
pub validator: V,
}
Expand Down Expand Up @@ -106,7 +109,8 @@ where
/// Checks if the relative difference between minimum and maximum is less than a given threshold.
///
/// Basically it checks if `abs(max - min) / max <= threshold`, but with some edge-case handling.
#[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RelativeToleranceValidator<T> {
threshold: T,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::nested::map::mom::impls::ported_from_mom_builder::state::merge_states::MergeStates;
use super::value_state::ValueState;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

/// Merges leaf states if values are exactly equal.
#[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ExactlyEqualMerger<T> {
phantom: PhantomData<T>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::nested::map::mom::impls::ported_from_mom_builder::state::merge_is_val
use crate::nested::map::mom::impls::ported_from_mom_builder::state::merge_states::MergeStates;
use super::ValueState;
use num_traits::Zero;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Sum values until merge state is valid.
#[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SumUntilMerger<V> {
pub validator: V,
}
Expand Down Expand Up @@ -47,7 +49,8 @@ where
}

/// Validator for maximum value, which checks if the merge state is less than or equal to the maximum value.
#[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MaximumValueValidator<T>(pub T);

impl<T> MergeIsValid for MaximumValueValidator<T>
Expand All @@ -68,7 +71,8 @@ where
/// and 2) number of empty siblings is less than or equal to the maximum number of empty siblings.
/// Also, you can optionally allow merge if all children states are zero. We refer to the children
/// states with zero value as "empty siblings".
#[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MaximumValueEmptySiblingValidator<T> {
/// Maximum value of the merged state.
pub threshold: T,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Simple leaf state with a single value.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ValueState<T>(pub T);

macro_rules! impl_from_value_state {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! Tree configuration.

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Tree configuration.
///
/// Actually, it is a configuration of the forest of trees, but for simplicity we call it
/// a multi-root tree.
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TreeConfig {
/// Number of roots.
#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion src/nested/map/mom/impls/zvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ where
}
}

#[cfg(test)]
#[cfg(all(test, feature = "skymap"))]
mod tests {
use std::f64::consts::PI;

Expand Down
Loading