Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
new torrust-serde-bencode package
Browse files Browse the repository at this point in the history
Minor changes and package configuration changed.
  • Loading branch information
josecelano committed Sep 25, 2023
1 parent 553adb4 commit 625081a
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 34 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Testing

on:
push:
pull_request:

env:
CARGO_TERM_COLOR: always

jobs:
unit:
name: Units
runs-on: ubuntu-latest

strategy:
matrix:
toolchain: [stable, nightly]

steps:
- id: checkout
name: Checkout Repository
uses: actions/checkout@v3

- id: setup
name: Setup Toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.toolchain }}
components: llvm-tools-preview

- id: cache
name: Enable Job Cache
uses: Swatinem/rust-cache@v2

- id: test
name: Run Unit Tests
run: cargo test --tests --benches --examples --workspace --all-targets --all-features

6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"streetsidesoftware.code-spell-checker",
"rust-lang.rust-analyzer"
]
}
26 changes: 26 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"[rust]": {
"editor.formatOnSave": true
},
"rust-analyzer.checkOnSave": true,
"rust-analyzer.check.command": "clippy",
"rust-analyzer.check.allTargets": true,
"rust-analyzer.check.extraArgs": [
"--",
"-D",
"clippy::correctness",
"-D",
"clippy::suspicious",
"-W",
"clippy::complexity",
"-W",
"clippy::perf",
"-W",
"clippy::style",
"-W",
"clippy::pedantic",
],
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
],
}
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "serde_bencode"
name = "torrust-serde-bencode"
description = "A Serde backed Bencode encoding/decoding library for Rust."
version = "0.2.3"
authors = ["Toby Padilla <tobypadilla@gmail.com>"]
repository = "https://github.com/toby/serde-bencode"
documentation = "https://docs.rs/serde_bencode/"
authors = ["Toby Padilla <tobypadilla@gmail.com>", "Nautilus Cyberneering <info@nautilus-cyberneering.de>"]
repository = "https://github.com/torrust/torrust-serde-bencode"
documentation = "https://docs.rs/torrust-serde-bencode/"
license = "MIT"
keywords = ["bencode", "serialize", "deserialize", "serde"]
edition = "2018"
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# serde-bencode
# Torrust Serde Bencode

A [Serde](https://github.com/serde-rs/serde) backed [Bencode](https://en.wikipedia.org/wiki/Bencode)
encoding/decoding library for Rust.

Forked from: <https://github.com/toby/serde-bencode> due to inactivity in upstream repo.

## Installation

Add the following to your `Cargo.toml`:

```toml
[dependencies]
serde_bencode = "^0.2.3"
torrust-serde-bencode = "^0.2.3"
serde = "^1.0.0"
serde_derive = "^1.0.0"
```

## Usage

This is an abbreviated `.torrent` parsing example from
[examples/parse_torrent.rs](examples/parse_torrent.rs). If you compile this crate as a binary, it
will print metadata for any Torrent sent to stdin.
This is an abbreviated `.torrent` parsing example from [examples/parse_torrent.rs](examples/parse_torrent.rs). If you compile this crate as a binary, it will print metadata for any Torrent sent to stdin.
18 changes: 10 additions & 8 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#![feature(test)]

extern crate test;
extern crate serde;
extern crate test;
#[macro_use]
extern crate serde_derive;
extern crate serde_bencode;
extern crate torrust_serde_bencode;

use test::Bencher;
use serde::Serialize;
use serde_bencode::ser::Serializer;
use serde_bencode::de::from_bytes;

use test::Bencher;
use torrust_serde_bencode::de::from_bytes;
use torrust_serde_bencode::ser::Serializer;

#[bench]
fn ser_de_simple(b: &mut Bencher) {
Expand All @@ -21,7 +20,7 @@ fn ser_de_simple(b: &mut Bencher) {
}

b.iter(|| {
let a = Fake {a: 2, b: 7};
let a = Fake { a: 2, b: 7 };
let mut ser = Serializer::new();
a.serialize(&mut ser).unwrap();
let b: Fake = from_bytes(ser.as_ref()).unwrap();
Expand All @@ -44,7 +43,10 @@ fn ser_de_nested(b: &mut Bencher) {
}

b.iter(|| {
let a = FakeB {a: 2, b: FakeA {a: 7, b: 9}};
let a = FakeB {
a: 2,
b: FakeA { a: 7, b: 9 },
};
let mut ser = Serializer::new();
a.serialize(&mut ser).unwrap();
let b: FakeB = from_bytes(ser.as_ref()).unwrap();
Expand Down
11 changes: 11 additions & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"words": [
"bencoded",
"Serde"
],
"enableFiletypes": [
"dockerfile",
"shellscript",
"toml"
]
}
22 changes: 11 additions & 11 deletions examples/parse_torrent.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extern crate serde;
extern crate serde_bencode;
extern crate torrust_serde_bencode;
#[macro_use]
extern crate serde_derive;
extern crate serde_bytes;

use serde_bencode::de;
use serde_bytes::ByteBuf;
use std::io::{self, Read};
use torrust_serde_bencode::de;

#[derive(Debug, Deserialize)]
struct Node(String, i64);
Expand All @@ -21,23 +21,23 @@ struct File {

#[derive(Debug, Deserialize)]
struct Info {
name: String,
pieces: ByteBuf,
pub name: String,
pub pieces: ByteBuf,
#[serde(rename = "piece length")]
piece_length: i64,
pub piece_length: i64,
#[serde(default)]
md5sum: Option<String>,
pub md5sum: Option<String>,
#[serde(default)]
length: Option<i64>,
pub length: Option<i64>,
#[serde(default)]
files: Option<Vec<File>>,
pub files: Option<Vec<File>>,
#[serde(default)]
private: Option<u8>,
pub private: Option<u8>,
#[serde(default)]
path: Option<Vec<String>>,
pub path: Option<Vec<String>>,
#[serde(default)]
#[serde(rename = "root hash")]
root_hash: Option<String>,
pub root_hash: Option<String>,
}

#[derive(Debug, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io::Read;
use std::str;

#[doc(hidden)]
// TODO: This should be pub(crate).
// todo: This should be pub(crate).
pub struct BencodeAccess<'a, R: 'a + Read> {
de: &'a mut Deserializer<R>,
len: Option<usize>,
Expand Down
10 changes: 5 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
extern crate serde_bencode;
extern crate torrust_serde_bencode;

use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_bencode::de::{from_bytes, from_str};
use serde_bencode::error::Result;
use serde_bencode::ser::{to_bytes, to_string, Serializer};
use serde_bencode::value::Value;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use torrust_serde_bencode::de::{from_bytes, from_str};
use torrust_serde_bencode::error::Result;
use torrust_serde_bencode::ser::{to_bytes, to_string, Serializer};
use torrust_serde_bencode::value::Value;

fn test_value_ser_de<T: Into<Value>>(a: T) {
let a = a.into();
Expand Down

0 comments on commit 625081a

Please sign in to comment.