tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

main.rs (2297B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 mod commands;
      6 use commands::ResultCode;
      7 use std::io::Error;
      8 use std::io::ErrorKind;
      9 
     10 fn main() -> Result<(), Error> {
     11    // The general structure of these functions is to print error cases to
     12    // stdout so that the extension can read them and then do error-handling
     13    // on that end.
     14    let message_length: u32 =
     15        commands::read_message_length(std::io::stdin()).or_else(|_| -> Result<u32, _> {
     16            commands::generate_response("Failed to read message length", ResultCode::Error.into())
     17                .expect("JSON error");
     18            return Err(Error::new(
     19                ErrorKind::InvalidInput,
     20                "Failed to read message length",
     21            ));
     22        })?;
     23    let message: String = commands::read_message_string(std::io::stdin(), message_length).or_else(
     24        |_| -> Result<String, _> {
     25            commands::generate_response("Failed to read message", ResultCode::Error.into())
     26                .expect("JSON error");
     27            return Err(Error::new(
     28                ErrorKind::InvalidInput,
     29                "Failed to read message",
     30            ));
     31        },
     32    )?;
     33    // Deserialize the message with the following expected format
     34    let native_messaging_json: commands::FirefoxCommand =
     35        serde_json::from_str(&message).or_else(|_| -> Result<commands::FirefoxCommand, _> {
     36            commands::generate_response(
     37                "Failed to deserialize message JSON",
     38                ResultCode::Error.into(),
     39            )
     40            .expect("JSON error");
     41            return Err(Error::new(
     42                ErrorKind::InvalidInput,
     43                "Failed to deserialize message JSON",
     44            ));
     45        })?;
     46    commands::process_command(&native_messaging_json).or_else(|e| -> Result<bool, _> {
     47        commands::generate_response(
     48            format!("Failed to process command: {}", e).as_str(),
     49            ResultCode::Error.into(),
     50        )
     51        .expect("JSON error");
     52        return Err(Error::new(
     53            ErrorKind::InvalidInput,
     54            "Failed to process command",
     55        ));
     56    })?;
     57    Ok(())
     58 }