use std::borrow::Borrow;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PackageName(String);
impl PackageName {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for PackageName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for PackageName {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for PackageName {
fn from(value: &str) -> Self {
Self(value.to_string())
}
}
impl AsRef<str> for PackageName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for PackageName {
fn borrow(&self) -> &str {
&self.0
}
}
impl PartialEq<str> for PackageName {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for PackageName {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct InvalidPackageName(std::borrow::Cow<'static, str>);
impl InvalidPackageName {
pub fn new(reason: impl Into<std::borrow::Cow<'static, str>>) -> Self {
Self(reason.into())
}
#[must_use]
pub fn reason(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VersionReq(String);
impl VersionReq {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for VersionReq {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for VersionReq {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for VersionReq {
fn from(value: &str) -> Self {
Self(value.to_string())
}
}
impl AsRef<str> for VersionReq {
fn as_ref(&self) -> &str {
&self.0
}
}
impl PartialEq<str> for VersionReq {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for VersionReq {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
#[cfg(test)]
mod tests {
use super::{PackageName, VersionReq};
#[test]
fn package_name_round_trips_empty_string() {
let name = PackageName::new("");
assert_eq!(name.as_str(), "");
assert_eq!(name, "");
}
#[test]
fn package_name_round_trips_surrounding_whitespace() {
let name = PackageName::new(" serde ");
assert_eq!(name.as_str(), " serde ");
assert_eq!(name, " serde ");
}
#[test]
fn package_name_round_trips_non_ascii() {
let name = PackageName::new("パッケージ");
assert_eq!(name.as_str(), "パッケージ");
assert_eq!(name, "パッケージ");
}
#[test]
fn package_name_compares_byte_wise_like_string() {
assert_ne!(PackageName::new("Foo"), PackageName::new("foo"));
assert_eq!(
PackageName::new("Foo") == PackageName::new("foo"),
"Foo" == "foo"
);
}
#[test]
fn package_name_into_string_roundtrip() {
let original = String::from("tokio");
let name = PackageName::new(original.clone());
assert_eq!(name.into_string(), original);
}
#[test]
fn package_name_hashmap_reachable_by_str_and_by_package_name() {
use std::collections::HashMap;
let mut map: HashMap<PackageName, u32> = HashMap::new();
map.insert(PackageName::new("serde"), 1);
assert_eq!(map.get("serde"), Some(&1));
assert_eq!(map.get(&PackageName::new("serde")), Some(&1));
}
#[test]
fn version_req_round_trips_empty_string() {
let req = VersionReq::new("");
assert_eq!(req.as_str(), "");
assert_eq!(req, "");
}
#[test]
fn version_req_round_trips_surrounding_whitespace() {
let req = VersionReq::new(" ^1.0 ");
assert_eq!(req.as_str(), " ^1.0 ");
assert_eq!(req, " ^1.0 ");
}
#[test]
fn version_req_round_trips_non_ascii() {
let req = VersionReq::new("非対応");
assert_eq!(req.as_str(), "非対応");
assert_eq!(req, "非対応");
}
#[test]
fn version_req_compares_byte_wise_like_string() {
assert_ne!(VersionReq::new("^1.0"), VersionReq::new("^1.0 "));
}
#[test]
fn version_req_into_string_roundtrip() {
let original = String::from("~2.3.4");
let req = VersionReq::new(original.clone());
assert_eq!(req.into_string(), original);
}
}