Skip to content

Commit 204c18f

Browse files
committed
Extract patch
1 parent 5e798bb commit 204c18f

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed

rust/agama-lib/src/questions/http_client.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use std::time::Duration;
2222

2323
use agama_utils::api::{
24-
config::Patch,
24+
patch::{self, Patch},
2525
question::{
2626
Answer, AnswerRule, Config as QuestionsConfig, Policy, Question, QuestionSpec,
2727
UpdateQuestion,
@@ -39,7 +39,7 @@ pub enum QuestionsHTTPClientError {
3939
#[error("Unknown question with ID {0}")]
4040
UnknownQuestion(u32),
4141
#[error(transparent)]
42-
Json(#[from] serde_json::Error),
42+
Patch(#[from] patch::Error),
4343
}
4444

4545
pub struct HTTPClient {
@@ -101,9 +101,7 @@ impl HTTPClient {
101101
..Default::default()
102102
};
103103

104-
let patch = Patch {
105-
update: Some(serde_json::to_value(config)?),
106-
};
104+
let patch = Patch::with_update(&config)?;
107105

108106
_ = self.client.patch_void("/v2/config", &patch).await?;
109107
Ok(())
@@ -122,9 +120,7 @@ impl HTTPClient {
122120
..Default::default()
123121
};
124122

125-
let patch = Patch {
126-
update: Some(serde_json::to_value(config)?),
127-
};
123+
let patch = Patch::with_update(&config)?;
128124
self.client.patch_void("/v2/config", &patch).await?;
129125
Ok(())
130126
}

rust/agama-server/src/server/web.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use agama_manager::{self as manager, message};
2626
use agama_utils::{
2727
actor::Handler,
2828
api::{
29-
config, event,
29+
event,
3030
question::{Question, QuestionSpec, UpdateQuestion},
31-
Action, Config, IssueMap, Status, SystemInfo,
31+
Action, Config, IssueMap, Patch, Status, SystemInfo,
3232
},
3333
question,
3434
};
@@ -206,12 +206,12 @@ async fn put_config(State(state): State<ServerState>, Json(json): Json<Value>) -
206206
(status = 400, description = "Not possible to patch the configuration.")
207207
),
208208
params(
209-
("config" = config::Patch, description = "Changes in the configuration.")
209+
("patch" = Patch, description = "Changes in the configuration.")
210210
)
211211
)]
212212
async fn patch_config(
213213
State(state): State<ServerState>,
214-
Json(patch): Json<config::Patch>,
214+
Json(patch): Json<Patch>,
215215
) -> ServerResult<()> {
216216
if let Some(json) = patch.update {
217217
config_schema::check(&json)?;

rust/agama-server/src/web/docs/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl ApiDocBuilder for ConfigApiDocBuilder {
164164
.schema_from::<agama_utils::api::Scope>()
165165
.schema_from::<agama_utils::api::Status>()
166166
.schema_from::<agama_utils::api::SystemInfo>()
167-
.schema_from::<agama_utils::api::config::Patch>()
167+
.schema_from::<agama_utils::api::Patch>()
168168
.schema_from::<agama_utils::api::l10n::Config>()
169169
.schema_from::<agama_utils::api::l10n::Keymap>()
170170
.schema_from::<agama_utils::api::l10n::LocaleEntry>()

rust/agama-utils/src/api.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub use system_info::SystemInfo;
4242
pub mod config;
4343
pub use config::Config;
4444

45+
pub mod patch;
46+
pub use patch::Patch;
47+
4548
mod proposal;
4649
pub use proposal::Proposal;
4750

rust/agama-utils/src/api/config.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
use crate::api::{l10n, question};
2222
use serde::{Deserialize, Serialize};
23-
use serde_json::{value::RawValue, Value};
23+
use serde_json::value::RawValue;
2424

2525
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
2626
#[serde(rename_all = "camelCase")]
@@ -37,11 +37,3 @@ pub struct Config {
3737
#[schema(value_type = Object)]
3838
pub legacy_autoyast_storage: Option<Box<RawValue>>,
3939
}
40-
41-
/// Patch for the config.
42-
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
43-
#[serde(rename_all = "camelCase")]
44-
pub struct Patch {
45-
/// Update for the current config.
46-
pub update: Option<Value>,
47-
}

rust/agama-utils/src/api/patch.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) [2025] SUSE LLC
2+
//
3+
// All Rights Reserved.
4+
//
5+
// This program is free software; you can redistribute it and/or modify it
6+
// under the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 2 of the License, or (at your option)
8+
// any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful, but WITHOUT
11+
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13+
// more details.
14+
//
15+
// You should have received a copy of the GNU General Public License along
16+
// with this program; if not, contact SUSE LLC.
17+
//
18+
// To contact SUSE LLC about this file by physical or electronic mail, you may
19+
// find current contact information at www.suse.com.
20+
21+
use crate::api::config::Config;
22+
use serde::{Deserialize, Serialize};
23+
use serde_json::Value;
24+
25+
#[derive(thiserror::Error, Debug)]
26+
pub enum Error {
27+
#[error(transparent)]
28+
Json(#[from] serde_json::Error),
29+
}
30+
31+
/// Patch for the config.
32+
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
33+
#[serde(rename_all = "camelCase")]
34+
pub struct Patch {
35+
/// Update for the current config.
36+
pub update: Option<Value>,
37+
}
38+
39+
impl Patch {
40+
pub fn with_update(config: &Config) -> Result<Self, Error> {
41+
Ok(Self {
42+
update: Some(serde_json::to_value(config)?),
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)