Skip to content

Clean up imports, fix doc lints, get doctests to pass #59

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

Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
rust: [stable]
FEATURES: ['', 'from_str', 'std']
FEATURES: ['', 'from_str', 'std', 'serde']

include:
# Test nightly but don't fail
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.52.1 # clippy is too much of a moving target at the moment
toolchain: 1.76 # clippy is too much of a moving target at the moment
override: true
components: clippy
- uses: actions-rs/clippy-check@v1
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ keywords = ["measurements", "lengths", "weights", "temperatures", "volumes"]
description = "Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight, Volume, Pressure"
license = "MIT"
readme = "README.md"
edition = "2018"

[features]
std = []
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ measurements = "0.11"
In your code...

```rust
extern crate measurements;

use measurements::{Length, Pressure, Temperature, Volume, Mass};

fn main() {
Expand Down
1 change: 0 additions & 1 deletion examples/engine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate measurements;
use measurements::{AngularVelocity, Power};

fn main() {
Expand Down
1 change: 0 additions & 1 deletion examples/format_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate measurements;
use measurements::*;

fn main() {
Expand Down
2 changes: 0 additions & 2 deletions examples/frequency.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate measurements;

fn main() {
// Sinusiodal Oscilator moves at 5 Hz across 50 mm
let f = measurements::Frequency::from_hertz(5.0);
Expand Down
15 changes: 7 additions & 8 deletions src/acceleration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Types and constants for handling acceleration.

use super::length;
use super::measurement::*;
use crate::{length, measurement::*};
#[cfg(feature = "from_str")]
use regex::Regex;
#[cfg(feature = "from_str")]
Expand All @@ -13,7 +12,6 @@ use std::str::FromStr;
/// # Example
///
/// ```
/// extern crate measurements;
/// use measurements::{Acceleration, Length, Speed};
/// use std::time::Duration;
///
Expand All @@ -26,7 +24,7 @@ use std::str::FromStr;
/// println!("You accelerated over {} at an average of {}", track, accel);
///}
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Acceleration {
meters_per_second_per_second: f64,
Expand Down Expand Up @@ -120,15 +118,16 @@ implement_measurement! { Acceleration }
#[cfg(test)]
mod test {

use super::*;
use speed::Speed;
use test_utils::assert_almost_eq;
use crate::{speed::Speed, test_utils::assert_almost_eq, *};

#[cfg(feature = "from_str")]
use std::str::FromStr;

// Metric
#[test]
fn speed_over_time() {
let s1 = Speed::from_meters_per_second(10.0);
let t1 = ::time::Duration::new(5, 0);
let t1 = crate::time::Duration::new(5, 0);
let i1 = s1 / t1;
let r1 = i1.as_meters_per_second_per_second();
assert_almost_eq(r1, 2.0);
Expand Down
10 changes: 4 additions & 6 deletions src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::str::FromStr;
/// let slice = whole_cake / pieces;
/// println!("Each slice will be {} degrees", slice.as_degrees());
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Angle {
radians: f64,
Expand All @@ -28,7 +28,7 @@ pub struct Angle {
impl Angle {
/// Create a new Angle from a floating point value in degrees
pub fn from_degrees(degrees: f64) -> Self {
Angle::from_radians(degrees * ::PI / 180.0)
Angle::from_radians(degrees * crate::PI / 180.0)
}

/// Create a new Angle from a floating point value in radians
Expand All @@ -38,7 +38,7 @@ impl Angle {

/// Convert this Angle to a floating point value in degrees
pub fn as_degrees(&self) -> f64 {
self.radians * 180.0 / ::PI
self.radians * 180.0 / crate::PI
}

/// Convert this Angle to a floating point value in radians
Expand Down Expand Up @@ -134,9 +134,7 @@ implement_measurement! { Angle }

#[cfg(test)]
mod test {
use angle::*;
use std::f64::consts::PI;
use test_utils::assert_almost_eq;
use crate::{angle::*, test_utils::assert_almost_eq, PI};

#[test]
fn radians() {
Expand Down
6 changes: 3 additions & 3 deletions src/angular_velocity.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Types and constants for handling speed of rotation (angular velocity)

use super::measurement::*;
use crate::PI;
#[cfg(feature = "from_str")]
use regex::Regex;
#[cfg(feature = "from_str")]
use std::str::FromStr;
use PI;

/// The 'AngularVelocity' struct can be used to deal with angular velocities in a common way.
///
Expand All @@ -18,7 +18,7 @@ use PI;
/// let engine_speed = AngularVelocity::from_rpm(9000.0);
/// let sparks_per_second = (engine_speed.as_hertz() / 2.0) * cylinders;
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct AngularVelocity {
radians_per_second: f64,
Expand Down Expand Up @@ -106,7 +106,7 @@ implement_measurement! { AngularVelocity }
#[cfg(test)]
mod test {
use super::*;
use test_utils::assert_almost_eq;
use crate::test_utils::assert_almost_eq;

#[test]
fn rpm() {
Expand Down
7 changes: 3 additions & 4 deletions src/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const SQUARE_METER_ACRE_FACTOR: f64 = 1.0 / 4046.86;
/// let acres = football_field.as_acres();
/// println!("There are {} acres in a football field.", acres);
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Area {
square_meters: f64,
Expand Down Expand Up @@ -318,7 +318,7 @@ impl FromStr for Area {

let re = Regex::new(r"(?i)\s*([0-9.]*)\s?([a-z2\u{00B2}\u{00B5} ]{1,5})\s*$").unwrap();
if let Some(caps) = re.captures(val) {
println!("{:?}", caps);
println!("{caps:?}");
let float_val = caps.get(1).unwrap().as_str();
return Ok(
match caps.get(2).unwrap().as_str().trim().to_lowercase().as_str() {
Expand Down Expand Up @@ -359,8 +359,7 @@ implement_measurement! { Area }

#[cfg(test)]
mod test {
use area::*;
use test_utils::assert_almost_eq;
use crate::{area::*, test_utils::assert_almost_eq};

#[test]
fn square_meters() {
Expand Down
5 changes: 2 additions & 3 deletions src/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::measurement::*;
/// let u_a = amperes.as_microamperes();
/// println!("35 mA correspond to {} A or {} µA", a, u_a);
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Current {
amperes: f64,
Expand Down Expand Up @@ -100,8 +100,7 @@ implement_measurement! { Current }

#[cfg(test)]
mod test {
use current::*;
use test_utils::assert_almost_eq;
use crate::{current::*, test_utils::assert_almost_eq};

#[test]
pub fn as_amperes() {
Expand Down
5 changes: 2 additions & 3 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const OCTET_TEBIOCTET_FACTOR: f64 = 1024.0 * 1024.0 * 1024.0 * 1024.0;
/// let octets = file_size.as_octets();
/// println!("There are {} octets in that file.", octets);
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Data {
octets: f64,
Expand Down Expand Up @@ -170,8 +170,7 @@ implement_measurement! { Data }

#[cfg(test)]
mod test {
use data::*;
use test_utils::assert_almost_eq;
use crate::{data::*, test_utils::assert_almost_eq};

// Metric
#[test]
Expand Down
48 changes: 20 additions & 28 deletions src/density.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Types and constants for handling density.

use super::measurement::*;
use mass::Mass;
use volume::Volume;
use crate::{mass::Mass, volume::Volume};

// Constants, metric
/// Number of pound per cubic foot in 1 kilograms per cubic meter
Expand All @@ -14,36 +13,29 @@ pub const LBCF_KGCM_FACTOR: f64 = 0.062427973725314;
/// # Example1 - calculating volume from units of mass and density
///
/// ```
/// extern crate measurements;
/// use measurements::{Density, Mass, Volume};
///
/// fn main() {
/// // Q: A 12 stone man hops into a brimming full bath, completely emersing himself.
/// // How many gallons of water spill on the floor?
/// // (Assume The human body is roughly about as dense as water - 1 gm/cm³)
/// //
/// let body_density: Density = Mass::from_grams(1.0) / Volume:: from_cubic_centimetres(1.0);
/// let mans_weight = Mass::from_stones(12.0);
/// let water_volume = mans_weight / body_density;
/// println!("{} gallons of water spilled on the floor", water_volume.as_gallons());
///}
/// // Q: A 12 stone man hops into a brimming full bath, completely emersing himself.
/// // How many gallons of water spill on the floor?
/// // (Assume The human body is roughly about as dense as water - 1 gm/cm³)
/// //
/// let body_density: Density = Mass::from_grams(1.0) / Volume:: from_cubic_centimetres(1.0);
/// let mans_weight = Mass::from_stones(12.0);
/// let water_volume = mans_weight / body_density;
/// println!("{} gallons of water spilled on the floor", water_volume.as_gallons());
/// ```
/// # Example2 - converting to ad-hoc units of density
///
/// ```
/// extern crate measurements;
/// use measurements::{Density, Mass, Volume};
///
/// fn main() {
/// // Q: what is 3 grams per litre in units of ounces per quart?
/// //
/// let density: Density = Mass::from_grams(3.0) / Volume:: from_litres(1.0);
/// let ounces = (density * Volume::from_quarts(1.0)).as_ounces();
/// println!("Answer is {} ounces per quart", ounces);
///}
/// // Q: what is 3 grams per litre in units of ounces per quart?
/// //
/// let density: Density = Mass::from_grams(3.0) / Volume:: from_litres(1.0);
/// let ounces = (density * Volume::from_quarts(1.0)).as_ounces();
/// println!("Answer is {} ounces per quart", ounces);
/// ```

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Density {
kilograms_per_cubic_meter: f64,
Expand Down Expand Up @@ -74,7 +66,7 @@ impl Density {
}

// mass / volume = density
impl ::std::ops::Div<Volume> for Mass {
impl ::core::ops::Div<Volume> for Mass {
type Output = Density;

fn div(self, other: Volume) -> Density {
Expand All @@ -83,7 +75,7 @@ impl ::std::ops::Div<Volume> for Mass {
}

// mass / density = volume
impl ::std::ops::Div<Density> for Mass {
impl ::core::ops::Div<Density> for Mass {
type Output = Volume;

fn div(self, other: Density) -> Volume {
Expand All @@ -92,7 +84,7 @@ impl ::std::ops::Div<Density> for Mass {
}

// volume * density = mass
impl ::std::ops::Mul<Density> for Volume {
impl ::core::ops::Mul<Density> for Volume {
type Output = Mass;

fn mul(self, other: Density) -> Mass {
Expand All @@ -101,7 +93,7 @@ impl ::std::ops::Mul<Density> for Volume {
}

// density * volume = mass
impl ::std::ops::Mul<Volume> for Density {
impl ::core::ops::Mul<Volume> for Density {
type Output = Mass;

fn mul(self, other: Volume) -> Mass {
Expand Down Expand Up @@ -129,7 +121,7 @@ implement_measurement! { Density }
mod test {

use super::*;
use test_utils::assert_almost_eq;
use crate::test_utils::assert_almost_eq;

// Metric
#[test]
Expand Down
5 changes: 2 additions & 3 deletions src/energy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::measurement::*;
/// let energy = Energy::from_kcalories(2500.0);
/// println!("Some say a health adult male should consume {} per day", energy);
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Energy {
joules: f64,
Expand Down Expand Up @@ -118,8 +118,7 @@ implement_measurement! { Energy }

#[cfg(test)]
mod test {
use energy::*;
use test_utils::assert_almost_eq;
use crate::{energy::*, test_utils::assert_almost_eq};

#[test]
pub fn as_kcalories() {
Expand Down
5 changes: 2 additions & 3 deletions src/force.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const DYNES_PER_NEWTON: f64 = 1e5;
/// "One metric ton exerts a force of {} due to gravity",
/// force);
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Default)]
pub struct Force {
newtons: f64,
Expand Down Expand Up @@ -139,8 +139,7 @@ implement_measurement! { Force }

#[cfg(test)]
mod test {
use force::*;
use test_utils::assert_almost_eq;
use crate::{force::*, test_utils::assert_almost_eq};

#[test]
pub fn newtons() {
Expand Down
Loading
Loading