anaso_algorithm/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
//! The [Ana.so](https://ana.so) Algorithm
#![deny(missing_docs)]
#![forbid(unsafe_code)]
#![allow(clippy::pedantic)]
/// Data used for scoring. Pass into [`score_post`].
pub struct ScoringData {
/// Unix timestamp in seconds that the post was submitted.
time_posted: i64,
/// Number of likes the post has received in total.
likes: i64,
}
/// The static score value to be associated with a post.
///
/// This is made to be run on a post's data whenever
/// a like is added or removed, and put in a score column
/// on the post table.
///
/// As an example (though not the real code):
///
/// ```
/// # use anaso_algorithm::*;
/// # struct PostData {time_posted:i64,likes:i64}
/// # fn query(s:&str,x:i64)->PostData{PostData{time_posted:0,likes:0}}
/// # let id=0;
/// // Get the relevant data for calculating the score
/// let post: PostData = query("SELECT * FROM post WHERE id=?", id);
///
/// // Calculate the new score
/// let score = score_post(ScoringData {
/// time_posted: post.time_posted,
/// likes: post.likes
/// // ...
/// });
///
/// // Update with score
/// query("UPDATE post SET score=? WHERE id=?", id);
/// ```
///
/// When getting posts for a particular page, they will be
/// ordered by this value.
///
/// As an example (though not the real query):
///
/// ```sql
/// SELECT * FROM post ORDER BY score LIMIT=20;
/// ```
pub fn score_post(data: ScoringData) -> i64 {
let score = (data.likes as f64).log10() as i64;
data.time_posted / 43200 + score
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {}
}