Skip to content

Commit

Permalink
Merge pull request #20 from WyvernIXTL/main
Browse files Browse the repository at this point in the history
Add Display Trait and fix Example in Readme
  • Loading branch information
iiiii7d authored Sep 19, 2024
2 parents 0751ef0 + 161da79 commit 9936c6c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
43 changes: 30 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,35 @@ Licenses are retrieved by searching in the following order:
* Text from `spdx` with identifier in `Cargo.toml`

## Usage
### *Cargo*

```
cargo add license-retriever
cargo add --build license-retriever
```

### `build.rs`

```rust
// build.rs ==========
fn main() {
let config = license_retriever::Config {
// options...
..Config::default()
};
license_retriever::LicenseRetriever::from_config(&config)?.save_in_out_dir("licenses")?;
}

// main project ==========
fn main() {
let licenses = license_retriever::license_retriever_data!("licenses").unwrap(); // Vec<(Package, Vec<String>)>
}
use license_retriever::{Config, LicenseRetriever};

fn main() {
let config = Config {
// options...
..Config::default()
};
LicenseRetriever::from_config(&config).unwrap().save_in_out_dir("LICENSE-3RD-PARTY").unwrap();
}

```

### `main.rs`

```rust
use license_retriever;

fn main() {
let licenses = license_retriever::license_retriever_data!("LICENSE-3RD-PARTY").unwrap();
println!("{}", licenses);
}
```
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
collections::HashSet,
path::{Path, PathBuf},
fmt,
};

use cargo_metadata::{Metadata, MetadataCommand, Package};
Expand Down Expand Up @@ -249,6 +250,32 @@ impl IntoIterator for LicenseRetriever {
}
}

impl fmt::Display for LicenseRetriever {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
const SEPERATOR_WIDTH: usize = 80;
let separator: String = "=".repeat(SEPERATOR_WIDTH);

writeln!(f, "{}\n", separator)?;

for (package, license) in self.iter() {
writeln!(f, "Package: {}", package.name)?;
writeln!(f, "Authors:")?;
for author in package.authors.iter() {
writeln!(f, " - {}", author)?;
}
writeln!(f, "\n{}\n", separator)?;

for line in license.iter() {
writeln!(f, "{}", line)?;
}

writeln!(f, "{}\n", separator)?;
}

Ok(())
}
}

#[macro_export]
macro_rules! license_retriever_data {
($file_name:literal) => {
Expand Down

0 comments on commit 9936c6c

Please sign in to comment.