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

Commit

Permalink
Update readme + slight optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteJanz committed Jul 7, 2024
1 parent 3d64a8b commit 05faf52
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary
# for performance profiling
# strip = false
# debug = true
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ to export data into (CSV) files or import data from (CSV) files.
## Installation

### With Cargo ([Rust toolchain](https://www.rust-lang.org/learn/get-started))

```bash
cargo install sw-sync-cli
```
Same command can be used for updates. See [crate](https://crates.io/crates/sw-sync-cli)

### Manual

head to [GitHub releases](https://github.com/MalteJanz/sw-sync-cli/releases) and download the right binary for your operating system.
Then either execute the binary directly or put it in your `PATH`.

### Build it from this repository

1. Clone this repository
2. Have the latest [Rust toolchain](https://www.rust-lang.org/learn/get-started) installed
3. Run `cargo build --release` inside the repository root folder
4. You will get your executable here `./target/release/sw-sync-cli`

## Usage

1. Set up an [integration](https://docs.shopware.com/en/shopware-6-en/settings/system/integrationen?category=shopware-6-en/settings/system) inside shopware.
Expand Down
6 changes: 3 additions & 3 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ impl SwClient {
}

async fn deserialize<T: for<'a> Deserialize<'a>>(response: Response) -> Result<T, SwApiError> {
let text = response.text().await?;
let bytes = response.bytes().await?;

match serde_json::from_str(&text) {
match serde_json::from_slice(&bytes) {
Ok(t) => Ok(t),
Err(_e) => {
let body: serde_json::Value = serde_json::from_str(&text)?;
let body: serde_json::Value = serde_json::from_slice(&bytes)?;
Err(SwApiError::DeserializeIntoSchema(
serde_json::to_string_pretty(&body)?,
))
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum Commands {
// #[arg(short, long, action = ArgAction::SetTrue)]
// verbose: bool,
/// How many requests can be "in-flight" at the same time
#[arg(short, long, default_value = "8")]
#[arg(short, long, default_value = "10")]
in_flight_limit: usize,
},
}
Expand Down
24 changes: 14 additions & 10 deletions src/data/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ pub fn deserialize_row(
let column_index = headers
.iter()
.position(|header| header == path_mapping.file_column)
.context(format!(
"Can't find column '{}' in CSV headers",
path_mapping.file_column
))?;
.with_context(|| {
format!(
"Can't find column '{}' in CSV headers",
path_mapping.file_column
)
})?;

let raw_value = row
.get(column_index)
Expand All @@ -56,10 +58,10 @@ pub fn serialize_entity(entity: Entity, context: &SyncContext) -> anyhow::Result
match mapping {
Mapping::ByPath(path_mapping) => {
let value = entity.get_by_path(&path_mapping.entity_path)
.context(format!(
.with_context(|| format!(
"could not get field path '{}' specified in mapping (you might try the optional chaining operator '?.' to fallback to null), entity attributes:\n{}",
path_mapping.entity_path,
serde_json::to_string_pretty(&entity).unwrap())
serde_json::to_string_pretty(&entity).unwrap()) // expensive for big entities
)?;

let value_str = match value {
Expand All @@ -72,10 +74,12 @@ pub fn serialize_entity(entity: Entity, context: &SyncContext) -> anyhow::Result
Mapping::ByScript(script_mapping) => {
let value = script_row
.get(script_mapping.key.as_str())
.context(format!(
"failed to retrieve script key '{}' of row",
script_mapping.key
))?;
.with_context(|| {
format!(
"failed to retrieve script key '{}' of row",
script_mapping.key
)
})?;
let value_str = serde_json::to_string(value)?;

row.push(value_str);
Expand Down
15 changes: 10 additions & 5 deletions src/data/transform/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::SyncContext;
use anyhow::Context;
use csv::StringRecord;
use rhai::packages::{BasicArrayPackage, CorePackage, MoreStringPackage, Package};
use rhai::{Engine, Position, Scope, AST};
use rhai::{Engine, OptimizationLevel, Position, Scope, AST};

#[derive(Debug)]
pub struct ScriptingEnvironment {
Expand Down Expand Up @@ -37,10 +37,9 @@ impl ScriptingEnvironment {
let column_index = headers
.iter()
.position(|h| h == mapping.file_column)
.context(format!(
"Can't find column '{}' in CSV headers",
mapping.file_column
))?;
.with_context(|| {
format!("Can't find column '{}' in CSV headers", mapping.file_column)
})?;

let value = row
.get(column_index)
Expand Down Expand Up @@ -78,7 +77,11 @@ impl ScriptingEnvironment {
};

let mut scope = Scope::new();

// this is potentially expensive for big entities!
// we might only want to pass some data into the script...
let script_entity = rhai::serde::to_dynamic(entity)?;

scope.push_dynamic("entity", script_entity);
let row_dynamic = rhai::Map::new();
scope.push("row", row_dynamic);
Expand Down Expand Up @@ -124,6 +127,8 @@ pub fn prepare_scripting_environment(

fn get_base_engine() -> Engine {
let mut engine = Engine::new_raw();
engine.set_optimization_level(OptimizationLevel::Full);

// Default print/debug implementations
engine.on_print(|text| println!("{text}"));
engine.on_debug(|text, source, pos| match (source, pos) {
Expand Down

0 comments on commit 05faf52

Please sign in to comment.