Skip to content

fix(queries): eliminate double-escaping in regex patterns #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
63 changes: 6 additions & 57 deletions aw-client-rust/src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@
//!
//! Taken from default classes in aw-webui

use log::warn;
use rand::Rng;
use serde::{Deserialize, Serialize};

use super::blocking::AwClient as ActivityWatchClient;
use serde_json;

pub type CategoryId = Vec<String>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CategorySpec {
#[serde(rename = "type")]
pub spec_type: String,
#[serde(default)]
pub regex: String,
#[serde(default)]
pub ignore_case: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassSetting {
#[serde(default)]
pub id: Option<i32>,
pub name: Vec<String>,
pub rule: CategorySpec,
#[serde(default)]
pub data: Option<serde_json::Value>,
}

/// Returns the default categorization classes
Expand Down Expand Up @@ -140,56 +142,3 @@ pub fn default_classes() -> Vec<(CategoryId, CategorySpec)> {
),
]
}

/// Get classes from server-side settings using default localhost:5600.
/// Might throw an error if not set yet, in which case we use the default classes as a fallback.
pub fn get_classes() -> Vec<(CategoryId, CategorySpec)> {
get_classes_from_server("localhost", 5600)
}

/// Get classes from server-side settings with custom host and port.
/// Might throw an error if not set yet, in which case we use the default classes as a fallback.
pub fn get_classes_from_server(host: &str, port: u16) -> Vec<(CategoryId, CategorySpec)> {
let mut rng = rand::rng();
let random_int = rng.random_range(0..10001);
let client_id = format!("get-setting-{}", random_int);

// Create a client with a random ID, similar to the Python implementation
let awc = match ActivityWatchClient::new(host, port, &client_id) {
Ok(client) => client,
Err(_) => {
warn!(
"Failed to create ActivityWatch client for {}:{}, using default classes",
host, port
);
return default_classes();
}
};

awc.get_setting("classes")
.map(|setting_value| {
// Try to deserialize the setting into Vec<ClassSetting>
if setting_value.is_null() {
return default_classes();
}

let class_settings: Vec<ClassSetting> = serde_json::from_value(setting_value)
.unwrap_or_else(|_| {
warn!("Failed to deserialize classes setting, using default classes");
return vec![];
});

// Convert ClassSetting to (CategoryId, CategorySpec) format
class_settings
.into_iter()
.map(|class| (class.name, class.rule))
.collect()
})
.unwrap_or_else(|_| {
warn!(
"Failed to get classes from server {}:{}, using default classes as fallback",
host, port
);
default_classes()
})
}
2 changes: 1 addition & 1 deletion aw-client-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl std::fmt::Debug for AwClient {
}

fn get_hostname() -> String {
return gethostname::gethostname().to_string_lossy().to_string();
gethostname::gethostname().to_string_lossy().to_string()
}

impl AwClient {
Expand Down
Loading