gitflow_core/session.rs
1//! Shared session context for adapter operations.
2//!
3//! The [`Session`] struct holds state that can be reused across multiple
4//! provider operations, such as repository information and cached auth tokens.
5//! This is particularly useful for workflow-chain commands that perform
6//! multiple operations in sequence.
7//!
8//! # Example
9//!
10//! ```
11//! use gitflow_core::Session;
12//!
13//! let session = Session::new("owner/repo", "github");
14//! assert_eq!(session.repo, "owner/repo");
15//! ```
16
17/// Shared session context for adapter operations.
18///
19/// Holds repository information and optional cached auth tokens to avoid
20/// redundant subprocess calls when performing multiple operations in sequence.
21///
22/// # Fields
23///
24/// - `repo`: Target repository in `"owner/repo"` format.
25/// - `platform`: Platform identifier (`"github"`, `"gitlab"`, or `"gitcode"`).
26/// - `cached_token`: Optional cached auth token. Currently unused but reserved for future
27/// token-caching optimization when workflow-chain commands exist.
28#[derive(Debug, Clone)]
29pub struct Session {
30 /// Target repository in `"owner/repo"` format.
31 pub repo: String,
32 /// Platform identifier (`"github"`, `"gitlab"`, or `"gitcode"`).
33 pub platform: String,
34 /// Cached auth token (reserved for future use).
35 ///
36 /// True caching requires interior mutability (`Mutex<String>` or
37 /// `RefCell<Option<String>>`) because provider methods take `&self`.
38 /// For now, this field documents the intent and can be populated
39 /// when a workflow-chain command demonstrates the performance benefit.
40 #[allow(dead_code, reason = "Reserved for future token-caching optimization")]
41 pub(crate) cached_token: Option<String>,
42}
43
44impl Session {
45 /// Create a new session with the given repository and platform.
46 ///
47 /// # Arguments
48 ///
49 /// * `repo` — Repository in `"owner/repo"` format.
50 /// * `platform` — Platform identifier: `"github"`, `"gitlab"`, or `"gitcode"`.
51 ///
52 /// # Examples
53 ///
54 /// ```
55 /// use gitflow_core::Session;
56 /// let session = Session::new("byx-darwin/gitflow-cli", "github");
57 /// assert_eq!(session.repo, "byx-darwin/gitflow-cli");
58 /// assert_eq!(session.platform, "github");
59 /// ```
60 #[must_use]
61 pub fn new(repo: impl Into<String>, platform: impl Into<String>) -> Self {
62 Self {
63 repo: repo.into(),
64 platform: platform.into(),
65 cached_token: None,
66 }
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_should_create_session_with_repo_and_platform() {
76 let session = Session::new("owner/repo", "github");
77 assert_eq!(session.repo, "owner/repo");
78 assert_eq!(session.platform, "github");
79 assert!(session.cached_token.is_none());
80 }
81
82 #[test]
83 fn test_should_clone_session() {
84 let session = Session::new("owner/repo", "gitlab");
85 let cloned = session.clone();
86 assert_eq!(cloned.repo, session.repo);
87 assert_eq!(cloned.platform, session.platform);
88 }
89
90 #[test]
91 fn test_should_debug_format_session() {
92 let session = Session::new("test/repo", "gitcode");
93 let debug_str = format!("{session:?}");
94 assert!(debug_str.contains("test/repo"));
95 assert!(debug_str.contains("gitcode"));
96 }
97}