
https://github.com/zed-industries/zed/issues/30972 brought up another case where our context is not enough to track the actual source of the issue: we get a general top-level error without inner error. The reason for this was `.ok_or_else(|| anyhow!("failed to read HEAD SHA"))?; ` on the top level. The PR finally reworks the way we use anyhow to reduce such issues (or at least make it simpler to bubble them up later in a fix). On top of that, uses a few more anyhow methods for better readability. * `.ok_or_else(|| anyhow!("..."))`, `map_err` and other similar error conversion/option reporting cases are replaced with `context` and `with_context` calls * in addition to that, various `anyhow!("failed to do ...")` are stripped with `.context("Doing ...")` messages instead to remove the parasitic `failed to` text * `anyhow::ensure!` is used instead of `if ... { return Err(...); }` calls * `anyhow::bail!` is used instead of `return Err(anyhow!(...));` Release Notes: - N/A
99 lines
2.4 KiB
Rust
99 lines
2.4 KiB
Rust
//! Constructs for working with [semantic versions](https://semver.org/).
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
use std::{
|
|
fmt::{self, Display},
|
|
str::FromStr,
|
|
};
|
|
|
|
use anyhow::{Context as _, Result};
|
|
use serde::{Deserialize, Serialize, de::Error};
|
|
|
|
/// A [semantic version](https://semver.org/) number.
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
|
|
pub struct SemanticVersion {
|
|
major: usize,
|
|
minor: usize,
|
|
patch: usize,
|
|
}
|
|
|
|
impl SemanticVersion {
|
|
/// Returns a new [`SemanticVersion`] from the given components.
|
|
pub const fn new(major: usize, minor: usize, patch: usize) -> Self {
|
|
Self {
|
|
major,
|
|
minor,
|
|
patch,
|
|
}
|
|
}
|
|
|
|
/// Returns the major version number.
|
|
#[inline(always)]
|
|
pub fn major(&self) -> usize {
|
|
self.major
|
|
}
|
|
|
|
/// Returns the minor version number.
|
|
#[inline(always)]
|
|
pub fn minor(&self) -> usize {
|
|
self.minor
|
|
}
|
|
|
|
/// Returns the patch version number.
|
|
#[inline(always)]
|
|
pub fn patch(&self) -> usize {
|
|
self.patch
|
|
}
|
|
}
|
|
|
|
impl FromStr for SemanticVersion {
|
|
type Err = anyhow::Error;
|
|
|
|
fn from_str(s: &str) -> Result<Self> {
|
|
let mut components = s.trim().split('.');
|
|
let major = components
|
|
.next()
|
|
.context("missing major version number")?
|
|
.parse()?;
|
|
let minor = components
|
|
.next()
|
|
.context("missing minor version number")?
|
|
.parse()?;
|
|
let patch = components
|
|
.next()
|
|
.context("missing patch version number")?
|
|
.parse()?;
|
|
Ok(Self {
|
|
major,
|
|
minor,
|
|
patch,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Display for SemanticVersion {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
|
|
}
|
|
}
|
|
|
|
impl Serialize for SemanticVersion {
|
|
fn serialize<S>(&self, serializer: S) -> std::prelude::v1::Result<S::Ok, S::Error>
|
|
where
|
|
S: serde::Serializer,
|
|
{
|
|
serializer.serialize_str(&self.to_string())
|
|
}
|
|
}
|
|
|
|
impl<'de> Deserialize<'de> for SemanticVersion {
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: serde::Deserializer<'de>,
|
|
{
|
|
let string = String::deserialize(deserializer)?;
|
|
Self::from_str(&string)
|
|
.map_err(|_| Error::custom(format!("Invalid version string \"{string}\"")))
|
|
}
|
|
}
|