Skip to content

Commit

Permalink
Merge 694fe39 into 87cc5af
Browse files Browse the repository at this point in the history
  • Loading branch information
Benno committed Aug 4, 2022
2 parents 87cc5af + 694fe39 commit eb9d29c
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 19 deletions.
14 changes: 1 addition & 13 deletions .github/workflows/pr.yml
Expand Up @@ -40,18 +40,6 @@ jobs:
# Run Clippy.
- name: Clippy checks
run: cargo clippy --all-targets --all-features

check_pr_size:
if: "!startsWith(github.event.pull_request.title, 'Automated version bump')"
name: Check PR size doesn't break set limit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: '0'
- uses: maidsafe/pr_size_checker@v2
with:
max_lines_changed: 200

coverage:
if: "!startsWith(github.event.pull_request.title, 'Automated version bump')"
Expand Down Expand Up @@ -151,7 +139,7 @@ jobs:

# Run the tests.
- name: Cargo test
run: cargo test --release
run: cargo test --all-features --release

# Test publish using --dry-run.
test-publish:
Expand Down
12 changes: 12 additions & 0 deletions Cargo.toml
Expand Up @@ -9,6 +9,10 @@ license = "MIT OR BSD-3-Clause"
readme = "README.md"
repository = "https://github.com/maidsafe/xor_name"

[features]
# Serialize `XorName` into a hex string if serializing into human-readable format
serialize-hex = ["hex", "serde_test"]

[dependencies]
rand_core = "0.6.3"

Expand All @@ -26,6 +30,14 @@ rand_core = "0.6.3"
default-features = false
features = [ "derive" ]

[dependencies.serde_test]
version = "1"
optional = true

[dependencies.hex]
version = "0.4"
optional = true

[dev-dependencies]
bincode = "1.2.1"

Expand Down
32 changes: 32 additions & 0 deletions README.md
Expand Up @@ -5,6 +5,38 @@ XorName is an array that is useful for calculations in DHT
| [MaidSafe website](http://maidsafe.net) | [SAFE Network Forum](https://safenetforum.org/) |
|:-------:|:-------:|

## Serialization

`XorName` and `Prefix` can be serialized into a human-readable hex string, instead of as a `u8` array. To enable this, activate the `serialize-hex` feature. This also allows for these structures to be serialised when used as a key in a map like `HashMap`, because most formats only allow keys to be strings, instead of more complex types.

A struct like this:
```rust
#[derive(Serialize, Deserialize)]
struct MyStruct {
prefix: Prefix,
xor_name: XorName,
}
```

Will yield this JSON
```json
{
"prefix": "8a817b6d791f4b00000000000000000000000000000000000000000000000000/56",
"xor_name": "8a817b6d791f4bae4117ac7ae15a88cd2c62fba0b040972ce885f1a47625dea1"
}
```

instead of
```json
{
"prefix": {
"bit_count": 56,
"name": [141,199,202,57,183,222,153,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
},
"xor_name": [141,199,202,57,183,222,153,14,185,67,253,100,133,71,118,221,133,170,130,195,58,66,105,105,60,87,179,110,7,73,237,143]
}
```

## License

This SAFE Network library is dual-licensed under the Modified BSD ([LICENSE-BSD](LICENSE-BSD) https://opensource.org/licenses/BSD-3-Clause) or the MIT license ([LICENSE-MIT](LICENSE-MIT) https://opensource.org/licenses/MIT) at your option.
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Expand Up @@ -57,7 +57,6 @@ use core::{cmp::Ordering, fmt, ops};
pub use prefix::Prefix;
pub use rand;
use rand::distributions::{Distribution, Standard};
use serde::{Deserialize, Serialize};
use tiny_keccak::{Hasher, Sha3};

/// Creates XorName with the given leading bytes and the rest filled with zeroes.
Expand Down Expand Up @@ -91,6 +90,8 @@ macro_rules! format {
}

mod prefix;
#[cfg(feature = "serialize-hex")]
mod serialize;

/// Constant byte length of `XorName`.
pub const XOR_NAME_LEN: usize = 32;
Expand All @@ -103,7 +104,11 @@ pub const XOR_NAME_LEN: usize = 32;
/// i. e. the points with IDs `x` and `y` are considered to have distance `x xor y`.
///
/// [1]: https://en.wikipedia.org/wiki/Kademlia#System_details
#[derive(Eq, Copy, Clone, Default, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[derive(Eq, Copy, Clone, Default, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
not(feature = "serialize-hex"),
derive(serde::Serialize, serde::Deserialize)
)]
pub struct XorName(pub [u8; XOR_NAME_LEN]);

impl XorName {
Expand Down
11 changes: 7 additions & 4 deletions src/prefix.rs
Expand Up @@ -15,14 +15,17 @@ use core::{
ops::RangeInclusive,
str::FromStr,
};
use serde::{Deserialize, Serialize};

/// A section prefix, i.e. a sequence of bits specifying the part of the network's name space
/// consisting of all names that start with this sequence.
#[derive(Clone, Copy, Default, Eq, Deserialize, Serialize)]
#[derive(Clone, Copy, Default, Eq)]
#[cfg_attr(
not(feature = "serialize-hex"),
derive(serde::Serialize, serde::Deserialize)
)]
pub struct Prefix {
bit_count: u16,
name: XorName,
pub(crate) bit_count: u16,
pub(crate) name: XorName,
}

impl Prefix {
Expand Down

0 comments on commit eb9d29c

Please sign in to comment.