use crate::VERSION;
#[derive(strum_macros::Display)]
pub enum ResponseFromFile {
AccessToken,
CommentsAllTypes,
Task,
TodayTasksWithoutDuration,
UnscheduledTasks,
TodayTask,
Ids,
TodayTasks,
Comment,
#[allow(dead_code)]
Label,
Labels,
Project,
Projects,
NewProjects,
Section,
Sections,
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 => Vec::new(),
Self::CommentsAllTypes => Vec::new(),
Self::Comment => Vec::new(),
Self::Task => Vec::new(),
Self::Ids => Vec::new(),
Self::Section => Vec::new(),
Self::Sections => Vec::new(),
Self::Label => Vec::new(),
Self::Labels => Vec::new(),
Self::Project => Vec::new(),
Self::Projects => Vec::new(),
Self::NewProjects => Vec::new(),
Self::User => Vec::new(),
Self::TodayTask => vec![("INSERTDATE", super::today_date().await)],
Self::UnscheduledTasks => vec![("INSERTDATE", super::today_date().await)],
Self::TodayTasksWithoutDuration => vec![("INSERTDATE", super::today_date().await)],
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
}
}