Skip to content

Commit

Permalink
Adding value() method to the Qiblah sturct
Browse files Browse the repository at this point in the history
The Qiblah struct now implements the Debug and Display traits that can be used for print out the value of the Qiblah. Updating the test to use the public interface and adding a new test to assert the display value of the Qiblah direction.
  • Loading branch information
insha committed Oct 14, 2023
1 parent f88e3ee commit 03e2d62
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "salah"
version = "0.7.0"
version = "0.7.1"
authors = ["Farhan Ahmed <[email protected]>"]
edition = "2018"
description = "Islamic prayer time library for Rust"
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Add the following to your `Cargo.toml` file under the `[dependencies]` section:

```
[dependencies]
salah = "0.7.0"
salah = "0.7.1"
```

To get prayer times, use the `PrayerSchedule` struct passing in coordinates, date, and calculation parameters.
Expand All @@ -25,7 +25,7 @@ use salah::prelude::*;

let new_york_city = Coordinates::new(40.7128, -74.0059);
let date = Utc.ymd(2019, 1, 25);
let params = Configuration::with(Method::NorthAmerica, Madhab::Hanafi);
let params = Configuration::with(Method::MoonsightingCommittee, Madhab::Hanafi);
let prayers = PrayerSchedule::new()
.on(date)
.for_location(new_york_city)
Expand Down Expand Up @@ -219,9 +219,18 @@ Get the direction, in degrees from North, of the Qibla from a given set of coord

```rust
let new_york_city = Coordinates::new(40.7128, -74.0059);
let qibla_direction = Qibla::new(coordinates: new_york_city);
let qiblah_direction = Qiblah::new(new_york_city);

println!("Qiblah: {}", qibla_direction); // Outputs: Qiblah: 58.4817
```

TO access that actual numerical (f64) value of the qiblah direction, you can use the `value()` method on the `Qiblah` instance.

```rust
let new_york_city = Coordinates::new(40.7128, -74.0059);
let qiblah_direction = Qiblah::new(new_york_city);

println!("Qiblah: {:?}", qibla_direction); // Outputs: Qiblah: 58.4817
println!("Qiblah: {}", qibla_direction.value()); // Outputs: Qiblah: 58.48176358718943
```

## Contributing
Expand Down
47 changes: 35 additions & 12 deletions src/astronomy/qiblah.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

use std::fmt;

use crate::astronomy::unit::{Angle, Coordinates};

#[derive(Debug)]
pub struct Qiblah(f64);

impl Qiblah {
Expand All @@ -26,8 +29,19 @@ impl Qiblah {

Qiblah(Angle::from_radians(term4).unwound().degrees)
}

pub fn value(&self) -> f64 {
self.0
}
}

impl fmt::Display for Qiblah {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value())
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -38,94 +52,103 @@ mod tests {
let nyc = Coordinates::new(40.7128, -74.0059);
let qiblah = Qiblah::new(nyc);

assert_that!(qiblah.0).is_close_to(58.4817635, 0.0000001f64);
assert_that!(qiblah.value()).is_close_to(58.4817635, 0.0000001f64);
}

#[test]
fn qiblah_direction_from_sf_in_north_america() {
let sf = Coordinates::new(37.7749, -122.4194);
let qiblah = Qiblah::new(sf);

assert_eq!(qiblah.0, 18.843822245692426);
assert_eq!(qiblah.value(), 18.843822245692426);
}

#[test]
fn qiblah_direction_from_dc_in_north_america() {
let dc = Coordinates::new(38.9072, -77.0369);
let qiblah = Qiblah::new(dc);

assert_eq!(qiblah.0, 56.56046821463599);
assert_eq!(qiblah.value(), 56.56046821463599);
}

#[test]
fn qiblah_direction_from_anchorage_in_north_america() {
let dc = Coordinates::new(61.2181, -149.9003);
let qiblah = Qiblah::new(dc);

assert_eq!(qiblah.0, 350.8830761159853);
assert_eq!(qiblah.value(), 350.8830761159853);
}

#[test]
fn qiblah_directioon_from_sydney_australia() {
let sydney = Coordinates::new(-33.8688, 151.2093);
let qiblah = Qiblah::new(sydney);

assert_eq!(qiblah.0, 277.4996044487399);
assert_eq!(qiblah.value(), 277.4996044487399);
}

#[test]
fn qiblah_directioon_from_auckland_new_zealand() {
let auckland = Coordinates::new(-36.8485, 174.7633);
let qiblah = Qiblah::new(auckland);

assert_eq!(qiblah.0, 261.19732640365845);
assert_eq!(qiblah.value(), 261.19732640365845);
}

#[test]
fn qiblah_direction_from_london_united_kingdom() {
let london = Coordinates::new(51.5074, -0.1278);
let qiblah = Qiblah::new(london);

assert_that!(qiblah.0).is_close_to(118.9872189, 0.0000001f64);
assert_that!(qiblah.value()).is_close_to(118.9872189, 0.0000001f64);
}

#[test]
fn qiblah_direction_from_paris_france() {
let paris = Coordinates::new(48.8566, 2.3522);
let qiblah = Qiblah::new(paris);

assert_eq!(qiblah.0, 119.16313542183347);
assert_eq!(qiblah.value(), 119.16313542183347);
}

#[test]
fn qiblah_direction_from_oslo_norway() {
let oslo = Coordinates::new(59.9139, 10.7522);
let qiblah = Qiblah::new(oslo);

assert_eq!(qiblah.0, 139.02785605537514);
assert_eq!(qiblah.value(), 139.02785605537514);
}

#[test]
fn qiblah_direction_from_islamabad_pakistan() {
let islamabad = Coordinates::new(33.7294, 73.0931);
let qiblah = Qiblah::new(islamabad);

assert_eq!(qiblah.0, 255.8816156785436);
assert_eq!(qiblah.value(), 255.8816156785436);
}

#[test]
fn qiblah_direction_from_tokyo_japan() {
let tokyo = Coordinates::new(35.6895, 139.6917);
let qiblah = Qiblah::new(tokyo);

assert_eq!(qiblah.0, 293.02072441441163);
assert_eq!(qiblah.value(), 293.02072441441163);
}

#[test]
fn qiblah_direction_from_jakarta_indonesia() {
let jakarta = Coordinates::new(-6.18233995, 106.84287154);
let qiblah = Qiblah::new(jakarta);

assert_that!(qiblah.0).is_close_to(295.1442983825265, 0.0000001f64);
assert_that!(qiblah.value()).is_close_to(295.1442983825265, 0.0000001f64);
}

#[test]
fn qiblah_direction_display() {
let nyc = Coordinates::new(40.7128, -74.0059);
let qiblah = Qiblah::new(nyc);
let actual_value = qiblah.to_string();

assert_eq!(actual_value, "58.48176358718943");
}
}

0 comments on commit 03e2d62

Please sign in to comment.