Created
March 21, 2022 02:49
-
-
Save jayhuang75/f839e8dac38919800cc13b0659341bda to your computer and use it in GitHub Desktop.
url_shortener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[derive(Debug, Clone)] | |
| pub struct Shortener { | |
| id: u64, | |
| generator: Harsh, | |
| } | |
| impl Default for Shortener { | |
| fn default() -> Self { | |
| Shortener{ | |
| id: 0, | |
| generator: Harsh::default(), | |
| } | |
| } | |
| } | |
| impl Shortener { | |
| fn next_id(&mut self) -> String { | |
| let hashed = self.generator.encode(&[self.id]); | |
| self.id += 1; | |
| hashed | |
| } | |
| } | |
| #[derive(Debug)] | |
| pub struct TinyUrl { | |
| pub shortener: Shortener, | |
| } | |
| impl TinyUrl { | |
| pub fn new() -> TinyUrl { | |
| TinyUrl { | |
| shortener: Shortener::default(), | |
| } | |
| } | |
| pub fn set(&mut self, url: &str) -> String { | |
| self.shortener.next_id() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment