@@ -19,6 +19,10 @@ enum InitType {
1919 Update
2020}
2121
22+ /// Handles communication with clients connecting to the server.
23+ ///
24+ /// * `stream` - Data stream representing the connection between the client and the server.
25+ /// * `blocker` - Pointer to the Engine struct which should be used for decision making.
2226fn handle_client ( mut stream : UnixStream , mut blocker : Arc < Engine > ) {
2327 let reader = BufReader :: new ( stream. try_clone ( ) . unwrap ( ) ) ;
2428 for line in reader. lines ( ) {
@@ -79,6 +83,12 @@ fn handle_client(mut stream: UnixStream, mut blocker: Arc<Engine>) {
7983 }
8084}
8185
86+ /// Updates the specified filter list, returns the URL with the expiration timestamp.
87+ ///
88+ /// The timestamp won't be appended if the filter list doesn't contain the `Expired` field.
89+ ///
90+ /// * `url` - URL of the filter list that should be updated.
91+ /// * `lists_dir` - Directory path where the filter list should be saved.
8292fn update_list ( url : & str , lists_dir : & str ) -> String {
8393 let filename = url. split ( '/' ) . last ( ) . unwrap ( ) ;
8494 let res = attohttpc:: get ( & url) . send ( ) . unwrap ( ) ;
@@ -107,6 +117,15 @@ fn update_list(url: &str, lists_dir: &str) -> String {
107117 return url. to_string ( ) ;
108118}
109119
120+ /// Parses the URL file and returns a boolean stating if the configuration was updated.
121+ ///
122+ /// Process every URL in the `urls_file`. If the file doesn't exist, it will be created.
123+ ///
124+ /// * `urls_file` - Path of the URL file.
125+ /// * `lists_dir` - Directory path where the filter lists should be saved.
126+ /// * `force_update` - Boolean which when set to true will forcefully update every filter list
127+ /// present in the URL file, even though it doesn't have to be updated (based on the 'Expires'
128+ /// field)
110129fn parse_urls ( urls_file : & str , lists_dir : & str , force_update : bool ) -> bool {
111130 fs:: create_dir_all ( & lists_dir) . unwrap ( ) ;
112131 let mut updated = false ;
@@ -147,6 +166,16 @@ fn parse_urls(urls_file: &str, lists_dir: &str, force_update: bool) -> bool {
147166 return updated;
148167}
149168
169+ /// Initializes the blocking engine from the adblock-rust crate, returns the initialized engine.
170+ ///
171+ /// If the engine configuration wasn't updated the engine data will be loaded from the serialized
172+ /// engine file, if it exists. If not, the files located in `lists_dir` will be processed as filter
173+ /// lists.
174+ ///
175+ /// * `engine_file` - Path of the serialized engine file.
176+ /// * `lists_dir` - Directory path where the filter lists should be saved.
177+ /// * `updated` - Boolean which when set to true will cause the serialized engine file to be
178+ /// ingored.
150179fn init_engine ( engine_file : & str , lists_dir : & str , updated : bool ) -> Engine {
151180 if Path :: new ( & engine_file) . exists ( ) && !updated {
152181 let mut blocker = Engine :: new ( true ) ;
@@ -183,6 +212,12 @@ fn init_engine(engine_file: &str, lists_dir: &str, updated: bool) -> Engine {
183212 }
184213}
185214
215+ /// Starts handling incoming client connections.
216+ ///
217+ /// The socket file will get automatically removed if it already exists.
218+ ///
219+ /// * `socket_path` - Path of the socket file used for communication.
220+ /// * `blocker` - The inialized Engine that should be used for filtering.
186221fn start_server ( socket_path : & str , blocker : Engine ) {
187222 if std:: path:: Path :: new ( socket_path) . exists ( ) {
188223 fs:: remove_file ( socket_path) . expect ( "Can't remove Unix domain socket file" ) ;
@@ -206,6 +241,12 @@ fn start_server(socket_path: &str, blocker: Engine) {
206241 }
207242}
208243
244+ /// Setup needed before initializing the server, returns the initialized Engine struct.
245+ ///
246+ /// Consists of finding the $HOME directory, parsing the url file with `parse_urls` based on
247+ /// specified `init_type` and creating the custom filters file if it doesn't exist.
248+ ///
249+ /// * `init_type` - Specifies the type of Engine initialization.
209250fn setup_blocker ( init_type : InitType ) -> Engine {
210251 let home_dir = var ( "HOME" ) . expect ( "Can't find environment variable $HOME" ) ;
211252 let config_dir = home_dir. to_owned ( ) + "/.config/ars" ;
@@ -242,6 +283,8 @@ fn setup_blocker(init_type: InitType) -> Engine {
242283 return init_engine ( & engine_file, & lists_dir, updated) ;
243284}
244285
286+ /// The main function that initializes the blocker with `setup_blocker` and starts the server with
287+ /// `start_server`.
245288fn main ( ) {
246289 let blocker = setup_blocker ( InitType :: Default ) ;
247290 start_server ( "/tmp/ars" , blocker) ;
0 commit comments