Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Fix codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteJanz committed Jun 29, 2024
1 parent 7a32696 commit fab075a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 85 deletions.
11 changes: 8 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ impl SwClient {
let credentials = Arc::new(credentials);
let auth_response = Self::authenticate(&client, credentials.as_ref()).await?;

println!("Shopware API client with in_flight_limit={} created and authenticated", in_flight_limit);
println!(
"Shopware API client with in_flight_limit={} created and authenticated",
in_flight_limit
);
Ok(Self {
client,
in_flight_semaphore: Arc::new(Semaphore::new(in_flight_limit)),
Expand Down Expand Up @@ -67,7 +70,8 @@ impl SwClient {
let response = {
let _lock = self.in_flight_semaphore.acquire().await.unwrap();
let start_instant = Instant::now();
let res = self.client
let res = self
.client
.post(format!("{}/api/_action/sync", self.credentials.base_url))
.bearer_auth(access_token)
.header("single-operation", 1)
Expand Down Expand Up @@ -184,7 +188,8 @@ impl SwClient {
let response = {
let _lock = self.in_flight_semaphore.acquire().await.unwrap();
let start_instant = Instant::now();
let res = self.client
let res = self
.client
.post(format!(
"{}/api/search/{}",
self.credentials.base_url, entity
Expand Down
191 changes: 109 additions & 82 deletions src/data/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,24 @@ impl EntityPath for Entity {
return;
}

let mut pointer = pointer.as_object_mut().expect("insert_by_path lead to non object");
let mut pointer = pointer
.as_object_mut()
.expect("insert_by_path lead to non object");
while let Some(token) = tokens.next() {
if tokens.peek().is_none() {
// simply insert the value
pointer.insert(token.to_string(), value);
return;
}

pointer = pointer.entry(token).or_insert_with(|| {
let child = Entity::with_capacity(1);
serde_json::Value::Object(child)
}).as_object_mut().expect("insert_by_path lead to non object");
pointer = pointer
.entry(token)
.or_insert_with(|| {
let child = Entity::with_capacity(1);
serde_json::Value::Object(child)
})
.as_object_mut()
.expect("insert_by_path lead to non object");
}
}
}
Expand Down Expand Up @@ -380,103 +386,124 @@ mod tests {
};

entity.insert_by_path("child.bar", json!("hello"));
assert_eq!(Value::Object(entity.clone()), json!({
"fiz": "buz",
"child": {
"bar": "hello",
},
}));
assert_eq!(
Value::Object(entity.clone()),
json!({
"fiz": "buz",
"child": {
"bar": "hello",
},
})
);

entity.insert_by_path("another.nested.child.value", json!(42));
assert_eq!(Value::Object(entity.clone()), json!({
"fiz": "buz",
"child": {
"bar": "hello",
},
"another": {
"nested": {
"child": {
"value": 42,
assert_eq!(
Value::Object(entity.clone()),
json!({
"fiz": "buz",
"child": {
"bar": "hello",
},
"another": {
"nested": {
"child": {
"value": 42,
},
},
},
},
}));
})
);

entity.insert_by_path("fiz", json!(42));
assert_eq!(Value::Object(entity.clone()), json!({
"fiz": 42,
"child": {
"bar": "hello",
},
"another": {
"nested": {
"child": {
"value": 42,
assert_eq!(
Value::Object(entity.clone()),
json!({
"fiz": 42,
"child": {
"bar": "hello",
},
"another": {
"nested": {
"child": {
"value": 42,
},
},
},
},
}));
})
);

entity.insert_by_path("child.bar", json!("buz"));
assert_eq!(Value::Object(entity.clone()), json!({
"fiz": 42,
"child": {
"bar": "buz",
},
"another": {
"nested": {
"child": {
"value": 42,
assert_eq!(
Value::Object(entity.clone()),
json!({
"fiz": 42,
"child": {
"bar": "buz",
},
"another": {
"nested": {
"child": {
"value": 42,
},
},
},
},
}));
})
);

entity.insert_by_path("child.hello", json!("world"));
assert_eq!(Value::Object(entity.clone()), json!({
"fiz": 42,
"child": {
"bar": "buz",
"hello": "world",
},
"another": {
"nested": {
"child": {
"value": 42,
assert_eq!(
Value::Object(entity.clone()),
json!({
"fiz": 42,
"child": {
"bar": "buz",
"hello": "world",
},
"another": {
"nested": {
"child": {
"value": 42,
},
},
},
},
}));
})
);

entity.insert_by_path("another.nested.sibling", json!({"type": "cousin"}));
assert_eq!(Value::Object(entity.clone()), json!({
"fiz": 42,
"child": {
"bar": "buz",
"hello": "world",
},
"another": {
"nested": {
"child": {
"value": 42,
},
"sibling": {
"type": "cousin",
assert_eq!(
Value::Object(entity.clone()),
json!({
"fiz": 42,
"child": {
"bar": "buz",
"hello": "world",
},
"another": {
"nested": {
"child": {
"value": 42,
},
"sibling": {
"type": "cousin",
},
},
},
},
}));
})
);

entity.insert_by_path("another.nested", json!("replaced"));
assert_eq!(Value::Object(entity.clone()), json!({
"fiz": 42,
"child": {
"bar": "buz",
"hello": "world",
},
"another": {
"nested": "replaced"
},
}));
assert_eq!(
Value::Object(entity.clone()),
json!({
"fiz": 42,
"child": {
"bar": "buz",
"hello": "world",
},
"another": {
"nested": "replaced"
},
})
);
}
}

0 comments on commit fab075a

Please sign in to comment.