use crate::VERSION;
#[derive(strum_macros::Display)]
pub enum ResponseFromFile {
AccessToken,
CommentsAllTypes,
Task,
TodayTasksWithoutDuration,
UnscheduledTasks,
TodayTask,
TodayTasks,
Comment,
#[allow(dead_code)]
Label,
Labels,
Project,
Projects,
NewProjects,
Section,
Sections,
Reminder,
Reminders,
User,
Versions,
}
impl ResponseFromFile {
pub async fn read(&self) -> String {
let path = format!("tests/responses/{self}.json");
let json = std::fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Could not find json file at {path}"));
self.replace_values(json).await
}
pub async fn read_with_version(&self, version: &str) -> String {
let path = format!("tests/responses/{self}.json");
let json = std::fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Could not find json file at {path}"));
match self {
Self::Versions => json.replace("INSERTVERSION", version),
_ => self.replace_values(json).await,
}
}
async fn replace_values(&self, json_string: String) -> String {
let replace_with: Vec<(&str, String)> = match self {
Self::AccessToken
| Self::CommentsAllTypes
| Self::Comment
| Self::Task
| Self::Section
| Self::Sections
| Self::Label
| Self::Labels
| Self::Project
| Self::Reminder
| Self::Reminders
| Self::Projects
| Self::NewProjects
| Self::User => Vec::new(),
Self::TodayTask
| Self::UnscheduledTasks
| Self::TodayTasksWithoutDuration
| Self::TodayTasks => vec![("INSERTDATE", super::today_date().await)],
Self::Versions => vec![("INSERTVERSION", VERSION.to_string())],
};
let mut result = json_string;
for (from, to) in replace_with {
result = result.replace(from, &to);
}
result
}
}