Skip to content

Commit

Permalink
Merge pull request #95 from AluVM/baid58
Browse files Browse the repository at this point in the history
Update with new id string representation
  • Loading branch information
dr-orlovsky authored Jul 23, 2023
2 parents f5ec8b9 + 5dbde44 commit 12e8b34
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 58 deletions.
111 changes: 67 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ required-features = ["stl"]
amplify = { version = "4.0.0", default-features = false, features = ["apfloat", "derive", "hex"] }
paste = "1"
strict_encoding = { version = "2.5.0", default-features = false, features = ["float", "derive"] }
strict_types = { version = "1.5.0", optional = true }
strict_types = { version = "1.6.0", optional = true }
sha2 = "0.10.6"
ripemd = "0.1.3"
baid58 = "0.3.2"
baid58 = "0.4.1"
secp256k1 = { version = "0.27.0", optional = true, features = ["global-context"] }
curve25519-dalek = { version = "3.2", optional = true }
half = "~2.2.0" # Required to maintain MSRV
Expand Down
62 changes: 55 additions & 7 deletions src/library/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ use crate::LIB_NAME_ALUVM;
pub const LIB_ID_TAG: [u8; 32] = *b"urn:ubideco:aluvm:lib:v01#230304";

/// Unique identifier for a library.
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Debug, Display, From)]
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Debug, From)]
#[wrapper(Deref, BorrowSlice, Hex, Index, RangeOps)]
#[display(Self::to_baid58_string)]
#[derive(StrictType, StrictDecode)]
#[cfg_attr(feature = "std", derive(StrictEncode))]
#[strict_type(lib = LIB_NAME_ALUVM)]
Expand All @@ -69,12 +68,18 @@ impl FromBaid58<32> for LibId {}

impl FromStr for LibId {
type Err = Baid58ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { Self::from_baid58_str(s) }
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_baid58_str(s.trim_start_matches("urn:ubideco:"))
}
}

impl LibId {
#[allow(clippy::wrong_self_convention)] // FIXME #[display] only accepts &self
fn to_baid58_string(&self) -> String { format!("{:+}", self.to_baid58()) }
impl Display for LibId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if f.sign_minus() {
write!(f, "urn:ubideco:{::<}", self.to_baid58())
} else {
write!(f, "urn:ubideco:{::<#}", self.to_baid58())
}
}
}

impl LibId {
Expand Down Expand Up @@ -339,3 +344,46 @@ impl LibSite {
/// value
pub fn with(pos: u16, lib: LibId) -> LibSite { LibSite { lib, pos } }
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn lib_id_display() {
let id = LibId::with("FLOAT", &b"", &b"", &none!());
assert_eq!(
format!("{id}"),
"urn:ubideco:alu:GrjjwmeTsibiEeYYtjokmc8j4Jn1KWL2SX8NugG6T5kZ#pinball-eternal-colombo"
);
assert_eq!(
format!("{id:-}"),
"urn:ubideco:alu:GrjjwmeTsibiEeYYtjokmc8j4Jn1KWL2SX8NugG6T5kZ"
);
}

#[test]
fn lib_id_from_str() {
let id = LibId::with("FLOAT", &b"", &b"", &none!());
assert_eq!(
Ok(id),
LibId::from_str(
"urn:ubideco:alu:GrjjwmeTsibiEeYYtjokmc8j4Jn1KWL2SX8NugG6T5kZ#\
pinball-eternal-colombo"
)
);
assert_eq!(
Ok(id),
LibId::from_str("urn:ubideco:alu:GrjjwmeTsibiEeYYtjokmc8j4Jn1KWL2SX8NugG6T5kZ")
);
assert_eq!(
Ok(id),
LibId::from_str(
"alu:GrjjwmeTsibiEeYYtjokmc8j4Jn1KWL2SX8NugG6T5kZ#pinball-eternal-colombo"
)
);
assert_eq!(Ok(id), LibId::from_str("alu:GrjjwmeTsibiEeYYtjokmc8j4Jn1KWL2SX8NugG6T5kZ"));

assert_eq!(Ok(id), LibId::from_str("GrjjwmeTsibiEeYYtjokmc8j4Jn1KWL2SX8NugG6T5kZ"));
}
}
3 changes: 2 additions & 1 deletion src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use crate::library::LibSite;
use crate::LIB_NAME_ALUVM;

/// Strict type id for the library providing data types from this crate.
pub const LIB_ID_ALUVM: &str = "degree_boston_heart_DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo";
pub const LIB_ID_ALUVM: &str =
"urn:ubideco:stl:DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy";

fn _aluvm_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_ALUVM), tiny_bset! {
Expand Down
Loading

0 comments on commit 12e8b34

Please sign in to comment.