n4n5 1.6.4

n4n5's utility crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! To see all subcommands, run:
//! ```shell
//! n4n5 movies
//! ```
//!
use std::{collections::BTreeMap, fs::read_to_string, path::PathBuf, process::Command};

use clap::{ArgAction, Subcommand};
use serde::{Deserialize, Serialize};

use crate::{
    config::Config,
    config_path,
    errors::GeneralError,
    get_config_path,
    utils::{get_input, input_path},
};

/// Movies configuration
#[derive(Deserialize, Serialize, Default)]
pub struct Movies {
    /// Path to the movies file
    pub file_path: Option<String>,

    /// public path to the movies file
    pub public_file_path: Option<String>,
}

/// Display mode
pub enum DisplayMode {
    /// Short display
    Short,

    /// Display with comment
    Comment,

    /// Full display
    Full,
}

/// All movies data
pub struct AllMovies {
    /// List of movies
    pub movies: Vec<OneMovie>,
}

impl AllMovies {
    /// Display the movies
    pub fn display(&self, mode: &DisplayMode) {
        for movie in &self.movies {
            match mode {
                DisplayMode::Short => println!("{}", movie.display()),
                DisplayMode::Comment => println!("{}", movie.display_comment()),
                DisplayMode::Full => println!("{}\n", movie.display_full()),
            }
        }
    }
}

/// Movie data
#[derive(Deserialize, Serialize, Debug)]
pub struct OneMovie {
    /// Movie title
    pub title: String,

    /// Movie note
    pub note: f64,

    /// Movie publication date
    pub date: u64,

    /// Comment about the movie
    pub comment: String,

    /// Seen date
    pub seen: Option<String>,

    /// Summary of the movie
    pub summary: Option<String>,
}

impl OneMovie {
    /// Display the movie
    pub fn display(&self) -> String {
        format!("{} - {} ({}) ", self.note, self.title, self.date,)
    }

    /// Display the movie with comment
    pub fn display_comment(&self) -> String {
        format!(
            "{} - {} ({}) - {}",
            self.note, self.title, self.date, self.comment
        )
    }

    /// Display the full movie
    pub fn display_full(&self) -> String {
        format!(
            "{} - {} ({}) - {} - {}\n{}",
            self.note,
            self.title,
            self.date,
            self.seen.as_deref().unwrap_or(""),
            self.comment,
            self.summary.as_deref().unwrap_or("")
        )
    }
}

/// Movies sub command
#[derive(Subcommand, Debug, Clone)]
pub enum MoviesSubCommand {
    /// add a movie
    Add,
    /// open movie file
    Open {
        /// print path of movies file
        #[arg(short = 'p', long = "path", action = ArgAction::SetTrue)]
        show_path: bool,
    },
    /// Show stats of movies
    Stats {
        /// print stats as json
        #[arg(short ='j', long = "json", action = ArgAction::SetTrue)]
        print_json: bool,
    },
    /// Show movies list
    Show {
        /// reverse mode
        #[arg(short = 'r', long = "reverse", action = ArgAction::SetTrue)]
        reverse: bool,
        /// show full mode
        #[arg(short = 'f', long = "full", action = ArgAction::SetTrue)]
        show_full: bool,
        /// show comment
        #[arg(short = 'c', long = "comment", action = ArgAction::SetTrue)]
        show_comment: bool,
    },
    /// Sync movies file
    Sync {
        /// print as json
        #[arg(short ='j', long = "json", action = ArgAction::SetTrue)]
        print_json: bool,
    },
}

impl MoviesSubCommand {
    /// invoke subcommand
    /// # Errors
    /// Error if error in subcommand
    pub fn invoke(self, config: &mut Config) -> Result<(), GeneralError> {
        match self {
            Self::Add => Movies::add_movie(config),
            Self::Open { show_path } => Movies::open_movies(config, show_path),
            Self::Show {
                reverse,
                show_full,
                show_comment,
            } => Movies::print_sorted_movies(config, reverse, show_comment, show_full),
            Self::Stats { print_json } => Movies::print_stats(config, print_json),
            Self::Sync { print_json } => Movies::full_sync_movies(config, print_json),
        }
    }
}

