1+ use anyhow:: anyhow;
2+ use cron;
3+ use log:: { info, warn} ;
14use rusqlite:: NO_PARAMS ;
25use rusqlite:: { params, Connection } ;
3- use std:: path:: PathBuf ;
46use std:: fs;
5- use url:: Url ;
6- use cron;
7+ use std:: path:: PathBuf ;
78use std:: str:: FromStr ;
8- use log:: { warn, info} ;
9- use anyhow:: anyhow;
9+ use url:: Url ;
1010
1111use super :: file;
1212
@@ -19,7 +19,6 @@ pub struct Schedule {
1919 pub url : String ,
2020}
2121
22-
2322/// Creates the db and path to it if it doesn't already exist
2423pub fn create_db ( ) -> anyhow:: Result < ( ) > {
2524 let path = file:: get_schedule_path ( ) ;
@@ -32,10 +31,10 @@ pub fn create_db() -> anyhow::Result<()> {
3231 if !parent. is_dir ( ) {
3332 fs:: create_dir_all ( parent) ?;
3433 }
35-
34+
3635 let conn: Connection = Connection :: open ( path) ?;
3736 conn. execute (
38- "create table if not exists Schedule (
37+ "create table if not exists Schedule (
3938 id integer primary key,
4039 user text not null,
4140 cron text not null,
@@ -51,16 +50,18 @@ pub fn delete_schedule(id: i32) -> anyhow::Result<()> {
5150 let path: PathBuf = file:: get_schedule_path ( ) ;
5251 let conn: Connection = Connection :: open ( path) ?;
5352
54- conn. execute (
55- "DELETE FROM Schedule WHERE ID = ?1" ,
56- params ! [ id] ,
57- ) ?;
53+ conn. execute ( "DELETE FROM Schedule WHERE ID = ?1" , params ! [ id] ) ?;
5854 Ok ( ( ) )
5955}
6056
6157/// Adds a new schedule to the database
6258pub fn add_schedule ( user : String , cron : String , url : String ) -> anyhow:: Result < ( ) > {
63- let schedule: Schedule = validate_schdule ( Schedule { id : 0 , user, cron, url} ) ?;
59+ let schedule: Schedule = validate_schdule ( Schedule {
60+ id : 0 ,
61+ user,
62+ cron,
63+ url,
64+ } ) ?;
6465
6566 create_db ( ) ?;
6667 let path: PathBuf = file:: get_schedule_path ( ) ;
@@ -77,9 +78,9 @@ pub fn add_schedule(user: String, cron: String, url: String) -> anyhow::Result<(
7778pub fn get_schedule ( ) -> anyhow:: Result < Vec < Schedule > > {
7879 let path = file:: get_schedule_path ( ) ;
7980 let conn: Connection = Connection :: open ( path) ?;
80-
81+
8182 let mut stmt = conn. prepare ( "SELECT id, user, cron, url FROM Schedule" ) ?;
82- let schedule_iter =stmt. query_map ( params ! [ ] , |row| {
83+ let schedule_iter = stmt. query_map ( params ! [ ] , |row| {
8384 Ok ( Schedule {
8485 id : row. get ( 0 ) ?,
8586 user : row. get ( 1 ) ?,
@@ -99,14 +100,14 @@ pub fn get_schedule() -> anyhow::Result<Vec<Schedule>> {
99100 Err ( e) => warn ! ( "Database has invalid data, {} for ID {}" , e, id) ,
100101 }
101102 }
102- Err ( e) => warn ! ( "Invaid result from database, {}" , e)
103+ Err ( e) => warn ! ( "Invaid result from database, {}" , e) ,
103104 }
104105 }
105106 Ok ( schedules)
106107}
107108
108109// Returns a Schedule if its valid, None if its not
109- fn validate_schdule ( schedule : Schedule ) -> anyhow:: Result < Schedule > {
110+ fn validate_schdule ( schedule : Schedule ) -> anyhow:: Result < Schedule > {
110111 if Url :: parse ( & schedule. url ) . is_err ( ) {
111112 return Err ( anyhow ! ( "Invalid schedule" ) ) ;
112113 }
0 commit comments