xnode_manager_sdk/file/
handlers.rs

1use crate::{
2    file::models::{
3        CreateDirectory, Directory, File, ReadDirectory, ReadFile, RemoveDirectory, RemoveFile,
4        WriteFile,
5    },
6    utils::{
7        Empty, SessionGetInput, SessionGetOutput, SessionPostInput, SessionPostOutput, session_get,
8        session_post,
9    },
10};
11
12pub fn scope() -> String {
13    "/file".to_string()
14}
15
16pub struct ReadFilePath {
17    pub scope: String,
18}
19pub type ReadFileInput<'a> = SessionGetInput<'a, ReadFilePath, ReadFile>;
20pub type ReadFileOutput = File;
21pub async fn read_file(input: ReadFileInput<'_>) -> SessionGetOutput<ReadFileOutput> {
22    session_get(input, scope(), |path| {
23        format!("/{scope}/read_file", scope = path.scope)
24    })
25    .await
26}
27
28pub struct WriteFilePath {
29    pub scope: String,
30}
31pub type WriteFileInput<'a> = SessionPostInput<'a, WriteFilePath, WriteFile>;
32pub type WriteFileOutput = Empty;
33pub async fn write_file(input: WriteFileInput<'_>) -> SessionPostOutput<WriteFileOutput> {
34    session_post(input, scope(), |path| {
35        format!("/{scope}/write_file", scope = path.scope)
36    })
37    .await
38}
39
40pub struct RemoveFilePath {
41    pub scope: String,
42}
43pub type RemoveFileInput<'a> = SessionPostInput<'a, RemoveFilePath, RemoveFile>;
44pub type RemoveFileOutput = Empty;
45pub async fn remove_file(input: RemoveFileInput<'_>) -> SessionPostOutput<RemoveFileOutput> {
46    session_post(input, scope(), |path| {
47        format!("/{scope}/remove_file", scope = path.scope)
48    })
49    .await
50}
51
52#[derive(Debug)]
53pub struct ReadDirectoryPath {
54    pub scope: String,
55}
56pub type ReadDirectoryInput<'a> = SessionGetInput<'a, ReadDirectoryPath, ReadDirectory>;
57pub type ReadDirectoryOutput = Directory;
58pub async fn read_directory(
59    input: ReadDirectoryInput<'_>,
60) -> SessionGetOutput<ReadDirectoryOutput> {
61    session_get(input, scope(), |path| {
62        format!("/{scope}/read_directory", scope = path.scope)
63    })
64    .await
65}
66
67#[derive(Debug)]
68pub struct CreateDirectoryPath {
69    pub scope: String,
70}
71pub type CreateDirectoryInput<'a> = SessionPostInput<'a, CreateDirectoryPath, CreateDirectory>;
72pub type CreateDirectoryOutput = Empty;
73pub async fn create_directory(
74    input: CreateDirectoryInput<'_>,
75) -> SessionPostOutput<CreateDirectoryOutput> {
76    session_post(input, scope(), |path| {
77        format!("/{scope}/create_directory", scope = path.scope)
78    })
79    .await
80}
81
82#[derive(Debug)]
83pub struct RemoveDirectoryPath {
84    pub scope: String,
85}
86pub type RemoveDirectoryInput<'a> = SessionPostInput<'a, RemoveDirectoryPath, RemoveDirectory>;
87pub type RemoveDirectoryOutput = Empty;
88pub async fn remove_directory(
89    input: RemoveDirectoryInput<'_>,
90) -> SessionPostOutput<RemoveDirectoryOutput> {
91    session_post(input, scope(), |path| {
92        format!("/{scope}/remove_directory", scope = path.scope)
93    })
94    .await
95}