objectiveai_sdk/filesystem/config/
functions.rs1use serde::{Serialize, Deserialize};
2
3#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
4#[schemars(rename = "filesystem.config.FunctionsConfig")]
5pub struct FunctionsConfig {
6 #[serde(skip_serializing_if = "FunctionsInventionsConfig::is_none")]
7 #[schemars(extend("omitempty" = true))]
8 pub inventions: Option<FunctionsInventionsConfig>,
9 #[serde(skip_serializing_if = "FunctionsProfilesConfig::is_none")]
10 #[schemars(extend("omitempty" = true))]
11 pub profiles: Option<FunctionsProfilesConfig>,
12 #[serde(skip_serializing_if = "crate::util::vec_is_none_or_empty")]
13 #[schemars(extend("omitempty" = true))]
14 pub favorites: Option<Vec<super::Favorite>>,
15}
16
17impl FunctionsConfig {
18 pub fn is_empty(&self) -> bool {
19 self.inventions.as_ref().is_none_or(|cfg| cfg.is_empty())
20 }
21
22 pub fn is_none(this: &Option<Self>) -> bool {
23 this.as_ref().is_none_or(|cfg| cfg.is_empty())
24 }
25
26 pub fn inventions(&mut self) -> &mut FunctionsInventionsConfig {
27 self.inventions.get_or_insert_with(FunctionsInventionsConfig::default)
28 }
29
30 pub fn profiles(&mut self) -> &mut FunctionsProfilesConfig {
31 self.profiles.get_or_insert_with(FunctionsProfilesConfig::default)
32 }
33
34 pub fn get_favorites(&self) -> &[super::Favorite] {
35 self.favorites.as_deref().unwrap_or(&[])
36 }
37
38 pub fn add_favorite(&mut self, favorite: super::Favorite) {
39 self.favorites.get_or_insert_with(Vec::new).push(favorite);
40 }
41
42 pub fn del_favorite(&mut self, name: &str) -> Result<(), super::super::Error> {
43 let favorites = self.favorites.as_mut().ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
44 let pos = favorites.iter().position(|f| f.get_name() == name)
45 .ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
46 favorites.remove(pos);
47 Ok(())
48 }
49
50 pub fn edit_favorite(&mut self, name: &str) -> Result<&mut super::Favorite, super::super::Error> {
51 let favorites = self.favorites.as_mut().ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
52 favorites.iter_mut().find(|f| f.get_name() == name)
53 .ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))
54 }
55
56 pub fn jq(&self, filter: &str) -> Result<Vec<serde_json::Value>, super::super::Error> {
57 super::super::run_jq(self, filter)
58 }
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
62#[schemars(rename = "filesystem.config.FunctionsInventionsConfig")]
63pub struct FunctionsInventionsConfig {
64 #[serde(default = "FunctionsInventionsConfig::default_remote")]
65 pub remote: crate::Remote,
66}
67
68impl Default for FunctionsInventionsConfig {
69 fn default() -> Self {
70 Self { remote: Self::default_remote() }
71 }
72}
73
74impl FunctionsInventionsConfig {
75 fn default_remote() -> crate::Remote {
76 crate::Remote::Filesystem
77 }
78
79 pub fn is_empty(&self) -> bool {
80 matches!(self.remote, crate::Remote::Filesystem)
81 }
82
83 pub fn is_none(this: &Option<Self>) -> bool {
84 this.as_ref().is_none_or(|cfg| cfg.is_empty())
85 }
86
87 pub fn get_remote(&self) -> crate::Remote {
88 self.remote
89 }
90
91 pub fn set_remote(&mut self, remote: crate::Remote) -> Result<(), super::super::Error> {
92 if matches!(remote, crate::Remote::Mock) {
93 return Err(super::super::Error::InvalidRemote(remote));
94 }
95 self.remote = remote;
96 Ok(())
97 }
98
99 pub fn jq(&self, filter: &str) -> Result<Vec<serde_json::Value>, super::super::Error> {
100 super::super::run_jq(self, filter)
101 }
102}
103
104#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
105#[schemars(rename = "filesystem.config.FunctionsProfilesConfig")]
106pub struct FunctionsProfilesConfig {
107 #[serde(skip_serializing_if = "FunctionsProfilesPairsConfig::is_none")]
108 #[schemars(extend("omitempty" = true))]
109 pub pairs: Option<FunctionsProfilesPairsConfig>,
110 #[serde(skip_serializing_if = "crate::util::vec_is_none_or_empty")]
111 #[schemars(extend("omitempty" = true))]
112 pub favorites: Option<Vec<super::Favorite>>,
113}
114
115impl FunctionsProfilesConfig {
116 pub fn is_empty(&self) -> bool {
117 self.pairs.as_ref().is_none_or(|cfg| cfg.is_empty())
118 && crate::util::vec_is_none_or_empty(&self.favorites)
119 }
120
121 pub fn is_none(this: &Option<Self>) -> bool {
122 this.as_ref().is_none_or(|cfg| cfg.is_empty())
123 }
124
125 pub fn pairs(&mut self) -> &mut FunctionsProfilesPairsConfig {
126 self.pairs.get_or_insert_with(FunctionsProfilesPairsConfig::default)
127 }
128
129 pub fn get_favorites(&self) -> &[super::Favorite] {
130 self.favorites.as_deref().unwrap_or(&[])
131 }
132
133 pub fn add_favorite(&mut self, favorite: super::Favorite) {
134 self.favorites.get_or_insert_with(Vec::new).push(favorite);
135 }
136
137 pub fn del_favorite(&mut self, name: &str) -> Result<(), super::super::Error> {
138 let favorites = self.favorites.as_mut().ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
139 let pos = favorites.iter().position(|f| f.get_name() == name)
140 .ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
141 favorites.remove(pos);
142 Ok(())
143 }
144
145 pub fn edit_favorite(&mut self, name: &str) -> Result<&mut super::Favorite, super::super::Error> {
146 let favorites = self.favorites.as_mut().ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
147 favorites.iter_mut().find(|f| f.get_name() == name)
148 .ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))
149 }
150
151 pub fn jq(&self, filter: &str) -> Result<Vec<serde_json::Value>, super::super::Error> {
152 super::super::run_jq(self, filter)
153 }
154}
155
156#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
157#[schemars(rename = "filesystem.config.FunctionsProfilesPairsConfig")]
158pub struct FunctionsProfilesPairsConfig {
159 #[serde(skip_serializing_if = "crate::util::vec_is_none_or_empty")]
160 #[schemars(extend("omitempty" = true))]
161 pub favorites: Option<Vec<super::PairFavorite>>,
162}
163
164impl FunctionsProfilesPairsConfig {
165 pub fn is_empty(&self) -> bool {
166 crate::util::vec_is_none_or_empty(&self.favorites)
167 }
168
169 pub fn is_none(this: &Option<Self>) -> bool {
170 this.as_ref().is_none_or(|cfg| cfg.is_empty())
171 }
172
173 pub fn get_favorites(&self) -> &[super::PairFavorite] {
174 self.favorites.as_deref().unwrap_or(&[])
175 }
176
177 pub fn add_favorite(&mut self, favorite: super::PairFavorite) {
178 self.favorites.get_or_insert_with(Vec::new).push(favorite);
179 }
180
181 pub fn del_favorite(&mut self, name: &str) -> Result<(), super::super::Error> {
182 let favorites = self.favorites.as_mut().ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
183 let pos = favorites.iter().position(|f| f.get_name() == name)
184 .ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
185 favorites.remove(pos);
186 Ok(())
187 }
188
189 pub fn edit_favorite(&mut self, name: &str) -> Result<&mut super::PairFavorite, super::super::Error> {
190 let favorites = self.favorites.as_mut().ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))?;
191 favorites.iter_mut().find(|f| f.get_name() == name)
192 .ok_or_else(|| super::super::Error::FavoriteNotFound(name.to_string()))
193 }
194
195 pub fn jq(&self, filter: &str) -> Result<Vec<serde_json::Value>, super::super::Error> {
196 super::super::run_jq(self, filter)
197 }
198}