Skip to content

Commit c83592f

Browse files
committed
Use serde Value instead of RawValue
- Value works with 'flatten' while deserializing. - Value allows to directly check whether the json is null.
1 parent bdfd2b1 commit c83592f

File tree

16 files changed

+82
-115
lines changed

16 files changed

+82
-115
lines changed

rust/agama-manager/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread", "sync"] }
1313
async-trait = "0.1.83"
1414
zbus = { version = "5", default-features = false, features = ["tokio"] }
1515
merge-struct = "0.1.0"
16-
serde_json = { version = "1.0.140", features = ["raw_value"] }
16+
serde_json = "1.0.140"
1717

1818
[dev-dependencies]
1919
tokio-test = "0.4.4"

rust/agama-manager/src/message.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use agama_utils::{
2222
actor::Message,
2323
api::{Action, Config, IssueMap, Proposal, Status, SystemInfo},
2424
};
25-
use serde_json::value::RawValue;
25+
use serde_json::Value;
2626

2727
/// Gets the installation status.
2828
pub struct GetStatus;
@@ -124,16 +124,16 @@ impl Message for RunAction {
124124
pub struct GetStorageModel;
125125

126126
impl Message for GetStorageModel {
127-
type Reply = Option<Box<RawValue>>;
127+
type Reply = Option<Value>;
128128
}
129129

130130
// Sets the storage model.
131131
pub struct SetStorageModel {
132-
pub model: Box<RawValue>,
132+
pub model: Value,
133133
}
134134

135135
impl SetStorageModel {
136-
pub fn new(model: Box<RawValue>) -> Self {
136+
pub fn new(model: Value) -> Self {
137137
Self { model }
138138
}
139139
}

rust/agama-manager/src/service.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use agama_utils::{
2929
};
3030
use async_trait::async_trait;
3131
use merge_struct::merge;
32-
use serde_json::value::RawValue;
32+
use serde_json::Value;
3333
use tokio::sync::broadcast;
3434

3535
#[derive(Debug, thiserror::Error)]
@@ -163,8 +163,7 @@ impl MessageHandler<message::GetExtendedConfig> for Service {
163163
Ok(Config {
164164
l10n: Some(l10n),
165165
questions: Some(questions),
166-
storage: storage.as_ref().and_then(|c| c.storage.clone()),
167-
legacy_autoyast_storage: storage.and_then(|c| c.legacy_autoyast_storage),
166+
storage,
168167
})
169168
}
170169
}
@@ -194,7 +193,7 @@ impl MessageHandler<message::SetConfig> for Service {
194193
.await?;
195194

196195
self.storage
197-
.call(storage::message::SetConfig::new((&config).try_into().ok()))
196+
.call(storage::message::SetConfig::new(config.storage.clone()))
198197
.await?;
199198

200199
self.config = config;
@@ -223,9 +222,9 @@ impl MessageHandler<message::UpdateConfig> for Service {
223222
.await?;
224223
}
225224

226-
if let Some(storage) = (&config).try_into().ok() {
225+
if let Some(storage) = &config.storage {
227226
self.storage
228-
.call(storage::message::SetConfig::with(storage))
227+
.call(storage::message::SetConfig::with(storage.clone()))
229228
.await?;
230229
}
231230

@@ -277,10 +276,7 @@ impl MessageHandler<message::RunAction> for Service {
277276
#[async_trait]
278277
impl MessageHandler<message::GetStorageModel> for Service {
279278
/// It returns the storage model.
280-
async fn handle(
281-
&mut self,
282-
_message: message::GetStorageModel,
283-
) -> Result<Option<Box<RawValue>>, Error> {
279+
async fn handle(&mut self, _message: message::GetStorageModel) -> Result<Option<Value>, Error> {
284280
Ok(self.storage.call(storage::message::GetConfigModel).await?)
285281
}
286282
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use axum::{
4040
};
4141
use hyper::StatusCode;
4242
use serde::Serialize;
43-
use serde_json::{json, value::RawValue, Value};
43+
use serde_json::{json, Value};
4444

4545
#[derive(thiserror::Error, Debug)]
4646
pub enum Error {
@@ -352,9 +352,7 @@ async fn run_action(
352352
(status = 400, description = "Not possible to retrieve the storage model.")
353353
)
354354
)]
355-
async fn get_storage_model(
356-
State(state): State<ServerState>,
357-
) -> ServerResult<Json<Option<Box<RawValue>>>> {
355+
async fn get_storage_model(State(state): State<ServerState>) -> ServerResult<Json<Option<Value>>> {
358356
let model = state.manager.call(message::GetStorageModel).await?;
359357
Ok(Json(model))
360358
}
@@ -371,7 +369,7 @@ async fn get_storage_model(
371369
)]
372370
async fn set_storage_model(
373371
State(state): State<ServerState>,
374-
Json(model): Json<Box<RawValue>>,
372+
Json(model): Json<Value>,
375373
) -> ServerResult<()> {
376374
state
377375
.manager

rust/agama-storage/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ zbus = "5.7.1"
1212
tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread", "sync"] }
1313
tokio-stream = "0.1.16"
1414
serde = { version = "1.0.228" }
15-
serde_json = { version = "1.0.140", features = ["raw_value"] }
15+
serde_json = "1.0.140"

rust/agama-storage/src/client.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
//! Implements a client to access Agama's storage service.
2222
23-
use crate::config::Config;
24-
use agama_utils::api::Issue;
25-
use serde_json::{value::RawValue, Value};
23+
use agama_utils::api::{storage::Config, Issue};
24+
use serde_json::Value;
2625
use zbus::{names::BusName, zvariant::OwnedObjectPath, Connection, Message};
2726

2827
const SERVICE_NAME: &str = "org.opensuse.Agama.Storage1";
@@ -72,7 +71,7 @@ impl Client {
7271
Ok(())
7372
}
7473

75-
pub async fn get_system(&self) -> Result<Option<Box<RawValue>>, Error> {
74+
pub async fn get_system(&self) -> Result<Option<Value>, Error> {
7675
let message = self.call("GetSystem", &()).await?;
7776
try_from_message(message)
7877
}
@@ -82,12 +81,12 @@ impl Client {
8281
try_from_message(message)
8382
}
8483

85-
pub async fn get_config_model(&self) -> Result<Option<Box<RawValue>>, Error> {
84+
pub async fn get_config_model(&self) -> Result<Option<Value>, Error> {
8685
let message = self.call("GetConfigModel", &()).await?;
8786
try_from_message(message)
8887
}
8988

90-
pub async fn get_proposal(&self) -> Result<Option<Box<RawValue>>, Error> {
89+
pub async fn get_proposal(&self) -> Result<Option<Value>, Error> {
9190
let message = self.call("GetProposal", &()).await?;
9291
try_from_message(message)
9392
}
@@ -104,21 +103,18 @@ impl Client {
104103
}
105104

106105
pub async fn set_config(&self, config: Option<Config>) -> Result<(), Error> {
107-
let config = config.filter(|c| c.is_some());
106+
let config = config.filter(|c| c.has_value());
108107
let json = serde_json::to_string(&config)?;
109108
self.call("SetConfig", &(json)).await?;
110109
Ok(())
111110
}
112111

113-
pub async fn set_config_model(&self, model: Box<RawValue>) -> Result<(), Error> {
112+
pub async fn set_config_model(&self, model: Value) -> Result<(), Error> {
114113
self.call("SetConfigModel", &(model.to_string())).await?;
115114
Ok(())
116115
}
117116

118-
pub async fn solve_config_model(
119-
&self,
120-
model: Box<RawValue>,
121-
) -> Result<Option<Box<RawValue>>, Error> {
117+
pub async fn solve_config_model(&self, model: Value) -> Result<Option<Value>, Error> {
122118
let message = self.call("SolveConfigModel", &(model.to_string())).await?;
123119
try_from_message(message)
124120
}
@@ -145,19 +141,11 @@ impl Client {
145141
fn try_from_message<T: serde::de::DeserializeOwned + Default>(
146142
message: Message,
147143
) -> Result<T, Error> {
148-
let json: String = message.body().deserialize()?;
149-
if is_json_null(&json) {
144+
let raw_json: String = message.body().deserialize()?;
145+
let json: Value = serde_json::from_str(&raw_json)?;
146+
if json.is_null() {
150147
return Ok(T::default());
151148
}
152-
let value = serde_json::from_str(&json)?;
149+
let value = serde_json::from_value(json)?;
153150
Ok(value)
154151
}
155-
156-
fn is_json_null(value: &str) -> bool {
157-
let value = serde_json::from_str::<Value>(value);
158-
match value {
159-
Ok(Value::Null) => true,
160-
Ok(_) => false,
161-
Err(_) => false,
162-
}
163-
}

rust/agama-storage/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ pub use start::start;
2424
pub mod service;
2525
pub use service::Service;
2626

27-
mod config;
28-
pub use config::Config;
29-
3027
mod client;
3128
pub mod message;
3229
mod monitor;

rust/agama-storage/src/message.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21-
use crate::config::Config;
22-
use agama_utils::{actor::Message, api::Issue};
23-
use serde_json::value::RawValue;
21+
use agama_utils::{
22+
actor::Message,
23+
api::{storage::Config, Issue},
24+
};
25+
use serde_json::Value;
2426

2527
#[derive(Clone)]
2628
pub struct Activate;
@@ -54,7 +56,7 @@ impl Message for Finish {
5456
pub struct GetSystem;
5557

5658
impl Message for GetSystem {
57-
type Reply = Option<Box<RawValue>>;
59+
type Reply = Option<Value>;
5860
}
5961

6062
#[derive(Clone)]
@@ -68,14 +70,14 @@ impl Message for GetConfig {
6870
pub struct GetConfigModel;
6971

7072
impl Message for GetConfigModel {
71-
type Reply = Option<Box<RawValue>>;
73+
type Reply = Option<Value>;
7274
}
7375

7476
#[derive(Clone)]
7577
pub struct GetProposal;
7678

7779
impl Message for GetProposal {
78-
type Reply = Option<Box<RawValue>>;
80+
type Reply = Option<Value>;
7981
}
8082

8183
#[derive(Clone)]
@@ -123,11 +125,11 @@ impl Message for SetConfig {
123125

124126
#[derive(Clone)]
125127
pub struct SetConfigModel {
126-
pub model: Box<RawValue>,
128+
pub model: Value,
127129
}
128130

129131
impl SetConfigModel {
130-
pub fn new(model: Box<RawValue>) -> Self {
132+
pub fn new(model: Value) -> Self {
131133
Self { model }
132134
}
133135
}
@@ -138,17 +140,17 @@ impl Message for SetConfigModel {
138140

139141
#[derive(Clone)]
140142
pub struct SolveConfigModel {
141-
pub model: Box<RawValue>,
143+
pub model: Value,
142144
}
143145

144146
impl SolveConfigModel {
145-
pub fn new(model: Box<RawValue>) -> Self {
147+
pub fn new(model: Value) -> Self {
146148
Self { model }
147149
}
148150
}
149151

150152
impl Message for SolveConfigModel {
151-
type Reply = Option<Box<RawValue>>;
153+
type Reply = Option<Value>;
152154
}
153155

154156
#[derive(Clone)]

rust/agama-storage/src/service.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020

2121
use crate::{
2222
client::{self, Client},
23-
config::Config,
2423
message,
2524
};
2625
use agama_utils::{
2726
actor::{self, Actor, Handler, MessageHandler},
28-
api::{Issue, Scope},
27+
api::{storage::Config, Issue, Scope},
2928
issue,
3029
};
3130
use async_trait::async_trait;
32-
use serde_json::value::RawValue;
31+
use serde_json::Value;
3332

3433
#[derive(thiserror::Error, Debug)]
3534
pub enum Error {
@@ -102,10 +101,7 @@ impl MessageHandler<message::Finish> for Service {
102101

103102
#[async_trait]
104103
impl MessageHandler<message::GetSystem> for Service {
105-
async fn handle(
106-
&mut self,
107-
_message: message::GetSystem,
108-
) -> Result<Option<Box<RawValue>>, Error> {
104+
async fn handle(&mut self, _message: message::GetSystem) -> Result<Option<Value>, Error> {
109105
self.client.get_system().await.map_err(|e| e.into())
110106
}
111107
}
@@ -119,20 +115,14 @@ impl MessageHandler<message::GetConfig> for Service {
119115

120116
#[async_trait]
121117
impl MessageHandler<message::GetConfigModel> for Service {
122-
async fn handle(
123-
&mut self,
124-
_message: message::GetConfigModel,
125-
) -> Result<Option<Box<RawValue>>, Error> {
118+
async fn handle(&mut self, _message: message::GetConfigModel) -> Result<Option<Value>, Error> {
126119
self.client.get_config_model().await.map_err(|e| e.into())
127120
}
128121
}
129122

130123
#[async_trait]
131124
impl MessageHandler<message::GetProposal> for Service {
132-
async fn handle(
133-
&mut self,
134-
_message: message::GetProposal,
135-
) -> Result<Option<Box<RawValue>>, Error> {
125+
async fn handle(&mut self, _message: message::GetProposal) -> Result<Option<Value>, Error> {
136126
self.client.get_proposal().await.map_err(|e| e.into())
137127
}
138128
}
@@ -169,10 +159,7 @@ impl MessageHandler<message::SetConfigModel> for Service {
169159
}
170160
#[async_trait]
171161
impl MessageHandler<message::SolveConfigModel> for Service {
172-
async fn handle(
173-
&mut self,
174-
message: message::SolveConfigModel,
175-
) -> Result<Option<Box<RawValue>>, Error> {
162+
async fn handle(&mut self, message: message::SolveConfigModel) -> Result<Option<Value>, Error> {
176163
self.client
177164
.solve_config_model(message.model)
178165
.await

rust/agama-utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition.workspace = true
88
agama-locale-data = { path = "../agama-locale-data" }
99
async-trait = "0.1.89"
1010
serde = { version = "1.0.228", features = ["derive"] }
11-
serde_json = { version = "1.0.140", features = ["raw_value"] }
11+
serde_json = "1.0.140"
1212
serde_with = "3.14.0"
1313
strum = { version = "0.27.2", features = ["derive"] }
1414
thiserror = "2.0.16"

0 commit comments

Comments
 (0)