Skip to content

Commit c2bc941

Browse files
committed
refactor
- cargo fmt - remove unnecessary tests - keep everything open
1 parent b8d3d3e commit c2bc941

File tree

5 files changed

+50
-75
lines changed

5 files changed

+50
-75
lines changed

gix-config-value/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rust-version = "1.82"
1212
include = ["src/**/*", "LICENSE-*"]
1313

1414
[lib]
15-
doctest = false
15+
doctest = true
1616

1717
[features]
1818
## Data structures implement `serde::Serialize` and `serde::Deserialize`.

gix-config-value/src/path.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ use bstr::BStr;
44

55
use crate::Path;
66

7-
/// The prefix used to mark a path as optional in Git configuration files.
8-
const OPTIONAL_PREFIX: &[u8] = b":(optional)";
9-
107
///
118
pub mod interpolate {
129
use std::path::PathBuf;
@@ -111,7 +108,9 @@ impl AsRef<BStr> for Path<'_> {
111108

112109
impl<'a> From<Cow<'a, BStr>> for Path<'a> {
113110
fn from(value: Cow<'a, BStr>) -> Self {
114-
// Check if the value starts with ":(optional)" prefix
111+
/// The prefix used to mark a path as optional in Git configuration files.
112+
const OPTIONAL_PREFIX: &[u8] = b":(optional)";
113+
115114
if value.starts_with(OPTIONAL_PREFIX) {
116115
// Strip the prefix while preserving the Cow variant for efficiency:
117116
// - Borrowed data remains borrowed (no allocation)
@@ -125,27 +124,18 @@ impl<'a> From<Cow<'a, BStr>> for Path<'a> {
125124
};
126125
Path {
127126
value: stripped,
128-
optional: true,
127+
is_optional: true,
129128
}
130129
} else {
131130
Path {
132131
value,
133-
optional: false,
132+
is_optional: false,
134133
}
135134
}
136135
}
137136
}
138137

