novel-cli 0.17.0

A set of tools for downloading novels from the web, manipulating text, and generating EPUB
Documentation
use color_eyre::eyre::Result;
use fluent_templates::Loader;
use novel_api::{Client, Keyring};

use crate::cmd::Source;
use crate::{LANG_ID, LOCALES};

pub async fn log_in<T>(client: &T, source: &Source, ignore_keyring: bool) -> Result<()>
where
    T: Client,
{
    match client.logged_in().await {
        Ok(false) | Err(_) => {
            let user_name = get_user_name()?;

            if ignore_keyring {
                let password = get_password()?;
                client.log_in(user_name, Some(password)).await?;
            } else {
                let keyring = Keyring::new(source, &user_name)?;
                let password = keyring.get_password();

                if let Ok(password) = password {
                    tracing::info!("Successfully obtained password from Keyring");

                    client.log_in(user_name, Some(password)).await?;
                } else {
                    tracing::info!("Unable to get password from Keyring");

                    let password = get_password()?;
                    client.log_in(user_name, Some(password.clone())).await?;

                    keyring.set_password(password)?;
                }
            }
        }
        _ => (),
    }

    Ok(())
}

pub async fn log_in_without_password<T>(client: &T) -> Result<()>
where
    T: Client,
{
    match client.logged_in().await {
        Ok(false) | Err(_) => {
            let user_name = get_user_name()?;
            client.log_in(user_name, None).await?;
        }
        _ => (),
    }

    Ok(())
}

fn get_user_name() -> Result<String> {
    Ok(novel_api::input(
        LOCALES.lookup(&LANG_ID, "enter_user_name"),
    )?)
}

fn get_password() -> Result<String> {
    Ok(novel_api::password(
        LOCALES.lookup(&LANG_ID, "enter_password"),
    )?)
}