Skip to content

Commit

Permalink
refactor: avoid deep clone
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Jun 18, 2024
1 parent 71d8afe commit 9de927a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clap::{Parser, ValueEnum};
about = "Bilal [A CLI salah time]",
after_long_help = "Bugs can be reported on GitHub: https://github.com/azzamsa/bilal/issues"
)]
#[derive(Debug)]
pub struct Opts {
/// A Salah mode to show
#[arg(value_enum)]
Expand All @@ -26,14 +27,14 @@ pub struct Opts {
pub color: Color,
}

#[derive(Clone, ValueEnum)]
#[derive(Debug, Clone, ValueEnum)]
pub enum Mode {
All,
Next,
Current,
}

#[derive(Clone, ValueEnum)]
#[derive(Debug, Clone, ValueEnum)]
pub enum Color {
/// show colors if the output goes to an interactive console (default)
Auto,
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;

use crate::error::Error;

#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct Config {
pub latitude: f32,
pub longitude: f32,
Expand All @@ -19,7 +19,7 @@ pub struct Config {
pub time_format: TimeFormat,
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[derive(Debug, PartialEq, Deserialize)]
pub enum TimeFormat {
#[serde(rename = "24H")]
H24,
Expand Down
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::process;
use std::{process, sync::Arc};

use clap::Parser;
use miette::Result;
Expand All @@ -11,7 +11,8 @@ use bilal::{
};

fn run() -> Result<()> {
let opts = Opts::parse();
let opts = Arc::new(Opts::parse());

match opts.color {
Color::Always => {
owo_colors::set_override(true);
Expand All @@ -29,9 +30,9 @@ fn run() -> Result<()> {
owo_colors::set_override(false);
}

let config = config::read()?;
let prayers = prayer::all(config.clone())?;
let printer = Printer::new(prayers, opts.json, config);
let config = Arc::new(config::read()?);
let prayers = prayer::all(Arc::clone(&config))?;
let printer = Printer::new(prayers, Arc::clone(&opts), config);

match opts.mode {
Mode::All => {
Expand Down
18 changes: 11 additions & 7 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
use std::io::{self, Write};
use std::{
io::{self, Write},
sync::Arc,
};

use owo_colors::{OwoColorize, Stream::Stdout};

use islam::salah::PrayerTimes;

use crate::{
cli::Opts,
config::{Config, TimeFormat},
DateTime,
};

#[derive(Debug)]
pub struct Printer {
prayers: PrayerTimes,
json_format: bool,
config: Config,
opts: Arc<Opts>,
config: Arc<Config>,
}

impl Printer {
pub const fn new(prayers: PrayerTimes, json_format: bool, config: Config) -> Self {
pub const fn new(prayers: PrayerTimes, opts: Arc<Opts>, config: Arc<Config>) -> Self {
Self {
prayers,
json_format,
opts,
config,
}
}
Expand Down Expand Up @@ -75,7 +79,7 @@ impl Printer {
};

// JSON
if self.json_format {
if self.opts.json {
prayer_fmt = format!(
r#"{{"icon": "{}", "state": "{}", "text": "{} {}"}}"#,
"bilal", state, "\u{23fa} ", prayer_fmt
Expand Down Expand Up @@ -105,7 +109,7 @@ impl Printer {

// JSON
let state = "Info";
if self.json_format {
if self.opts.json {
prayer_fmt = format!(
r#"{{"icon": "{}", "state": "{}", "text": "{} {}"}}"#,
"bilal", state, "\u{25b6}", prayer_fmt
Expand Down
4 changes: 3 additions & 1 deletion src/prayer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::sync::Arc;

use islam::salah::{Config as SalahConfig, Location, Madhab, Method, PrayerSchedule, PrayerTimes};

use crate::{config::Config, error::Error};

/// Returns all prayers
pub fn all(config: Config) -> Result<PrayerTimes, Error> {
pub fn all(config: Arc<Config>) -> Result<PrayerTimes, Error> {
let latitude = config.latitude;
let longitude = config.longitude;
let method = method(&config.method)?;
Expand Down

0 comments on commit 9de927a

Please sign in to comment.