impl Movies {
    /// Get the movie path
    /// # Errors
    /// Returns an error if unable to read the movies file
    pub fn get_movie_path(config: &Config) -> Result<PathBuf, GeneralError> {
        if let Some(movies_config) = &config.config_data.movies
            && let Some(movie_path) = &movies_config.file_path
        {
            return Ok(PathBuf::from(movie_path));
        }
        Err(GeneralError::new("movies path not set"))
    }

    /// Add a movie
    /// # Errors
    /// Returns an error if unable to read the movies file
    fn add_movie(config: &mut Config) -> Result<(), GeneralError> {
        let file_path = Movies::get_movie_path(config)?;
        let title = get_input("Title")?;
        let note = get_input("Note")?.parse()?;
        let date = get_input("Date")?.parse()?;
        let comment = get_input("Comment")?;
        let seen = get_input("Seen")?;
        let summary = get_input("Summary")?;
        let movie = OneMovie {
            title,
            note,
            date,
            comment,
            seen: Some(seen),
            summary: Some(summary),
        };
        let mut all_movies = Movies::get_all_movies(config)?;
        all_movies.movies.push(movie);
        let movies_file_to_str = serde_json::to_string_pretty(&all_movies.movies)?;
        std::fs::write(&file_path, movies_file_to_str)?;
        println!("Movie added to '{}'", file_path.display());
        Ok(())
    }

    /// Open movie file
    /// # Errors
    /// Returns an error if unable to open the movies file
    pub fn open_movies(config: &mut Config, show_path: bool) -> Result<(), GeneralError> {
        let file_path = Movies::get_movie_path(config)?;
        if show_path {
            println!("{}", file_path.display());
            return Ok(());
        }
        println!("Opening movies file at {}", file_path.display());
        Command::new("vi").arg(&file_path).spawn()?.wait()?;
        Ok(())
    }

    /// Get all movies
    /// # Errors
    /// Returns an error if unable to read the movies file
    pub fn get_all_movies(config: &Config) -> Result<AllMovies, GeneralError> {
        let file_path = Movies::get_movie_path(config)?;
        if config.debug > 0 {
            println!("Reading movies file at {}", file_path.display());
        }
        if !file_path.exists() {
            return Err(GeneralError::new(format!(
                "Movies file not found at '{}'",
                file_path.display()
            )));
        }
        if !file_path.is_file() {
            return Err(GeneralError::new(format!(
                "Movies file is not a file at '{}'",
                file_path.display()
            )));
        }
        let movies_file_to_str = read_to_string(&file_path)?;
        let all_movies: Vec<OneMovie> = serde_json::from_str(&movies_file_to_str)?;
        Ok(AllMovies { movies: all_movies })
    }

    /// Print the movies sorted by note
    /// # Errors
    /// Returns an error if unable to read the movies file
    fn print_sorted_movies(
        config: &mut Config,
        reverse: bool,
        show_comment: bool,
        show_full: bool,
    ) -> Result<(), GeneralError> {
        let mut all_movies = Movies::get_all_movies(config)?;
        all_movies.movies.sort_by(|a, b| {
            if reverse {
                b.note
                    .partial_cmp(&a.note)
                    .unwrap_or(std::cmp::Ordering::Equal)
            } else {
                a.note
                    .partial_cmp(&b.note)
                    .unwrap_or(std::cmp::Ordering::Equal)
            }
        });
        if show_full {
            all_movies.display(&DisplayMode::Full);
        } else if show_comment {
            all_movies.display(&DisplayMode::Comment);
        } else {
            all_movies.display(&DisplayMode::Short);
        }
        Ok(())
    }

    /// Group movies by date
    fn group_movies_by_date(movies: &AllMovies) -> std::collections::HashMap<u64, Vec<&OneMovie>> {
        movies
            .movies
            .iter()
            .fold(std::collections::HashMap::new(), |mut acc, movie| {
                let entry: &mut Vec<&OneMovie> = acc.entry(movie.date).or_default();
                entry.push(movie);
                acc
            })
    }

