diff --git a/src/core/error.rs b/src/core/error.rs index cd3950d..9e21fb7 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -29,6 +29,9 @@ pub enum CoreError { impl From for CoreError { fn from(err: rusqlite_migration::Error) -> Self { - CoreError::Database(rusqlite::Error::from(err)) + match err { + rusqlite_migration::Error::Rusqlite(e) => CoreError::Database(e), + e => CoreError::Other(e.into()), + } } } diff --git a/src/core/item_service.rs b/src/core/item_service.rs index df76273..2d77902 100644 --- a/src/core/item_service.rs +++ b/src/core/item_service.rs @@ -8,7 +8,6 @@ use crate::db::{self, Meta}; use crate::compression_engine::{get_compression_engine, CompressionType}; use crate::modes::common::settings_compression_type; use clap::Command; -use log::debug; use rusqlite::Connection; use std::collections::HashMap; use std::fs; diff --git a/src/db.rs b/src/db.rs index 9b9cb03..c9c616c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,6 +1,7 @@ use anyhow::{Context, Error, Result}; use chrono::prelude::*; use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; use log::*; use rusqlite::{Connection, OpenFlags, params}; use rusqlite_migration::{M, Migrations}; @@ -37,7 +38,7 @@ lazy_static! { ]); } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Item { pub id: Option, pub ts: DateTime, @@ -45,13 +46,13 @@ pub struct Item { pub compression: String, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Tag { pub id: i64, pub name: String, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Meta { pub id: i64, pub name: String, diff --git a/src/modes/info.rs b/src/modes/info.rs index 10d9f5f..bf61d29 100644 --- a/src/modes/info.rs +++ b/src/modes/info.rs @@ -74,7 +74,7 @@ fn show_item( let item = item_with_meta.item; let item_id = item.id.unwrap(); - let item_tags: Vec = item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let mut table = Table::new(); if std::io::stdout().is_terminal() { @@ -156,7 +156,7 @@ fn show_item_structured( ) -> Result<()> { let item = item_with_meta.item; let item_id = item.id.unwrap(); - let item_tags: Vec = item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let mut item_path_buf = data_path.clone(); item_path_buf.push(item_id.to_string()); diff --git a/src/modes/list.rs b/src/modes/list.rs index 9472cdc..7eac340 100644 --- a/src/modes/list.rs +++ b/src/modes/list.rs @@ -71,7 +71,7 @@ pub fn mode_list( for item_with_meta in items_with_meta { let item = item_with_meta.item; - let tags: Vec = item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let meta = item_with_meta.meta_as_map(); let mut item_path = data_path.clone(); @@ -297,7 +297,7 @@ fn show_list_structured( for item_with_meta in items_with_meta { let item = item_with_meta.item; let item_id = item.id.unwrap(); - let tags = item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let meta = item_with_meta.meta_as_map(); let mut item_path = data_path.clone(); diff --git a/src/modes/server/api/item.rs b/src/modes/server/api/item.rs index 15d05bd..13e157a 100644 --- a/src/modes/server/api/item.rs +++ b/src/modes/server/api/item.rs @@ -57,7 +57,7 @@ pub async fn handle_list_items( // Apply ordering (default is newest first) match params.order.as_deref().unwrap_or("newest") { "newest" => items_with_meta.sort_by(|a, b| b.item.ts.cmp(&a.item.ts)), - "oldest" => items_with_meta.sort_by(|a, b| a.item.ts.cmp(&a.item.ts)), + "oldest" => items_with_meta.sort_by(|a, b| a.item.ts.cmp(&b.item.ts)), _ => items_with_meta.sort_by(|a, b| b.item.ts.cmp(&a.item.ts)), // default to newest } @@ -70,7 +70,7 @@ pub async fn handle_list_items( .into_iter() .map(|item_with_meta| { let item_id = item_with_meta.item.id.unwrap_or(0); - let item_tags = item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let item_meta = item_with_meta.meta_as_map(); ItemInfo { diff --git a/src/modes/server/mcp/tools.rs b/src/modes/server/mcp/tools.rs index 8a06595..31a9548 100644 --- a/src/modes/server/mcp/tools.rs +++ b/src/modes/server/mcp/tools.rs @@ -100,7 +100,7 @@ impl KeepTools { let item = item_with_content.item_with_meta.item; let content = String::from_utf8_lossy(&item_with_content.content).to_string(); - let tags: Vec = item_with_content.item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let tags: Vec = item_with_content.item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let metadata = item_with_content.item_with_meta.meta_as_map(); let response = serde_json::json!({ @@ -118,6 +118,7 @@ impl KeepTools { pub async fn get_latest_item(&self, args: Option) -> Result { let tags: Vec = args + .as_ref() .and_then(|v| v.get("tags")) .and_then(|v| v.as_array()) .map(|arr| arr.iter().filter_map(|v| v.as_str().map(|s| s.to_string())).collect()) @@ -136,7 +137,7 @@ impl KeepTools { let item = item_with_content.item_with_meta.item; let content = String::from_utf8_lossy(&item_with_content.content).to_string(); - let tags: Vec = item_with_content.item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let tags: Vec = item_with_content.item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let metadata = item_with_content.item_with_meta.meta_as_map(); let response = serde_json::json!({ @@ -182,7 +183,7 @@ impl KeepTools { .map(|item_with_meta| { let item = item_with_meta.item; let item_id = item.id.unwrap_or(0); - let item_tags: Vec = item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let item_meta = item_with_meta.meta_as_map(); serde_json::json!({ @@ -234,7 +235,7 @@ impl KeepTools { .map(|item_with_meta| { let item = item_with_meta.item; let item_id = item.id.unwrap_or(0); - let item_tags: Vec = item_with_meta.tags.into_iter().map(|t| t.name).collect(); + let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let item_meta = item_with_meta.meta_as_map(); serde_json::json!({