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
use std::fs::OpenOptions;
use chrono::prelude::*;
use crate::{
config::Config,
entry::{Entry, EntryContent},
errors::DiaryError,
utils::{editing, file_system},
};
pub struct NewOptions {
pub open: bool,
}
pub fn new(
opts: &NewOptions,
config: &Config,
date: &Date<Local>,
string_getter: editing::StringGetter,
) -> Result<(), DiaryError> {
config.initialised()?;
let diary_entry = Entry::from_config(config)?;
let mut new_entry_path = file_system::month_folder(config.diary_path().to_path_buf(), date);
file_system::create_month_folder(&new_entry_path)?;
let entry_name = diary_entry.file_name(date);
new_entry_path.push(entry_name);
let result = OpenOptions::new()
.write(true)
.create_new(true)
.open(new_entry_path);
let mut file = match result {
Ok(mut file) => {
editing::add_user_content_to_file(&mut file, diary_entry.file_type().title(date))?;
file
}
Err(e) => return Err(e.into()),
};
if opts.open {
let contents = string_getter("".to_string())?;
editing::add_user_content_to_file(&mut file, contents)?;
};
Ok(())
}
#[cfg(test)]
mod test {
use std::{fs, path::PathBuf};
use chrono::prelude::*;
use init::InitOptions;
use tempfile::tempdir;
use super::{new, NewOptions};
use crate::{
config::Config, entry::Entry, ops::init, utils::editing::test::test_string_getter,
};
#[test]
fn new_success() {
let new_opts = NewOptions { open: false };
let init_opts = InitOptions {
path: PathBuf::from(""),
prefix: None,
git_repo: false,
};
let diary_path = tempdir().unwrap().path().to_path_buf();
let config = Config::builder().diary_path(diary_path).build();
let date = Local.ymd(2021, 11, 6);
init(&init_opts, &config).unwrap();
new(&new_opts, &config, &date, test_string_getter).unwrap();
let diary_file = Entry::from_config(&config).unwrap();
let test_path = diary_file.get_entry_path(&date);
assert!(test_path.exists());
}
#[test]
#[should_panic(expected = "kind: NotFound")]
fn new_not_init() {
let new_opts = NewOptions { open: false };
let diary_path = tempdir().unwrap().path().to_path_buf();
let config = Config::builder().diary_path(diary_path).build();
let date = Local.ymd(2021, 11, 6);
new(&new_opts, &config, &date, test_string_getter).unwrap();
}
#[test]
#[should_panic(expected = "kind: AlreadyExists")]
fn new_fail_second_time() {
let new_opts = NewOptions { open: false };
let init_opts = InitOptions {
path: PathBuf::from(""),
prefix: None,
git_repo: false,
};
let diary_path = tempdir().unwrap().path().to_path_buf();
let config = Config::builder().diary_path(diary_path).build();
let date = Local.ymd(2021, 11, 6);
init(&init_opts, &config).unwrap();
new(&new_opts, &config, &date, test_string_getter).unwrap();
new(&new_opts, &config, &date, test_string_getter).unwrap();
}
#[test]
#[should_panic(expected = "value: UnInitialised")]
fn new_not_init_default_config() {
let new_opts = NewOptions { open: false };
let config = Config::default();
let date = Local.ymd(2021, 11, 6);
new(&new_opts, &config, &date, test_string_getter).unwrap();
}
#[test]
fn new_open_file_success() {
let new_opts = NewOptions { open: true };
let init_opts = InitOptions {
path: PathBuf::from(""),
prefix: None,
git_repo: false,
};
let diary_path = tempdir().unwrap().path().to_path_buf();
let config = Config::builder().diary_path(diary_path).build();
let date = Local.ymd(2021, 11, 6);
init(&init_opts, &config).unwrap();
new(&new_opts, &config, &date, test_string_getter).unwrap();
let diary_file = Entry::from_config(&config).unwrap();
let test_path = diary_file.get_entry_path(&date);
let content = fs::read_to_string(test_path).unwrap();
assert!(content.contains("Test content"));
}
}