Skip to content

Commit

Permalink
Merge pull request #13 from Danieroner/overall-improvement
Browse files Browse the repository at this point in the history
Overall improvement
  • Loading branch information
Danieroner authored Apr 10, 2024
2 parents bcd83bd + e1deeba commit 73d8f11
Show file tree
Hide file tree
Showing 18 changed files with 868 additions and 878 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tinify-rs"
version = "1.3.0"
version = "1.4.0"
edition = "2021"
description = "A Rust client for the Tinify API"
authors = ["The tinify-rs Developers"]
Expand All @@ -18,6 +18,7 @@ tokio = { version = "1", features = ["full"], optional = true}
serde = { version = "1.0.149", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.89", default-features = false, features = ["alloc"] }
serde_derive = "1.0.149"
url = "2.5.0"

[dev-dependencies]
dotenv = "0.15.0"
Expand Down
59 changes: 39 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Tinify API client for Rust 🦀

<p align="center">
<img src="https://tinypng.com/images/panda-happy.png" alt="Tinify"/>
</p>
</p>
<p align="center">
<a href="https://github.com/Danieroner/tinify-rs/actions">
<img alt="CI Status" src="https://github.com/Danieroner/tinify-rs/actions/workflows/ci.yml/badge.svg" />
Expand Down Expand Up @@ -44,14 +40,14 @@ Install the API client with Cargo. Add this to `Cargo.toml`:

```toml
[dependencies]
tinify-rs = "1.3.0"
tinify-rs = "1.4.0"
```

Using async client

```toml
[dependencies]
tinify-rs = { version = "1.3.0", features = ["async"] }
tinify-rs = { version = "1.4.0", features = ["async"] }
```

## Usage
Expand All @@ -62,38 +58,61 @@ tinify-rs = { version = "1.3.0", features = ["async"] }

- Compress from a file
```rust
use tinify::sync::Tinify;
use tinify::error::TinifyError;
use tinify::sync::Tinify;
use std::path::Path;

fn main() -> Result<(), TinifyError> {
let key = "tinify api key";
let key = "api key";
let output = Path::new("./optimized.jpg");
let tinify = Tinify::new().set_key(key);
let client = tinify.get_client()?;
let _ = client
let optimized = tinify
.get_client()?
.from_file("./unoptimized.jpg")?
.to_file("./optimized.jpg")?;

.to_file(output);

if let Err(error) = optimized {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}

```

- Compress from a file async
```rust
use tinify::async_bin::Tinify as AsyncTinify;
use tinify::error::TinifyError;
use tinify::async_bin::Tinify;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), TinifyError> {
let key = "tinify api key";
let tinify = AsyncTinify::new().set_key(key);
let client = tinify.get_async_client()?;
client
.from_file("./unoptimized.jpg")
.await?
.to_file("./optimized.jpg")?;
let key = "api key";
let output = Path::new("./optimized.jpg");
let tinify = Tinify::new().set_key(key);
let compressed = tinify
.get_async_client()?
.from_file("./unoptimized.jpg").await?
.to_file(output).await;

if let Err(error) = compressed {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}

```

## Running tests
Expand Down
36 changes: 36 additions & 0 deletions examples/async/convert_transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use tinify::error::TinifyError;
use tinify::transform::Transform;
use tinify::async_bin::Tinify;
use tinify::convert::Convert;
use tinify::convert::Type;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), TinifyError> {
let key = "api key";
let convert = Convert {
r#type: vec![Type::Jpeg],
};
let transform = Transform {
background: "#800020".to_string(),
};
let output = Path::new("./optimized.jpg");
let tinify = Tinify::new().set_key(key);
let optimized = tinify
.get_async_client()?
.from_url("https://tinypng.com/images/panda-happy.png").await?
.convert(convert)?
.transform(transform)?
.to_file(output).await;

if let Err(error) = optimized {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}
25 changes: 25 additions & 0 deletions examples/async/optimize_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use tinify::error::TinifyError;
use tinify::async_bin::Tinify;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), TinifyError> {
let key = "api key";
let output = Path::new("./optimized.jpg");
let tinify = Tinify::new().set_key(key);
let optimized = tinify
.get_async_client()?
.from_file("./unoptimized.jpg").await?
.to_file(output).await;

if let Err(error) = optimized {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}
33 changes: 33 additions & 0 deletions examples/async/resize_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use tinify::error::TinifyError;
use tinify::async_bin::Tinify;
use tinify::resize::Resize;
use tinify::resize::Method;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), TinifyError> {
let key = "api key";
let output = Path::new("./optimized.jpg");
let resize = Resize {
method: Method::Fit,
width: Some(150),
height: Some(100),
};
let tinify = Tinify::new().set_key(key);
let optimized = tinify
.get_async_client()?
.from_file("./unoptimized.jpg").await?
.resize(resize)?
.to_file(output).await;

if let Err(error) = optimized {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}
35 changes: 35 additions & 0 deletions examples/sync/convert_transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use tinify::error::TinifyError;
use tinify::transform::Transform;
use tinify::convert::Convert;
use tinify::convert::Type;
use tinify::sync::Tinify;
use std::path::Path;

fn main() -> Result<(), TinifyError> {
let key = "api key";
let convert = Convert {
r#type: vec![Type::Jpeg],
};
let transform = Transform {
background: "#800020".to_string(),
};
let output = Path::new("./optimized.jpg");
let tinify = Tinify::new().set_key(key);
let optimized = tinify
.get_client()?
.from_url("https://tinypng.com/images/panda-happy.png")?
.convert(convert)?
.transform(transform)?
.to_file(output);

if let Err(error) = optimized {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}
24 changes: 24 additions & 0 deletions examples/sync/optimize_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use tinify::error::TinifyError;
use tinify::sync::Tinify;
use std::path::Path;

fn main() -> Result<(), TinifyError> {
let key = "api key";
let output = Path::new("./optimized.jpg");
let tinify = Tinify::new().set_key(key);
let optimized = tinify
.get_client()?
.from_file("./unoptimized.jpg")?
.to_file(output);

if let Err(error) = optimized {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}
32 changes: 32 additions & 0 deletions examples/sync/resize_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use tinify::error::TinifyError;
use tinify::resize::Method;
use tinify::resize::Resize;
use tinify::sync::Tinify;
use std::path::Path;

fn main() -> Result<(), TinifyError> {
let key = "api key";
let resize = Resize {
method: Method::Fit,
width: Some(150),
height: Some(100),
};
let output = Path::new("./optimized.jpg");
let tinify = Tinify::new().set_key(key);
let optimized = tinify
.get_client()?
.from_file("./unoptimized.jpg")?
.resize(resize)?
.to_file(output);

if let Err(error) = optimized {
match error {
TinifyError::ClientError { ref upstream } => {
println!("Error: {} message: {}", upstream.error, upstream.message);
}
_ => println!("{:?}", error),
}
}

Ok(())
}
Loading

0 comments on commit 73d8f11

Please sign in to comment.