|
| 1 | +// Copyright 2023 Helsing GmbH |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::{fmt, ops::Deref, str::FromStr}; |
| 16 | + |
| 17 | +use miette::IntoDiagnostic; |
| 18 | +use serde::{Deserialize, Serialize}; |
| 19 | + |
| 20 | +/// A `buffrs` package directory for parsing and type safety |
| 21 | +#[derive(Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug)] |
| 22 | +#[serde(try_from = "String", into = "String")] |
| 23 | +pub struct PackageDirectory(String); |
| 24 | + |
| 25 | +/// Errors that can be generated parsing [`PackageDirectory`], see [`PackageDirectory::new()`]. |
| 26 | +#[derive(thiserror::Error, Debug, PartialEq)] |
| 27 | +pub enum PackageDirectoryError { |
| 28 | + /// Empty package directory. |
| 29 | + #[error("package directory must be at least one character long, but was empty")] |
| 30 | + Empty, |
| 31 | + /// Too long. |
| 32 | + #[error("package directories must be at most 128 characters long, but was {0:}")] |
| 33 | + TooLong(usize), |
| 34 | + /// Invalid start character. |
| 35 | + #[error("package directory must start with alphabetic character, but was {0:}")] |
| 36 | + InvalidStart(char), |
| 37 | + /// Invalid character. |
| 38 | + #[error("package directory must consist of only ASCII lowercase and dashes (-, _), but contains {0:} at position {1:}")] |
| 39 | + InvalidCharacter(char, usize), |
| 40 | +} |
| 41 | + |
| 42 | +impl super::ParseError for PackageDirectoryError { |
| 43 | + #[inline] |
| 44 | + fn empty() -> Self { |
| 45 | + Self::Empty |
| 46 | + } |
| 47 | + |
| 48 | + #[inline] |
| 49 | + fn too_long(current_length: usize) -> Self { |
| 50 | + Self::TooLong(current_length) |
| 51 | + } |
| 52 | + |
| 53 | + #[inline] |
| 54 | + fn invalid_start(first: char) -> Self { |
| 55 | + Self::InvalidStart(first) |
| 56 | + } |
| 57 | + |
| 58 | + #[inline] |
| 59 | + fn invalid_character(found: char, pos: usize) -> Self { |
| 60 | + Self::InvalidCharacter(found, pos) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl PackageDirectory { |
| 65 | + const MAX_LENGTH: usize = 128; |
| 66 | + |
| 67 | + /// New package directory from string. |
| 68 | + pub fn new<S: Into<String>>(value: S) -> Result<Self, PackageDirectoryError> { |
| 69 | + let value = value.into(); |
| 70 | + Self::validate(&value)?; |
| 71 | + Ok(Self(value)) |
| 72 | + } |
| 73 | + |
| 74 | + /// Validate a package directory. |
| 75 | + pub fn validate(directory: impl AsRef<str>) -> Result<(), PackageDirectoryError> { |
| 76 | + super::validate(directory.as_ref(), &[b'-', b'_'], Self::MAX_LENGTH) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl TryFrom<String> for PackageDirectory { |
| 81 | + type Error = PackageDirectoryError; |
| 82 | + |
| 83 | + fn try_from(value: String) -> Result<Self, Self::Error> { |
| 84 | + Self::new(value) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl FromStr for PackageDirectory { |
| 89 | + type Err = miette::Report; |
| 90 | + |
| 91 | + fn from_str(input: &str) -> Result<Self, Self::Err> { |
| 92 | + Self::new(input).into_diagnostic() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl From<PackageDirectory> for String { |
| 97 | + fn from(s: PackageDirectory) -> Self { |
| 98 | + s.to_string() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl Deref for PackageDirectory { |
| 103 | + type Target = str; |
| 104 | + |
| 105 | + fn deref(&self) -> &Self::Target { |
| 106 | + &self.0 |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl fmt::Display for PackageDirectory { |
| 111 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 112 | + self.0.fmt(f) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +mod test { |
| 118 | + use super::*; |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn ascii_lowercase() { |
| 122 | + assert_eq!( |
| 123 | + PackageDirectory::new("abc"), |
| 124 | + Ok(PackageDirectory("abc".into())) |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn short() { |
| 130 | + assert_eq!(PackageDirectory::new("a"), Ok(PackageDirectory("a".into()))); |
| 131 | + assert_eq!( |
| 132 | + PackageDirectory::new("ab"), |
| 133 | + Ok(PackageDirectory("ab".into())) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn long() { |
| 139 | + assert_eq!( |
| 140 | + PackageDirectory::new("a".repeat(PackageDirectory::MAX_LENGTH)), |
| 141 | + Ok(PackageDirectory("a".repeat(PackageDirectory::MAX_LENGTH))) |
| 142 | + ); |
| 143 | + |
| 144 | + assert_eq!( |
| 145 | + PackageDirectory::new("a".repeat(PackageDirectory::MAX_LENGTH + 1)), |
| 146 | + Err(PackageDirectoryError::TooLong( |
| 147 | + PackageDirectory::MAX_LENGTH + 1 |
| 148 | + )) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn empty() { |
| 154 | + assert_eq!(PackageDirectory::new(""), Err(PackageDirectoryError::Empty)); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn numeric_start() { |
| 159 | + assert_eq!( |
| 160 | + PackageDirectory::new("4abc"), |
| 161 | + Err(PackageDirectoryError::InvalidStart('4')) |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn underscore_and_dash() { |
| 167 | + assert_eq!( |
| 168 | + PackageDirectory::new("with_underscore-and-dash"), |
| 169 | + Ok(PackageDirectory("with_underscore-and-dash".into())), |
| 170 | + ); |
| 171 | + } |
| 172 | +} |
0 commit comments