Change method name of Preference to better conform to standards

Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
This commit is contained in:
Ben Aaron Goldberg 2021-10-23 20:46:56 -04:00
parent 2dfa2c2f92
commit 87583fc383
2 changed files with 18 additions and 18 deletions

View file

@ -46,7 +46,7 @@ pub trait Preference {
/// crucial step to randomly selecting a pronoun set based on a user's preferences, as
/// any selection is done by using a [`WeightedTable`]. All preference versions must
/// implement this method.
fn into_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> Result<WeightedTable<&'a Pronoun>, ParseError>;
fn create_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> Result<WeightedTable<&'a Pronoun>, ParseError>;
/// Parse a given prefstring, after it's extraction from base64
///
@ -63,7 +63,7 @@ pub trait Preference {
/// This should be done in accordance with the [prefstring specification][1].
///
/// [1]: https://fem.mint.lgbt/Emi/PronounsToday/raw/branch/main/doc/User-Preference-String-Spec.txt
fn into_prefstring_bytes(&self) -> Vec<u8>;
fn as_prefstring_bytes(&self) -> Vec<u8>;
/// Parse a base64 prefstring
///
@ -79,8 +79,8 @@ pub trait Preference {
///
/// This is the primary method of creating a prefstring from a `Preference` object. The
/// default implementation calls the underlying [`Preference::into_prefstring_bytes()`] method.
fn into_prefstring(&self) -> String {
BASE32_NOPAD.encode(&self.into_prefstring_bytes())
fn as_prefstring(&self) -> String {
BASE32_NOPAD.encode(&self.as_prefstring_bytes())
}
/// Select a pronoun for today using this Preference's WeightedTable.
@ -98,15 +98,15 @@ pub trait Preference {
Some(name) => name.as_bytes(),
None => &[],
};
Ok(self.into_weighted_table(settings)?.select_today(seed))
Ok(self.create_weighted_table(settings)?.select_today(seed))
}
}
impl Preference for UserPreferences {
fn into_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> Result<WeightedTable<&'a Pronoun>, ParseError> {
fn create_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> Result<WeightedTable<&'a Pronoun>, ParseError> {
match self {
UserPreferences::V0(pref) => pref,
}.into_weighted_table(settings)
}.create_weighted_table(settings)
}
fn from_prefstring_bytes(bytes: &[u8]) -> Result<Self, ParseError> where Self: Sized {
@ -123,9 +123,9 @@ impl Preference for UserPreferences {
}
}
fn into_prefstring_bytes(&self) -> Vec<u8> {
fn as_prefstring_bytes(&self) -> Vec<u8> {
match self {
UserPreferences::V0(pref) => pref,
}.into_prefstring_bytes()
}.as_prefstring_bytes()
}
}

View file

@ -24,7 +24,7 @@ pub struct UserPreferencesV0 {
}
impl Preference for UserPreferencesV0 {
fn into_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> Result<WeightedTable<&'a Pronoun>, ParseError> {
fn create_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> Result<WeightedTable<&'a Pronoun>, ParseError> {
let mut remaining_pronouns = settings.pronoun_list.iter();
let mut weighted_pronouns = HashMap::with_capacity(settings.pronoun_list.len());
@ -118,7 +118,7 @@ impl Preference for UserPreferencesV0 {
})
}
fn into_prefstring_bytes(&self) -> Vec<u8> {
fn as_prefstring_bytes(&self) -> Vec<u8> {
let mut defaults_byte = 0u8;
if self.default_enabled {
defaults_byte |= 0b10000000;
@ -241,7 +241,7 @@ mod tests {
commands: Vec::new(),
};
let table = p.into_weighted_table(&s).unwrap();
let table = p.create_weighted_table(&s).unwrap();
let expected_table = vec![
("she", 2),
("he", 2),
@ -271,7 +271,7 @@ mod tests {
],
};
let table = p.into_weighted_table(&s).unwrap();
let table = p.create_weighted_table(&s).unwrap();
let expected_table = vec![
("she", 3),
("he", 3),
@ -293,7 +293,7 @@ mod tests {
],
};
let table = p.into_weighted_table(&s).unwrap();
let table = p.create_weighted_table(&s).unwrap();
let expected_table = vec![
("she", 5),
("he", 4),
@ -323,7 +323,7 @@ mod tests {
],
};
let table = p.into_weighted_table(&s).unwrap();
let table = p.create_weighted_table(&s).unwrap();
let expected_table = vec![
("he", 9),
("they", 5),
@ -347,7 +347,7 @@ mod tests {
],
};
let err = p.into_weighted_table(&s).unwrap_err();
let err = p.create_weighted_table(&s).unwrap_err();
assert_eq!(err, ParseError::PrefstringExceedsPronounCount);
}
@ -366,7 +366,7 @@ mod tests {
],
};
let err = p.into_weighted_table(&s).unwrap_err();
let err = p.create_weighted_table(&s).unwrap_err();
assert_eq!(err, ParseError::PrefstringExceedsPronounCount);
}
@ -385,7 +385,7 @@ mod tests {
],
};
let table = p.into_weighted_table(&s).unwrap();
let table = p.create_weighted_table(&s).unwrap();
let expected_table = vec![
("it", 3),
];