Skip to content

Commit

Permalink
refactor(network): replace handwritten diagrams with diagrams in asci…
Browse files Browse the repository at this point in the history
…iflow
  • Loading branch information
nerodono committed Jan 21, 2025
1 parent d146625 commit 418a5b6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 63 deletions.
114 changes: 57 additions & 57 deletions elfo-network/src/socket/capabilities/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,39 @@ use std::fmt;

use crate::config::Preference;

bitflags::bitflags! {
/// Set of algorithms. 24 bits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Algorithms: u32 {
const LZ4 = 1;
// NB: Shift by 2: `const ZSTD = 1 << 2;`.
}
}

/// Compression capabilities.
///
/// Layout:
/// ```text
/// Bits
/// 6 2
/// +---+-----+
/// | R | Lz4 |
/// +---+-----+
/// 22 2
/// ┌────────────┬─────┐
/// │ Reserved │ Lz4 │
/// └────────────┴─────┘
/// ```
///
/// `R` - reserved, any other mean specific compression algorithm. Layout
/// for specific compression algorithm:
/// Each mentioned algorithm here occupies two bits for a reason, here's the
/// layout of those bits:
/// ```text
/// Bits
/// 1 1
/// +---+---+
/// | S | P |
/// +---+---+
/// 1 1
/// ┌───────────┬───────────┐
/// │ Supported │ Preferred │
/// └───────────┴───────────┘
/// ```
///
/// 1. `S` - the compression algorithm is supported.
/// 2. `P` - the compression algorithm is preferred, implies `S`.
/// 1. Supported - the compression algorithm is supported.
/// 2. Preferred - the compression algorithm is preferred, implies `Supported`.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Compression(u32);

fn write_array(
hide: Option<Algorithms>,
algos: Algorithms,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
write!(f, "[")?;
let mut need_comma = false;
for (name, _) in algos
.iter_names()
.filter(|(_, algo)| hide.map_or(true, |hide| hide.contains(*algo)))
{
if need_comma {
write!(f, ", ")?;
}

f.write_str(name)?;
need_comma = true;
}

write!(f, "]")
}

impl fmt::Display for Compression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let preferred = self.preferred();
let supported = self.supported();

write!(f, "(preferred: ")?;
write_array(None, preferred, f)?;
write!(f, ", supported: ")?;
// Don't show preferred in supported, more compact
// output.
write_array(Some(preferred), supported, f)?;
write!(f, ")")
}
}

impl Compression {
pub(crate) const fn empty() -> Self {
Self::new(Algorithms::empty(), Algorithms::empty())
Expand Down Expand Up @@ -147,11 +119,39 @@ impl Compression {
}
}

bitflags::bitflags! {
// Actually, 24 bits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Algorithms: u32 {
const LZ4 = 1;
// NB: Shift by 2: `const ZSTD = 1 << 2;`.
impl fmt::Display for Compression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn write_array(
hide: Option<Algorithms>,
algos: Algorithms,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
write!(f, "[")?;
let mut need_comma = false;
for (name, _) in algos
.iter_names()
.filter(|(_, algo)| hide.map_or(true, |hide| hide.contains(*algo)))
{
if need_comma {
write!(f, ", ")?;
}

f.write_str(name)?;
need_comma = true;
}

write!(f, "]")
}

let preferred = self.preferred();
let supported = self.supported();

write!(f, "(preferred: ")?;
write_array(None, preferred, f)?;
write!(f, ", supported: ")?;
// Don't show preferred in supported, more compact
// output.
write_array(Some(preferred), supported, f)?;
write!(f, ")")
}
}
24 changes: 18 additions & 6 deletions elfo-network/src/socket/capabilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ use self::compression::Compression;

pub(crate) mod compression;

/// Things supported by the node.
///
/// ### Layout
///
/// ```text
/// 24 bits 8 bits
/// ┌─────────────────────┬──────────────────┐
/// │ Compression │ Reserved │
/// └─────────────────────┴──────────────────┘
/// ```
///
/// 1. [`Compression`] - compression capabilities.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Capabilities(u32);

impl fmt::Display for Capabilities {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(compression: {})", self.compression())
}
}

impl Capabilities {
pub(crate) const fn new(compression: Compression) -> Self {
let compression = compression.bits() as u32;
Expand Down Expand Up @@ -41,3 +47,9 @@ impl Capabilities {
self.0
}
}

impl fmt::Display for Capabilities {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(compression: {})", self.compression())
}
}

0 comments on commit 418a5b6

Please sign in to comment.