Created
October 20, 2017 11:04
-
-
Save DoumanAsh/b6d46da4d4de5cc06a5bd28ce81843b0 to your computer and use it in GitHub Desktop.
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
| ### toml | |
| # HTTP Authorization Bearer token | |
| [gab] | |
| token = 'fill your token' | |
| # Consumer Token of application | |
| [twitter.consumer] | |
| key = 'key' | |
| secret = 'secret' | |
| # Authorization Token to access user account | |
| [twitter.access] | |
| key = 'key' | |
| secret = 'secret' | |
| ### code to parse | |
| //!Configuration module | |
| use ::std::fs::{ | |
| File | |
| }; | |
| use ::std::path::{ | |
| Path | |
| }; | |
| use ::std::io::{ | |
| BufReader, | |
| Read | |
| }; | |
| use ::toml; | |
| pub const NAME: &'static str = "fie.toml"; | |
| #[derive(Serialize, Deserialize, Debug)] | |
| pub struct Token { | |
| key: String, | |
| secret: String | |
| } | |
| ///Twitter configuration | |
| #[derive(Deserialize, Debug)] | |
| pub struct Twitter { | |
| pub consumer: Token, | |
| pub access: Token | |
| } | |
| ///Gab configuration. | |
| #[derive(Deserialize, Debug)] | |
| pub struct Gab { | |
| ///Token to use for authorization. | |
| /// | |
| ///You can get it after logging into gab.io and examining your HTTP requests. | |
| pub token: String | |
| } | |
| #[derive(Deserialize, Debug)] | |
| pub struct Config { | |
| pub gab: Gab, | |
| pub twitter: Twitter | |
| } | |
| impl Config { | |
| pub fn from_file(path: &Path) -> Result<Self, String> { | |
| let file = File::open(path).map_err(error_formatter!("Cannot open config file."))?; | |
| let mut file = BufReader::new(file); | |
| let mut buffer = String::new(); | |
| file.read_to_string(&mut buffer).map_err(error_formatter!("Unable to read config file."))?; | |
| toml::from_str(&buffer).map_err(error_formatter!("Invalid config file!"))? | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment