post_archiver/manager/binded.rs
1use crate::{AuthorId, CollectionId, FileMetaId, PlatformId, PostId, TagId};
2
3use super::{PostArchiverConnection, PostArchiverManager};
4
5/// Marker trait for ID types that can be bound to a [`Binded`] context.
6///
7/// Implemented for all strongly-typed ID types in the system.
8pub trait BindableId: Copy {}
9
10impl BindableId for PostId {}
11impl BindableId for AuthorId {}
12impl BindableId for TagId {}
13impl BindableId for PlatformId {}
14impl BindableId for CollectionId {}
15impl BindableId for FileMetaId {}
16
17/// A bound entity context for update, delete, and relation operations.
18///
19/// Created via [`PostArchiverManager::bind(id)`](PostArchiverManager::bind).
20/// The `Id` type parameter is inferred from the argument — no turbofish needed.
21///
22/// # Examples
23/// ```no_run
24/// # use post_archiver::manager::PostArchiverManager;
25/// # use post_archiver::PostId;
26/// # fn example(manager: &PostArchiverManager, post_id: PostId) -> Result<(), post_archiver::error::Error> {
27/// // Binded<'_, PostId, _> — type inferred from post_id
28/// manager.bind(post_id).delete()?;
29/// # Ok(())
30/// # }
31/// ```
32#[derive(Debug)]
33pub struct Binded<'a, Id: BindableId, C: PostArchiverConnection = rusqlite::Connection> {
34 manager: &'a PostArchiverManager<C>,
35 id: Id,
36}
37
38impl<'a, Id: BindableId, C: PostArchiverConnection> Binded<'a, Id, C> {
39 pub fn new(manager: &'a PostArchiverManager<C>, id: Id) -> Self {
40 Self { manager, id }
41 }
42
43 /// Returns the bound entity ID.
44 pub fn id(&self) -> Id {
45 self.id
46 }
47
48 /// Returns a reference to the underlying manager.
49 pub fn manager(&self) -> &'a PostArchiverManager<C> {
50 self.manager
51 }
52
53 /// Shortcut: returns a reference to the database connection.
54 pub fn conn(&self) -> &rusqlite::Connection {
55 self.manager.conn()
56 }
57}