wallet_adapter/wallet_ser_der/
version.rs1use std::borrow::Cow;
2
3use crate::{Reflection, WalletError, WalletResult};
4
5use wallet_adapter_common::SemverVersion as SemverVersionData;
6#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct SemverVersion(pub SemverVersionData);
9
10impl SemverVersion {
11 pub fn major(&self) -> u8 {
13 self.0.major()
14 }
15
16 pub fn minor(&self) -> u8 {
18 self.0.minor()
19 }
20
21 pub fn patch(&self) -> u8 {
23 self.0.patch()
24 }
25
26 pub(crate) fn from_jsvalue(reflection: &Reflection) -> WalletResult<Self> {
28 let version = reflection
29 .reflect_inner("version")
30 .or(Err(WalletError::VersionNotFound))?
31 .as_string()
32 .ok_or(WalletError::InternalError(
33 "Expected `version` JsValue to be a String".to_string(),
34 ))?;
35
36 SemverVersion::parse(&version)
37 }
38
39 pub fn parse(version: &str) -> WalletResult<Self> {
41 let chunks = version.split(".").collect::<Vec<&str>>();
42
43 if chunks.len() != 3 {
44 return Err(WalletError::InvalidWalletVersion(version.to_string()));
45 }
46
47 let version_chunks = chunks
48 .iter()
49 .map(|chunk| {
50 chunk
51 .parse::<u8>()
52 .map_err(|_| WalletError::InvalidSemVerNumber(chunk.to_string()))
53 })
54 .collect::<WalletResult<Vec<u8>>>()?;
55
56 Ok(Self(
57 SemverVersionData::new()
58 .set_major(version_chunks[0])
59 .set_minor(version_chunks[1])
60 .set_patch(version_chunks[2]),
61 ))
62 }
63
64 pub fn stringify_version<'a>(&'a self) -> Cow<'a, str> {
66 self.0.stringify_version()
67 }
68}
69
70impl core::fmt::Display for SemverVersion {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 write!(f, "{}", self.0)
73 }
74}