Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ pub async fn execute_subscriber_summarize(
let target_language = request
.target_language
.as_deref()
.map(str::trim)
.unwrap_or("");
.map_or("", str::trim);

let client = build_client()?;
let response = client
Expand Down Expand Up @@ -1822,14 +1821,12 @@ fn text_contains_news_filter_keyword(text: &str, keyword: &str) -> bool {
let start_ok = text[..index]
.chars()
.next_back()
.map(|ch| !is_news_filter_word_char(ch))
.unwrap_or(true);
.map_or(true, |ch| !is_news_filter_word_char(ch));
let end_index = index + keyword.len();
let end_ok = text[end_index..]
.chars()
.next()
.map(|ch| !is_news_filter_word_char(ch))
.unwrap_or(true);
.map_or(true, |ch| !is_news_filter_word_char(ch));

if start_ok && end_ok {
return true;
Expand All @@ -1839,7 +1836,7 @@ fn text_contains_news_filter_keyword(text: &str, keyword: &str) -> bool {
false
}

fn is_news_filter_word_char(ch: char) -> bool {
const fn is_news_filter_word_char(ch: char) -> bool {
ch.is_ascii_alphanumeric() || ch == '_'
}

Expand Down Expand Up @@ -1896,8 +1893,7 @@ fn resolve_news_category(
|| category.category_name.eq_ignore_ascii_case(requested)
|| metadata_map
.get(&category.category_id)
.map(|entry| entry.display_name.eq_ignore_ascii_case(requested))
.unwrap_or(false)
.is_some_and(|entry| entry.display_name.eq_ignore_ascii_case(requested))
}) {
return Ok(merge_news_category(
category.clone(),
Expand Down Expand Up @@ -2951,7 +2947,7 @@ fn format_client_error_suffix(body: &str) -> String {
}

if let Ok(payload) = serde_json::from_str::<Value>(trimmed) {
return format!("; {}", payload);
return format!("; {payload}");
}

format!("; {trimmed}")
Expand Down
6 changes: 3 additions & 3 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum CredentialKind {
}

impl CredentialKind {
pub fn as_str(self) -> &'static str {
pub const fn as_str(self) -> &'static str {
match self {
Self::ApiToken => "api-token",
Self::SessionToken => "session-token",
Expand All @@ -34,7 +34,7 @@ pub enum CredentialSource {
}

impl CredentialSource {
pub fn as_str(self) -> &'static str {
pub const fn as_str(self) -> &'static str {
match self {
Self::Env => "env",
Self::Config => "config",
Expand All @@ -59,7 +59,7 @@ impl SearchAuthPreference {
}
}

pub fn as_str(self) -> &'static str {
pub const fn as_str(self) -> &'static str {
match self {
Self::Session => "session",
Self::Api => "api",
Expand Down
32 changes: 14 additions & 18 deletions src/auth_wizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::search;

const VALIDATION_QUERY: &str = "rust lang";
const GOLD: u8 = 220;
const AUTH_ASCII_ART: &str = r#" ███████ ███████ ███████ █████
const AUTH_ASCII_ART: &str = r" ███████ ███████ ███████ █████
███████ █████████ ███████ █████████
███████ █████████ ███████ █████████
███████ ███████ ███████ ███████ ███████
Expand All @@ -36,7 +36,7 @@ const AUTH_ASCII_ART: &str = r#" ███████
███████████████████████
███████████████████████
████████ ████████
███"#;
███";

struct KagiAuthTheme;

Expand Down Expand Up @@ -255,7 +255,7 @@ pub async fn run_auth_wizard() -> Result<(), KagiError> {

fn render_auth_intro() -> Result<(), KagiError> {
let term = Term::stdout();
let width = term.size_checked().map(|(_rows, cols)| cols).unwrap_or(0);
let width = term.size_checked().map_or(0, |(_rows, cols)| cols);

if should_render_auth_ascii(width) {
let mut stdout = io::stdout().lock();
Expand Down Expand Up @@ -334,7 +334,7 @@ fn build_candidate_credential(kind: CredentialKind, input: &str) -> Result<Crede
})
}

fn kind_display(kind: CredentialKind) -> &'static str {
const fn kind_display(kind: CredentialKind) -> &'static str {
match kind {
CredentialKind::ApiToken => "API token",
CredentialKind::SessionToken => "Session Link",
Expand All @@ -361,15 +361,13 @@ fn format_inventory_summary(inventory: &crate::auth::CredentialInventory) -> Str
wizard_status_line(
"Selected",
&inventory
.preferred_for_status()
.map(|credential| {
.preferred_for_status().map_or_else(|| "none".to_string(), |credential| {
format!(
"{} ({})",
credential.kind.as_str(),
credential.source.as_str()
)
})
.unwrap_or_else(|| "none".to_string()),
}),
),
wizard_status_line("Base Search", inventory.search_preference.as_str()),
wizard_status_line(
Expand All @@ -390,15 +388,13 @@ fn format_saved_summary(inventory: &crate::auth::CredentialInventory) -> String
wizard_status_line(
"Selected",
&inventory
.preferred_for_status()
.map(|credential| {
.preferred_for_status().map_or_else(|| "none".to_string(), |credential| {
format!(
"{} ({})",
credential.kind.as_str(),
credential.source.as_str()
)
})
.unwrap_or_else(|| "none".to_string()),
}),
),
wizard_status_line("Base Search", inventory.search_preference.as_str()),
wizard_status_line(
Expand All @@ -413,14 +409,14 @@ fn format_saved_summary(inventory: &crate::auth::CredentialInventory) -> String
.join("\n")
}

fn method_title(kind: CredentialKind) -> &'static str {
const fn method_title(kind: CredentialKind) -> &'static str {
match kind {
CredentialKind::ApiToken => "API Token Setup",
CredentialKind::SessionToken => "Session Link Setup",
}
}

fn method_prompt(kind: CredentialKind) -> &'static str {
const fn method_prompt(kind: CredentialKind) -> &'static str {
match kind {
CredentialKind::ApiToken => "Paste your API token",
CredentialKind::SessionToken => "Paste your Session Link or raw session token",
Expand Down Expand Up @@ -460,7 +456,7 @@ fn env_override_notice(kind: CredentialKind) -> Option<String> {
env::var_os(env_var).map(|_| env_override_message(env_var))
}

fn has_config_credential(snapshot: &ConfigAuthSnapshot, kind: CredentialKind) -> bool {
const fn has_config_credential(snapshot: &ConfigAuthSnapshot, kind: CredentialKind) -> bool {
match kind {
CredentialKind::ApiToken => snapshot.api_token.is_some(),
CredentialKind::SessionToken => snapshot.session_token.is_some(),
Expand All @@ -483,14 +479,14 @@ fn should_prompt_preference_with_other_method(
other_method_configured && snapshot.search_preference != preference_for_kind(kind)
}

fn preference_for_kind(kind: CredentialKind) -> SearchAuthPreference {
const fn preference_for_kind(kind: CredentialKind) -> SearchAuthPreference {
match kind {
CredentialKind::ApiToken => SearchAuthPreference::Api,
CredentialKind::SessionToken => SearchAuthPreference::Session,
}
}

fn other_kind(kind: CredentialKind) -> CredentialKind {
const fn other_kind(kind: CredentialKind) -> CredentialKind {
match kind {
CredentialKind::ApiToken => CredentialKind::SessionToken,
CredentialKind::SessionToken => CredentialKind::ApiToken,
Expand All @@ -501,7 +497,7 @@ fn other_method_configured(snapshot: &ConfigAuthSnapshot, kind: CredentialKind)
has_config_credential(snapshot, other_kind(kind))
}

fn env_var_name(kind: CredentialKind) -> &'static str {
const fn env_var_name(kind: CredentialKind) -> &'static str {
match kind {
CredentialKind::ApiToken => API_TOKEN_ENV,
CredentialKind::SessionToken => SESSION_TOKEN_ENV,
Expand Down
46 changes: 23 additions & 23 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ pub enum OutputFormat {
impl std::fmt::Display for OutputFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OutputFormat::Json => write!(f, "json"),
OutputFormat::Pretty => write!(f, "pretty"),
OutputFormat::Compact => write!(f, "compact"),
OutputFormat::Markdown => write!(f, "markdown"),
OutputFormat::Csv => write!(f, "csv"),
Self::Json => write!(f, "json"),
Self::Pretty => write!(f, "pretty"),
Self::Compact => write!(f, "compact"),
Self::Markdown => write!(f, "markdown"),
Self::Csv => write!(f, "csv"),
}
}
}
Expand All @@ -57,10 +57,10 @@ pub enum QuickOutputFormat {
impl std::fmt::Display for QuickOutputFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
QuickOutputFormat::Json => write!(f, "json"),
QuickOutputFormat::Pretty => write!(f, "pretty"),
QuickOutputFormat::Compact => write!(f, "compact"),
QuickOutputFormat::Markdown => write!(f, "markdown"),
Self::Json => write!(f, "json"),
Self::Pretty => write!(f, "pretty"),
Self::Compact => write!(f, "compact"),
Self::Markdown => write!(f, "markdown"),
}
}
}
Expand All @@ -76,10 +76,10 @@ pub enum AssistantOutputFormat {
impl std::fmt::Display for AssistantOutputFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AssistantOutputFormat::Json => write!(f, "json"),
AssistantOutputFormat::Pretty => write!(f, "pretty"),
AssistantOutputFormat::Compact => write!(f, "compact"),
AssistantOutputFormat::Markdown => write!(f, "markdown"),
Self::Json => write!(f, "json"),
Self::Pretty => write!(f, "pretty"),
Self::Compact => write!(f, "compact"),
Self::Markdown => write!(f, "markdown"),
}
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ pub enum LensTemplate {
}

impl LensTemplate {
pub fn as_form_value(&self) -> &'static str {
pub const fn as_form_value(&self) -> &'static str {
match self {
Self::Default => "0",
Self::News => "1",
Expand All @@ -122,7 +122,7 @@ pub enum NewsFilterMode {
}

impl NewsFilterMode {
pub fn as_str(&self) -> &'static str {
pub const fn as_str(&self) -> &'static str {
match self {
Self::Hide => "hide",
Self::Blur => "blur",
Expand All @@ -138,7 +138,7 @@ pub enum NewsFilterScope {
}

impl NewsFilterScope {
pub fn as_str(&self) -> &'static str {
pub const fn as_str(&self) -> &'static str {
match self {
Self::Title => "title",
Self::Summary => "summary",
Expand Down Expand Up @@ -199,7 +199,7 @@ pub enum Commands {
AskPage(AskPageArgs),
/// Translate text through Kagi Translate using session-token auth
Translate(Box<TranslateArgs>),
/// Answer a query with Kagi's FastGPT API
/// Answer a query with Kagi's `FastGPT` API
Fastgpt(FastGptArgs),
/// Query Kagi's enrichment indexes
Enrich(EnrichCommand),
Expand Down Expand Up @@ -246,13 +246,13 @@ pub struct SearchArgs {
/// Scope search to a Kagi lens by numeric index (e.g., "0", "1", "2").
///
/// Lens indices are user-specific. Find yours by:
/// 1. Visit https://kagi.com/settings/lenses to see enabled lenses
/// 1. Visit <https://kagi.com/settings/lenses> to see enabled lenses
/// 2. Search in Kagi web UI with a lens active
/// 3. Check the URL for the "l=" parameter value
#[arg(long, value_name = "INDEX")]
pub lens: Option<String>,

/// Restrict results to a Kagi region code such as "us", "gb", or "no_region"
/// Restrict results to a Kagi region code such as "us", "gb", or "`no_region`"
#[arg(long, value_name = "REGION")]
pub region: Option<String>,

Expand Down Expand Up @@ -315,7 +315,7 @@ pub struct BatchSearchArgs {
#[arg(long, value_name = "INDEX")]
pub lens: Option<String>,

/// Restrict results to a Kagi region code such as "us", "gb", or "no_region"
/// Restrict results to a Kagi region code such as "us", "gb", or "`no_region`"
#[arg(long, value_name = "REGION")]
pub region: Option<String>,

Expand Down Expand Up @@ -505,7 +505,7 @@ impl NewsArgs {
Ok(())
}

pub fn has_filter_inputs(&self) -> bool {
pub const fn has_filter_inputs(&self) -> bool {
!self.filter_preset.is_empty() || !self.filter_keyword.is_empty()
}
}
Expand Down Expand Up @@ -799,7 +799,7 @@ pub struct TranslateArgs {
#[arg(long)]
pub preserve_formatting: Option<bool>,

/// Raw JSON array passed through as context_memory
/// Raw JSON array passed through as `context_memory`
#[arg(long, value_name = "JSON")]
pub context_memory_json: Option<String>,

Expand Down Expand Up @@ -849,7 +849,7 @@ pub struct EnrichCommand {
pub enum EnrichSubcommand {
/// Query Kagi's Teclis web enrichment index
Web(EnrichArgs),
/// Query Kagi's TinyGem news enrichment index
/// Query Kagi's `TinyGem` news enrichment index
News(EnrichArgs),
}

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub enum KagiError {

impl From<serde_json::Error> for KagiError {
fn from(err: serde_json::Error) -> Self {
KagiError::Parse(format!("JSON serialization error: {}", err))
Self::Parse(format!("JSON serialization error: {err}"))
}
}
Loading
Loading