pub struct Tags { /* private fields */ }Expand description
Tag table access, including task-tag links.
Implementations§
Source§impl Tags
impl Tags
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Opens the database and ensures both tag tables exist (migration v3 creates them officially).
use kasl::db::tags::Tags;
let mut tags = Tags::new()?;Sourcepub fn create(&mut self, tag: &Tag) -> Result<i32>
pub fn create(&mut self, tag: &Tag) -> Result<i32>
Inserts the tag and returns its assigned id; duplicate names are rejected by the unique constraint.
use kasl::db::tags::{Tags, Tag};
let mut tags = Tags::new()?;
let tag = Tag::new("priority".to_string(), Some("orange".to_string()));
let tag_id = tags.create(&tag)?;
println!("Created tag with ID: {}", tag_id);Sourcepub fn update(&mut self, tag: &Tag) -> Result<()>
pub fn update(&mut self, tag: &Tag) -> Result<()>
Updates name and color by id; errors when the tag has no id or no longer exists.
let mut tags = Tags::new()?;
let tag_id = 1;
let mut tag = tags.get_by_id(tag_id)?.unwrap();
tag.name = "high-priority".to_string();
tag.color = Some("crimson".to_string());
tags.update(&tag)?;Sourcepub fn delete(&mut self, id: i32) -> Result<()>
pub fn delete(&mut self, id: i32) -> Result<()>
Deletes the tag; its task links go with it via CASCADE.
let mut tags = Tags::new()?;
let tag_id = 1;
tags.delete(tag_id)?;Sourcepub fn get_all(&mut self) -> Result<Vec<Tag>>
pub fn get_all(&mut self) -> Result<Vec<Tag>>
Returns every tag, sorted by name.
let mut tags = Tags::new()?;
let all_tags = tags.get_all()?;
for tag in all_tags {
println!("Tag: {} ({})", tag.name, tag.color.unwrap_or("no color".to_string()));
}Sourcepub fn get_by_name(&mut self, name: &str) -> Result<Option<Tag>>
pub fn get_by_name(&mut self, name: &str) -> Result<Option<Tag>>
Fetches one tag by exact (case-sensitive) name.
let mut tags = Tags::new()?;
if let Some(tag) = tags.get_by_name("urgent")? {
println!("Found tag: {} with color: {:?}", tag.name, tag.color);
} else {
println!("Tag 'urgent' not found");
}Sourcepub fn get_by_id(&mut self, id: i32) -> Result<Option<Tag>>
pub fn get_by_id(&mut self, id: i32) -> Result<Option<Tag>>
Fetches one tag by id.
let mut tags = Tags::new()?;
if let Some(tag) = tags.get_by_id(42)? {
println!("Tag ID 42: {}", tag.name);
}Returns the task’s tags, sorted by name; empty when it has none.
let mut tags = Tags::new()?;
let task_id = 1;
let task_tags = tags.get_tags_by_task(task_id)?;
for tag in task_tags {
println!("Task has tag: {}", tag.name);
}Sourcepub fn get_tasks_by_tag(&mut self, tag_id: i32) -> Result<Vec<i32>>
pub fn get_tasks_by_tag(&mut self, tag_id: i32) -> Result<Vec<i32>>
Returns the ids of tasks carrying the tag.
let mut tags = Tags::new()?;
let tag_id = 1;
let task_ids = tags.get_tasks_by_tag(tag_id)?;
println!("Tag is used by {} tasks", task_ids.len());Sourcepub fn add_tag_to_task(&mut self, task_id: i32, tag_id: i32) -> Result<()>
pub fn add_tag_to_task(&mut self, task_id: i32, tag_id: i32) -> Result<()>
Links a tag to a task; idempotent thanks to OR IGNORE.
let mut tags = Tags::new()?;
let (task_id, tag_id) = (1, 1);
tags.add_tag_to_task(task_id, tag_id)?;Sourcepub fn remove_tag_from_task(&mut self, task_id: i32, tag_id: i32) -> Result<()>
pub fn remove_tag_from_task(&mut self, task_id: i32, tag_id: i32) -> Result<()>
Unlinks a tag from a task; a missing link is not an error.
let mut tags = Tags::new()?;
let (task_id, tag_id) = (1, 1);
tags.remove_tag_from_task(task_id, tag_id)?;Removes every tag link from the task; returns how many went.
let mut tags = Tags::new()?;
let task_id = 1;
let removed_count = tags.remove_all_tags_from_task(task_id)?;
println!("Removed {} tag associations", removed_count);Replaces the task’s tag set: clears existing links, then adds the
given ids. Not transactional - a failure after the clear leaves the
task untagged. Ids must exist; use Tags::get_or_create_tags when
unsure.
let mut tags = Tags::new()?;
let task_id = 1;
let new_tag_ids = vec![1, 3, 5]; // urgent, backend, review
tags.set_task_tags(task_id, &new_tag_ids)?;
let current_tags = tags.get_tags_by_task(task_id)?;
assert_eq!(current_tags.len(), 3);Resolves names to tag ids, creating missing tags with a color from the default rotation.
let mut tags = Tags::new()?;
let tag_names = vec!["urgent".to_string(), "backend".to_string()];
let tag_ids = tags.get_or_create_tags(&tag_names)?;