    /// Get the stats of the movies
    /// # Errors
    /// Fails if cannot get min date, max date, or convert length
    fn get_stats(movies: &AllMovies) -> Result<(u64, u64, f64, f64), GeneralError> {
        // calculate the min date
        let min_date = movies
            .movies
            .iter()
            .map(|m| m.date)
            .min()
            .ok_or("Cannot get min date of movies")?;
        // calculate the max date
        let max_date = movies
            .movies
            .iter()
            .map(|m| m.date)
            .max()
            .ok_or("Cannot get max date of movies")?;
        // calculate the average note
        let movies_len = f64::from(
            u32::try_from(movies.movies.len())
                .map_err(|e| format!("Cannot convert the length of movies to a f64 {e}"))?,
        );
        let avg_note = movies.movies.iter().map(|m| m.note).sum::<f64>() / movies_len;
        // calculate the median note
        let mut notes = movies.movies.iter().map(|m| m.note).collect::<Vec<f64>>();
        notes.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let median_note = notes[notes.len() / 2];

        Ok((min_date, max_date, avg_note, median_note))
    }

    /// Print the stats of the movies
    /// # Errors
    /// Returns an error if unable to read the movies file
    fn print_stats(config: &mut Config, is_json: bool) -> Result<(), GeneralError> {
        let movies = Movies::get_all_movies(config)?;
        let (min_date, max_date, avg_note, median_note) = Movies::get_stats(&movies)?;
        if is_json {
            let stats = serde_json::json!({
                "movies": movies.movies.len(),
                "min_date": min_date,
                "max_date": max_date,
                "avg_note": avg_note,
                "median_note": median_note,
            });
            println!("{stats}");
        } else {
            println!("Number of movies: {}", movies.movies.len());
            println!("Min date: {min_date}");
            println!("Max date: {max_date}");
            println!("Average note: {avg_note:.3}");
            println!("Median note: {median_note:.3}");
        }
        Ok(())
    }

    /// Full sync movies. Used to set the settings
    ///
    /// # Errors
    /// Fails if updating the config fails
    pub fn full_sync_movies(config: &mut Config, print_json: bool) -> Result<(), GeneralError> {
        Movies::pre_sync_movies(config)?;
        Movies::sync_movies(config, print_json)
    }

    /// Pre sync movies. Used to set the settings
    ///
    /// # Errors
    /// Fails if updating the config fails
    pub fn pre_sync_movies(config: &mut Config) -> Result<(), GeneralError> {
        config_path!(config, movies, Movies, file_path, "movies file");
        config_path!(
            config,
            movies,
            Movies,
            public_file_path,
            "the public file for movies"
        );
        Ok(())
    }

    /// Sync the public movie file
    /// # Errors
    /// Returns an error if unable to read the movies file
    pub fn sync_movies(config: &Config, print_json: bool) -> Result<(), GeneralError> {
        if config.debug > 1 {
            println!("Syncing movies");
        }
        let movies = Movies::get_all_movies(config)?;
        let public_movies_path = get_config_path!(
            config,
            movies,
            Movies,
            public_file_path,
            "the public file for movies"
        )?;
        let movies_by_date = Movies::group_movies_by_date(&movies);
        // create an hashmap with the date as key and the movies number for that date as value
        let movie_by_date_count: std::collections::HashMap<u64, u64> = movies_by_date
            .iter()
            .map(|(date, movies)| (*date, movies.len() as u64))
            .collect();
        // sort the hashmap by date
        let movie_by_date_count = BTreeMap::from_iter(movie_by_date_count);

        let formatter = serde_json::ser::PrettyFormatter::with_indent(b"    ");
        let mut buf = Vec::new();
        let mut ser = serde_json::Serializer::with_formatter(&mut buf, formatter);
        movie_by_date_count.serialize(&mut ser)?;
        if print_json {
            let movies_str = String::from_utf8(buf)?;
            println!("{movies_str}");
        } else {
            std::fs::write(&public_movies_path, buf)?;
            println!("Movies file saved to '{}'", public_movies_path.display());
        }
        Ok(())
    }
}