139138
impl<'a> Path<'a> {
140-
/// Returns `true` if this path was prefixed with `:(optional)`.
141-
///
142-
/// Optional paths indicate that it's acceptable if the file doesn't exist.
143-
/// This is typically used for configuration like `blame.ignorerevsfile` where
144-
/// the file might not exist in all repositories.
145-
pub fn is_optional(&self) -> bool {
146-
self.optional
147-
}
148-
149139
/// Interpolates this path into a path usable on the file system.
150140
///
151141
/// If this path starts with `~/` or `~user/` or `%(prefix)/`
@@ -158,8 +148,8 @@ impl<'a> Path<'a> {
158148
/// This location is not known at compile time and therefore need to be
159149
/// optionally provided by the caller through `git_install_dir`.
160150
///
161-
/// Any other, non-empty path value is returned unchanged and error is returned in case of an empty path value or if required input
162-
/// wasn't provided.
151+
/// Any other, non-empty path value is returned unchanged and error is returned in case of an empty path value or if the required
152+
/// input wasn't provided.
163153
pub fn interpolate(
164154
self,
165155
interpolate::Context {

gix-config-value/src/types.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct Boolean(pub bool);
4646
///
4747
/// Paths can be marked as optional by prefixing them with `:(optional)` in the configuration.
4848
/// This indicates that it's acceptable if the file doesn't exist, which is useful for
49-
/// configuration values like `blame.ignorerevsfile` that may only exist in some repositories.
49+
/// configuration values like `blame.ignoreRevsFile` that may only exist in some repositories.
5050
///
5151
/// ```
5252
/// use std::borrow::Cow;
@@ -55,18 +55,21 @@ pub struct Boolean(pub bool);
5555
///
5656
/// // Regular path - file is expected to exist
5757
/// let path = Path::from(Cow::Borrowed(b"/etc/gitconfig".as_bstr()));
58-
/// assert!(!path.is_optional());
58+
/// assert!(!path.is_optional);
5959
///
6060
/// // Optional path - it's okay if the file doesn't exist
6161
/// let path = Path::from(Cow::Borrowed(b":(optional)~/.gitignore".as_bstr()));
62-
/// assert!(path.is_optional());
62+
/// assert!(path.is_optional);
6363
/// assert_eq!(path.value.as_ref(), b"~/.gitignore"); // prefix is stripped
6464
/// ```
6565
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
6666
pub struct Path<'a> {
6767
/// The path string, un-interpolated
6868
pub value: std::borrow::Cow<'a, bstr::BStr>,
6969
/// Whether this path was prefixed with `:(optional)`, indicating it's acceptable if the file doesn't exist.
70-
/// Use `is_optional()` method to check this value.
71-
pub(crate) optional: bool,
70+
///
71+
/// Optional paths indicate that it's acceptable if the file doesn't exist.
72+
/// This is typically used for configuration like `blame.ignorerevsfile` where
73+
/// the file might not exist in all repositories.
74+
pub is_optional: bool,
7275
}

gix-config-value/tests/value/main.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,3 @@ mod boolean;
1515
mod color;
1616
mod integer;
1717
mod path;
18-
19-
/// Ensure that the `:(optional)` prefix is only recognized for Path types, not for other types
20-
mod optional_prefix_only_for_paths {
21-
use std::borrow::Cow;
22-
use gix_config_value::{Boolean, Integer};
23-
24-
#[test]
25-
fn optional_prefix_not_recognized_in_boolean() {
26-
// Boolean should fail to parse this because it's not a valid boolean value
27-
let result = Boolean::try_from(Cow::Borrowed(crate::b(":(optional)true")));
28-
assert!(result.is_err(), "Boolean should not recognize :(optional) prefix");
29-
}
30-
31-
#[test]
32-
fn optional_prefix_not_recognized_in_integer() {
33-
// Integer should fail to parse this because it's not a valid integer value
34-
let result = Integer::try_from(Cow::Borrowed(crate::b(":(optional)42")));
35-
assert!(result.is_err(), "Integer should not recognize :(optional) prefix");
36-
}
37-
}

gix-config-value/tests/value/path.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,111 +131,113 @@ mod interpolate {
131131
mod optional_prefix {
132132
use std::borrow::Cow;
133133

134-
use bstr::ByteSlice;
135134
use crate::{b, cow_str};
135+
use bstr::ByteSlice;
136136

137137
#[test]
138138
fn path_without_optional_prefix_is_not_optional() {
139139
let path = gix_config_value::Path::from(Cow::Borrowed(b("/some/path")));
140-
assert!(!path.is_optional(), "path without prefix should not be optional");
140+
assert!(!path.is_optional, "path without prefix should not be optional");
141141
assert_eq!(path.value.as_ref(), b"/some/path");
142142
}
143143

144144
#[test]
145145
fn path_with_optional_prefix_is_optional() {
146146
let path = gix_config_value::Path::from(cow_str(":(optional)/some/path"));
147-
assert!(path.is_optional(), "path with :(optional) prefix should be optional");
147+
assert!(path.is_optional, "path with :(optional) prefix should be optional");
148148
assert_eq!(path.value.as_ref(), b"/some/path", "prefix should be stripped");
149149
}
150150

151151
#[test]
152152
fn optional_prefix_with_relative_path() {
153153
let path = gix_config_value::Path::from(cow_str(":(optional)relative/path"));
154-
assert!(path.is_optional());
154+
assert!(path.is_optional);
155155
assert_eq!(path.value.as_ref(), b"relative/path");
156156
}
157157

158158
#[test]
159159
fn optional_prefix_with_tilde_expansion() {
160160
let path = gix_config_value::Path::from(cow_str(":(optional)~/config/file"));
161-
assert!(path.is_optional());
162-
assert_eq!(path.value.as_ref(), b"~/config/file", "tilde should be preserved for interpolation");
161+
assert!(path.is_optional);
162+
assert_eq!(
163+
path.value.as_ref(),
164+
b"~/config/file",
165+
"tilde should be preserved for interpolation"
166+
);
163167
}
164168

165169
#[test]
166170
fn optional_prefix_with_prefix_substitution() {
167171
let path = gix_config_value::Path::from(cow_str(":(optional)%(prefix)/share/git"));
168-
assert!(path.is_optional());
169-
assert_eq!(path.value.as_ref(), b"%(prefix)/share/git", "prefix should be preserved for interpolation");
172+
assert!(path.is_optional);
173+
assert_eq!(
174+
path.value.as_ref(),
175+
b"%(prefix)/share/git",
176+
"prefix should be preserved for interpolation"
177+
);
170178
}
171179

172180
#[test]
173181
fn optional_prefix_with_windows_path() {
174182
let path = gix_config_value::Path::from(cow_str(r":(optional)C:\Users\file"));
175-
assert!(path.is_optional());
183+
assert!(path.is_optional);
176184
assert_eq!(path.value.as_ref(), br"C:\Users\file");
177185
}
178186

179187
#[test]
180188
fn optional_prefix_followed_by_empty_path() {
181189
let path = gix_config_value::Path::from(cow_str(":(optional)"));
182-
assert!(path.is_optional());
190+
assert!(path.is_optional);
183191
assert_eq!(path.value.as_ref(), b"", "empty path after prefix is valid");
184192
}
185193

186194
#[test]
187195
fn partial_optional_string_is_not_treated_as_prefix() {
188196
let path = gix_config_value::Path::from(cow_str(":(opt)ional/path"));
189-
assert!(!path.is_optional(), "incomplete prefix should not be treated as optional marker");
197+
assert!(
198+
!path.is_optional,
199+
"incomplete prefix should not be treated as optional marker"
200+
);
190201
assert_eq!(path.value.as_ref(), b":(opt)ional/path");
191202
}
192203

193204
#[test]
194205
fn optional_prefix_case_sensitive() {
195206
let path = gix_config_value::Path::from(cow_str(":(OPTIONAL)/some/path"));
196-
assert!(!path.is_optional(), "prefix should be case-sensitive");
207+
assert!(!path.is_optional, "prefix should be case-sensitive");
197208
assert_eq!(path.value.as_ref(), b":(OPTIONAL)/some/path");
198209
}
199210

200211
#[test]
201212
fn optional_prefix_with_spaces() {
202213
let path = gix_config_value::Path::from(cow_str(":(optional) /path/with/space"));
203-
assert!(path.is_optional());
204-
assert_eq!(path.value.as_ref(), b" /path/with/space", "space after prefix should be preserved");
205-
}
206-
207-
#[test]
208-
fn interpolate_preserves_optional_flag() -> crate::Result {
209-
use gix_config_value::path;
210-
211-
let path = gix_config_value::Path::from(cow_str(":(optional)/absolute/path"));
212-
assert!(path.is_optional());
213-
214-
let interpolated = path.interpolate(path::interpolate::Context::default())?;
215-
assert_eq!(interpolated.as_ref(), std::path::Path::new("/absolute/path"));
216-
217-
Ok(())
214+
assert!(path.is_optional);
215+
assert_eq!(
216+
path.value.as_ref(),
217+
b" /path/with/space",
218+
"space after prefix should be preserved"
219+
);
218220
}
219221

220222
#[test]
221223
fn borrowed_path_stays_borrowed_after_prefix_stripping() {
222224
// Verify that we don't unnecessarily allocate when stripping the prefix from borrowed data
223225
let borrowed_input: &[u8] = b":(optional)/some/path";
224226
let path = gix_config_value::Path::from(Cow::Borrowed(borrowed_input.as_bstr()));
225-
226-
assert!(path.is_optional());
227+
228+
assert!(path.is_optional);
227229
assert_eq!(path.value.as_ref(), b"/some/path");
228230
// Verify it's still borrowed (no unnecessary allocation)
229231
assert!(matches!(path.value, Cow::Borrowed(_)));
230232
}
231233

232234
#[test]
233235
fn owned_path_stays_owned_after_prefix_stripping() {
234-
// Verify that owned data remains owned after prefix stripping (but efficiently)
236+
// Verify that owned data remains owned after prefix stripping
235237
let owned_input = bstr::BString::from(":(optional)/some/path");
236238
let path = gix_config_value::Path::from(Cow::Owned(owned_input));
237-
238-
assert!(path.is_optional());
239+
240+
assert!(path.is_optional);
239241
assert_eq!(path.value.as_ref(), b"/some/path");
240242
// Verify it's still owned
241243
assert!(matches!(path.value, Cow::Owned(_)));

0 commit comments

Comments
 (0)