Skip to content

Commit

Permalink
ur: take a reference to custom ur type identifiers
Browse files Browse the repository at this point in the history
And inline the private `encode` function that was
only used twice before, and failed to inline the "ur"
format argument.
  • Loading branch information
dspicher committed Oct 16, 2023
1 parent 5df0b99 commit 8798ba7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Take a reference to custom UR type identifiers

## [0.4.0](https://github.com/dspicher/ur-rs/releases/tag/0.4.0) - 2023-08-04
- Added support for `no-std` environments. https://github.com/dspicher/ur-rs/pull/183
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum Msg {
}

pub struct App {
encoder: ur::Encoder,
encoder: ur::Encoder<'static>,
interval: Option<Interval>,
current_part: Option<String>,
input: String,
Expand Down
43 changes: 17 additions & 26 deletions src/ur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,22 @@ impl From<crate::fountain::Error> for Error {
#[must_use]
pub fn encode(data: &[u8], ur_type: &Type) -> String {
let body = crate::bytewords::encode(data, crate::bytewords::Style::Minimal);
encode_ur(&[ur_type.encoding(), body])
}

#[must_use]
fn encode_ur(items: &[String]) -> String {
alloc::format!("{}:{}", "ur", items.join("/"))
alloc::format!("ur:{}/{body}", ur_type.encoding())
}

/// The type of uniform resource.
pub enum Type {
pub enum Type<'a> {
/// A `bytes` uniform resource.
Bytes,
/// A custom uniform resource.
Custom(String),
Custom(&'a str),
}

impl Type {
fn encoding(&self) -> String {
impl<'a> Type<'a> {
const fn encoding(&self) -> &'a str {
match self {
Self::Bytes => "bytes".into(),
Self::Custom(s) => s.clone(),
Self::Bytes => "bytes",
Self::Custom(s) => s,
}
}
}
Expand All @@ -116,12 +111,12 @@ impl Type {
/// # Examples
///
/// See the [`crate::ur`] module documentation for an example.
pub struct Encoder {
pub struct Encoder<'a> {
fountain: crate::fountain::Encoder,
ur_type: Type,
ur_type: Type<'a>,
}

impl Encoder {
impl<'a> Encoder<'a> {
/// Creates a new [`bytes`] [`Encoder`] for given a message payload.
///
/// The emitted fountain parts will respect the maximum fragment length argument.
Expand Down Expand Up @@ -153,14 +148,10 @@ impl Encoder {
/// will be returned.
///
/// [`custom`]: Type::Custom
pub fn new(
message: &[u8],
max_fragment_length: usize,
s: impl Into<String>,
) -> Result<Self, Error> {
pub fn new(message: &[u8], max_fragment_length: usize, s: &'a str) -> Result<Self, Error> {
Ok(Self {
fountain: crate::fountain::Encoder::new(message, max_fragment_length)?,
ur_type: Type::Custom(s.into()),
ur_type: Type::Custom(s),
})
}

Expand All @@ -176,11 +167,11 @@ impl Encoder {
pub fn next_part(&mut self) -> Result<String, Error> {
let part = self.fountain.next_part();
let body = crate::bytewords::encode(&part.cbor()?, crate::bytewords::Style::Minimal);
Ok(encode_ur(&[
Ok(alloc::format!(
"ur:{}/{}/{body}",
self.ur_type.encoding(),
part.sequence_id(),
body,
]))
part.sequence_id()
))
}

/// Returns the current count of already emitted parts.
Expand Down Expand Up @@ -417,7 +408,7 @@ mod tests {

let data = crypto_seed().unwrap();

let e = encode(&data, &Type::Custom("crypto-request".into()));
let e = encode(&data, &Type::Custom("crypto-request"));
let expected = "ur:crypto-request/oeadtpdagdaobncpftlnylfgfgmuztihbawfsgrtflaotaadwkoyadtaaohdhdcxvsdkfgkepezepefrrffmbnnbmdvahnptrdtpbtuyimmemweootjshsmhlunyeslnameyhsdi";
assert_eq!(expected, e);

Expand Down

0 comments on commit 8798ba7

Please sign in to comment.