Skip to content

Commit c38996c

Browse files
committed
refactor(config)!: improve function argument from config.rs
1 parent 0447fb5 commit c38996c

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/config.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ impl Config {
5454
///
5555
/// If the value is a list, the entire list is cleared.
5656
/// If you need to clear 1 value from a list see `self.clear_value`
57-
pub fn clear(&self, key: &str) {
57+
pub fn clear(&self, key: impl ToString) {
5858
raw::clear(key.to_string());
5959
}
6060

6161
/// Clear a single value from a list.
6262
/// Used for removing one item in an apt configuruation list
63-
pub fn clear_value(&self, key: &str, value: &str) {
63+
pub fn clear_value(&self, key: impl ToString, value: impl ToString) {
6464
raw::clear_value(key.to_string(), value.to_string());
6565
}
6666

@@ -80,12 +80,12 @@ impl Config {
8080
/// Find a key and return it's value as a string.
8181
///
8282
/// default is what will be returned if nothing is found.
83-
pub fn find(&self, key: &str, default: &str) -> String {
83+
pub fn find(&self, key: impl ToString, default: impl ToString) -> String {
8484
raw::find(key.to_string(), default.to_string())
8585
}
8686

8787
/// Exactly like find but takes no default and returns an option instead.
88-
pub fn get(&self, key: &str) -> Option<String> {
88+
pub fn get(&self, key: impl ToString) -> Option<String> {
8989
let value = raw::find(key.to_string(), "".to_string());
9090
if value.is_empty() {
9191
return None;
@@ -103,7 +103,7 @@ impl Config {
103103
/// There is not much difference in `self.dir` and `self.file`
104104
///
105105
/// `dir` will return with a trailing `/` where `file` will not.
106-
pub fn file(&self, key: &str, default: &str) -> String {
106+
pub fn file(&self, key: impl ToString, default: impl ToString) -> String {
107107
raw::find_file(key.to_string(), default.to_string())
108108
}
109109

@@ -116,24 +116,24 @@ impl Config {
116116
/// There is not much difference in `self.dir` and `self.file`
117117
///
118118
/// `dir` will return with a trailing `/` where `file` will not.
119-
pub fn dir(&self, key: &str, default: &str) -> String {
119+
pub fn dir(&self, key: impl ToString, default: impl ToString) -> String {
120120
raw::find_dir(key.to_string(), default.to_string())
121121
}
122122

123123
/// Same as find, but for boolean values.
124-
pub fn bool(&self, key: &str, default: bool) -> bool {
124+
pub fn bool(&self, key: impl ToString, default: bool) -> bool {
125125
raw::find_bool(key.to_string(), default)
126126
}
127127

128128
/// Same as find, but for i32 values.
129-
pub fn int(&self, key: &str, default: i32) -> i32 {
129+
pub fn int(&self, key: impl ToString, default: i32) -> i32 {
130130
raw::find_int(key.to_string(), default)
131131
}
132132

133133
/// Return a vector for an Apt configuration list.
134134
///
135135
/// An example of a common key that contains a list `raw::NeverAutoRemove`.
136-
pub fn find_vector(&self, key: &str) -> Vec<String> {
136+
pub fn find_vector(&self, key: impl ToString) -> Vec<String> {
137137
raw::find_vector(key.to_string())
138138
}
139139

@@ -153,11 +153,11 @@ impl Config {
153153
}
154154

155155
/// Set the given key to the specified value.
156-
pub fn set(&self, key: &str, value: &str) {
156+
pub fn set(&self, key: impl ToString, value: impl ToString) {
157157
raw::set(key.to_string(), value.to_string())
158158
}
159159

160-
pub fn tree(&self, key: &str) -> Option<ConfigTree> {
160+
pub fn tree(&self, key: impl ToString) -> Option<ConfigTree> {
161161
let tree = unsafe { raw::tree(key.to_string()) };
162162
if tree.end() {
163163
return None;
@@ -187,8 +187,8 @@ impl Config {
187187
/// // Using "AptList" here will not work and will panic.
188188
/// config.set_vector("AptList", &apt_list);
189189
/// ```
190-
pub fn set_vector(&self, key: &str, values: &Vec<&str>) {
191-
let mut vec_key = String::from(key);
190+
pub fn set_vector(&self, key: &str, values: &[impl ToString]) {
191+
let mut vec_key = key.to_string();
192192
if !vec_key.ends_with("::") {
193193
vec_key.push_str("::");
194194
}

tests/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ mod config {
9898
assert!(config.find_vector("oma_apt::aptlist").is_empty());
9999
}
100100

101+
#[test]
102+
fn test_compression_ytypes_order() {
103+
let config = Config::new();
104+
config.set_vector(
105+
"Acquire::CompressionTypes::Order",
106+
&vec!["zst"],
107+
);
108+
109+
let order = config.get_compression_types();
110+
111+
assert_eq!(
112+
order,
113+
vec![
114+
"zst".to_string(),
115+
"xz".to_string(),
116+
"bz2".to_string(),
117+
"lzma".to_string(),
118+
"gz".to_string(),
119+
"lz4".to_string(),
120+
"uncompressed".to_string(),
121+
]
122+
)
123+
}
124+
101125
#[test]
102126
fn get_architectures() {
103127
let config = Config::new();

0 commit comments

Comments
 (0)