Skip to main content

agent_tui_usecases/usecases/
input.rs

1//! Input use case.
2
3use std::sync::Arc;
4
5use crate::domain::KeydownInput;
6use crate::domain::KeydownOutput;
7use crate::domain::KeystrokeInput;
8use crate::domain::KeystrokeOutput;
9use crate::domain::KeyupInput;
10use crate::domain::KeyupOutput;
11use crate::domain::TypeInput;
12use crate::domain::TypeOutput;
13use crate::usecases::ports::SessionError;
14use crate::usecases::ports::SessionRepository;
15
16pub trait KeystrokeUseCase: Send + Sync {
17    fn execute(&self, input: KeystrokeInput) -> Result<KeystrokeOutput, SessionError>;
18}
19
20pub struct KeystrokeUseCaseImpl<R: SessionRepository> {
21    repository: Arc<R>,
22}
23
24impl<R: SessionRepository> KeystrokeUseCaseImpl<R> {
25    pub fn new(repository: Arc<R>) -> Self {
26        Self { repository }
27    }
28}
29
30impl<R: SessionRepository> KeystrokeUseCase for KeystrokeUseCaseImpl<R> {
31    fn execute(&self, input: KeystrokeInput) -> Result<KeystrokeOutput, SessionError> {
32        let session = self.repository.resolve(input.session_id.as_ref())?;
33        session.keystroke(&input.key)?;
34
35        Ok(KeystrokeOutput { success: true })
36    }
37}
38
39pub trait TypeUseCase: Send + Sync {
40    fn execute(&self, input: TypeInput) -> Result<TypeOutput, SessionError>;
41}
42
43pub struct TypeUseCaseImpl<R: SessionRepository> {
44    repository: Arc<R>,
45}
46
47impl<R: SessionRepository> TypeUseCaseImpl<R> {
48    pub fn new(repository: Arc<R>) -> Self {
49        Self { repository }
50    }
51}
52
53impl<R: SessionRepository> TypeUseCase for TypeUseCaseImpl<R> {
54    fn execute(&self, input: TypeInput) -> Result<TypeOutput, SessionError> {
55        let session = self.repository.resolve(input.session_id.as_ref())?;
56        session.type_text(&input.text)?;
57
58        Ok(TypeOutput { success: true })
59    }
60}
61
62pub trait KeydownUseCase: Send + Sync {
63    fn execute(&self, input: KeydownInput) -> Result<KeydownOutput, SessionError>;
64}
65
66pub struct KeydownUseCaseImpl<R: SessionRepository> {
67    repository: Arc<R>,
68}
69
70impl<R: SessionRepository> KeydownUseCaseImpl<R> {
71    pub fn new(repository: Arc<R>) -> Self {
72        Self { repository }
73    }
74}
75
76impl<R: SessionRepository> KeydownUseCase for KeydownUseCaseImpl<R> {
77    fn execute(&self, input: KeydownInput) -> Result<KeydownOutput, SessionError> {
78        let session = self.repository.resolve(input.session_id.as_ref())?;
79        session.keydown(&input.key)?;
80
81        Ok(KeydownOutput { success: true })
82    }
83}
84
85pub trait KeyupUseCase: Send + Sync {
86    fn execute(&self, input: KeyupInput) -> Result<KeyupOutput, SessionError>;
87}
88
89pub struct KeyupUseCaseImpl<R: SessionRepository> {
90    repository: Arc<R>,
91}
92
93impl<R: SessionRepository> KeyupUseCaseImpl<R> {
94    pub fn new(repository: Arc<R>) -> Self {
95        Self { repository }
96    }
97}
98
99impl<R: SessionRepository> KeyupUseCase for KeyupUseCaseImpl<R> {
100    fn execute(&self, input: KeyupInput) -> Result<KeyupOutput, SessionError> {
101        let session = self.repository.resolve(input.session_id.as_ref())?;
102        session.keyup(&input.key)?;
103
104        Ok(KeyupOutput { success: true })
105    }
106}
107
108#[cfg(test)]
109#[path = "input_tests.rs"]
110mod tests;