postcard_bindgen/package/
mod.rs1pub mod npm_package;
2pub mod pip_module;
3
4use std::{
5 error::Error,
6 fmt::{Debug, Display},
7 str::FromStr,
8};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
26pub struct Version {
27 major: u32,
28 minor: u32,
29 patch: u32,
30}
31
32pub struct PackageInfo {
34 pub name: String,
35 pub version: Version,
36}
37
38impl Version {
39 pub fn from_array(parts: [u32; 3]) -> Self {
40 Self {
41 major: parts[0],
42 minor: parts[1],
43 patch: parts[2],
44 }
45 }
46}
47
48pub struct VersionFromStrError;
50
51impl Debug for VersionFromStrError {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 write!(
54 f,
55 "supplied string not a version format - <major.minor.patch>"
56 )
57 }
58}
59
60impl Display for VersionFromStrError {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 write!(f, "{:?}", self)
63 }
64}
65
66impl Error for VersionFromStrError {}
67
68impl FromStr for Version {
69 type Err = VersionFromStrError;
70
71 fn from_str(s: &str) -> Result<Self, Self::Err> {
72 let parts = s.split('.').collect::<Vec<_>>();
73 if parts.len() != 3 {
74 Err(VersionFromStrError)
75 } else {
76 Ok(Self {
77 major: u32::from_str(parts[0]).map_err(|_| VersionFromStrError)?,
78 minor: u32::from_str(parts[1]).map_err(|_| VersionFromStrError)?,
79 patch: u32::from_str(parts[2]).map_err(|_| VersionFromStrError)?,
80 })
81 }
82 }
83}
84
85impl Display for Version {
86 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87 write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
88 }
89}
90
91impl TryFrom<&str> for Version {
92 type Error = VersionFromStrError;
93
94 fn try_from(value: &str) -> Result<Self, Self::Error> {
95 Self::from_str(value)
96 }
97}