Skip to content

Commit 1f60fb8

Browse files
committed
allocator parsing model deserialization
1 parent f6b37d5 commit 1f60fb8

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

fplus-http-server/src/router/allocator.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,24 @@ pub async fn allocators() -> impl Responder {
3131
#[post("/allocator/create")]
3232
pub async fn create_from_json(file: web::Json<ChangedAllocator>) -> actix_web::Result<impl Responder> {
3333
let file_name = &file.file_changed;
34+
log::info!("File name: {}", file_name);
3435

3536
match process_allocator_file(file_name).await {
3637
Ok(model) => {
37-
if model.multisig_address.is_empty() {
38+
if model.address.is_empty() {
3839
return Ok(HttpResponse::BadRequest().body("Missing or invalid multisig_address"));
3940
}
40-
let verifiers_gh_handles = if model.verifiers.is_empty() {
41+
let verifiers_gh_handles = if model.application.github_handles.is_empty() {
4142
None
4243
} else {
43-
Some(model.verifiers.join(", ")) // Join verifiers in a string if exists
44+
Some(model.application.github_handles.join(", ")) // Join verifiers in a string if exists
4445
};
4546

4647
match database::create_or_update_allocator(
4748
model.organization,
4849
model.slug,
49-
Some(model.installation_id as i64),
50-
Some(model.multisig_address),
50+
Some(model.common_ui_install_id as i64),
51+
Some(model.address),
5152
verifiers_gh_handles,
5253
).await {
5354
Ok(allocator_model) => Ok(HttpResponse::Ok().json(allocator_model)),

fplus-lib/src/core/allocator/file.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
use serde::{Deserialize, Serialize};
1+
use serde::{Serialize, Deserialize};
22

33
#[derive(Serialize, Deserialize, Debug)]
44
pub struct AllocatorModel {
5-
pub slug: String,
5+
pub slug: String,
66
pub organization: String,
7-
pub multisig_address: String,
8-
pub verifiers: Vec<String>,
9-
pub installation_id: u64,
7+
pub address: String,
8+
pub application: Application,
9+
pub common_ui_install_id: u64,
10+
}
11+
12+
#[derive(Serialize, Deserialize, Debug)]
13+
pub struct Application {
14+
pub github_handles: Vec<String>
1015
}

fplus-lib/src/core/allocator/mod.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub async fn process_allocator_file(file_name: &str) -> Result<AllocatorModel, L
1313
let path = file_name.to_string();
1414

1515
let gh = GithubWrapper::new(owner.to_string(), repo.to_string());
16-
16+
log::info!("Github is initialized");
1717
let content_items = gh.get_file(&path, branch).await.map_err(|e| LDNError::Load(e.to_string()))?;
1818
let model = content_items_to_allocator_model(content_items).map_err(|e| LDNError::Load(e.to_string()))?;
1919

@@ -22,13 +22,28 @@ pub async fn process_allocator_file(file_name: &str) -> Result<AllocatorModel, L
2222

2323

2424
fn content_items_to_allocator_model(file: ContentItems) -> Result<AllocatorModel, LDNError> {
25-
let encoded_content = file.items
26-
.get(0)
27-
.and_then(|f| f.content.clone())
28-
.ok_or(LDNError::Load("Allocator file is corrupted".to_string()))?;
29-
30-
let allocator_model = decode_allocator_model(&encoded_content.replace("\n", ""))
31-
.ok_or(LDNError::Load("Failed to parse allocator model".to_string()))?;
32-
33-
Ok(allocator_model)
25+
let encoded_content = match file.items.get(0).and_then(|f| f.content.clone()) {
26+
Some(content) => {
27+
log::info!("Fetched content: {:?}", content);
28+
content
29+
},
30+
None => {
31+
log::error!("Allocator file is corrupted or empty");
32+
return Err(LDNError::Load("Allocator file is corrupted".to_string()));
33+
}
34+
};
35+
36+
let cleaned_content = encoded_content.replace("\n", "");
37+
log::info!("Cleaned content: {:?}", cleaned_content);
38+
39+
match decode_allocator_model(&cleaned_content) {
40+
Some(model) => {
41+
log::info!("Parsed allocator model successfully");
42+
Ok(model)
43+
},
44+
None => {
45+
log::error!("Failed to parse allocator model");
46+
Err(LDNError::Load("Failed to parse allocator model".to_string()))
47+
}
48+
}
3449
}

0 commit comments

Comments
 (0)