Cookie Consent by Free Privacy Policy Generator 📌 How to deserialise DateTime from toml configuration


✅ How to deserialise DateTime from toml configuration


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

When implementing a new configuration parameter for Texted, I needed something to represent the date the blog started.

When looking into the TOML website, I was fortunate to discover that it has a data type for dates, so I could create the new parameter

blog_start_date = 2016-06-25

However, my fortune was gone as I found that the Toml crate does not support deserialising.

First, I tried to find if any other crate implemented this, but I found nothing that worked and most of them were over complicated for my use case.

Then I thought, I am pretty sure I am not the only one with this need and with some searching, I found in Arnau Siches website a nice solution for what I needed and I am sharing here a slightly modified version of their solution

use std::str::FromStr;  

use chrono::{NaiveDate, ParseError};  
use serde::Deserialize;  

// Code adapted from https://www.seachess.net/notes/toml-dates/  
#[derive(Copy, Clone, PartialEq, Debug)]  
pub struct TomlDate(pub NaiveDate);  

impl<'de> Deserialize<'de> for TomlDate {  
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>  
        where  
            D: serde::Deserializer<'de>,  
    {  
        use serde::de::Error;  
        let value = toml::value::Datetime::deserialize(deserializer)?;  
        let date = TomlDate::from_str(&value.to_string()).map_err(Error::custom)?;  
        Ok(date)  
    }  
}  

impl FromStr for TomlDate {  
    type Err = ParseError;  

    fn from_str(s: &str) -> Result<Self, Self::Err> {  
        let naive = NaiveDate::from_str(s)?;  
        Ok(Self(naive))  
    }  
}

Of course, one small unit test to make sure it works.

#[cfg(test)]  
mod tests {  
    use serde::Deserialize;  

    use super::*;  

    #[derive(Deserialize)]  
    pub struct Personal {  
        pub activity_start_year: i32,  
        pub blog_start_date: TomlDate,  
    }  

    #[derive(Deserialize)]  
    pub struct Config {  
        pub personal: Personal,  
    }  

    #[test]  
    fn test_date_time() {  
        let toml_str = r##"  
[personal]  
activity_start_year = 2000  
blog_start_date = 2024-04-22  
"##;  
        let cfg: Config = toml::from_str::<Config>(toml_str).unwrap();  
        assert_eq!(cfg.personal.blog_start_date, TomlDate(NaiveDate::from_ymd_opt(2024, 04, 22).unwrap()));  
    }  
}

To conclude, please note that this code was adapted from a CC BY-NC 4.0 license

Link to author's blog: thiagocafe/How to deserialise DateTime from toml configuration

...

✅ How to deserialise DateTime from toml configuration


📈 96.34 Punkte

✅ Medium CVE-2021-25946: Nconf-toml project Nconf-toml


📈 60.29 Punkte

✅ Another way to deserialise DateTime in Rust


📈 57.8 Punkte

✅ Zwei Probleme in ghc-toml-parser (Fedora)


📈 30.14 Punkte

✅ Zwei Probleme in ghc-toml-parser (Fedora)


📈 30.14 Punkte

✅ nconf-toml 0.0.1/0.0.2 Prototype code injection


📈 30.14 Punkte

✅ Introducing Python’s datetime Module


📈 23.13 Punkte

✅ How to Change the Datetime Format in Pandas


📈 23.13 Punkte

✅ Python: String to DateTime konvertieren und einsetzen


📈 23.13 Punkte

✅ PostgreSQL 8.4/9.0 datetime memory corruption


📈 23.13 Punkte

✅ Golang Date and Time (Datetime)


📈 23.13 Punkte

✅ D-Link DIR-816 A2 1.10 B05 /goform/form2systime.cgi handler datetime command injection


📈 23.13 Punkte

✅ How to Convert Datetime to Epoch in Python


📈 23.13 Punkte

✅ How to Compare DateTime Objects in C#


📈 23.13 Punkte

✅ [webapps] delpino73 Blue-Smiley-Organizer 1.32 - 'datetime' SQL Injection


📈 23.13 Punkte

✅ How to Convert DateTime to YYYYMMDDHHMMSS Format in C#


📈 23.13 Punkte

✅ wvWare 0.7.4/0.7.5/0.7.6/1.0 wvHandleDateTimePicture DateTime memory corruption


📈 23.13 Punkte

✅ Testing DateTime.Now Revisited: Using .NET 8.0 TimeProvider


📈 23.13 Punkte

✅ DateTime Equal or Greater Than Today in MySQL


📈 23.13 Punkte

✅ In Python: `date` vs `datetime` object


📈 23.13 Punkte

✅ Datetime Get Previous Month Python


📈 23.13 Punkte

✅ MySQL: Using and Enhancing `DATETIME` and `TIMESTAMP`


📈 23.13 Punkte

✅ Python datetime: So arbeiten Sie mit Datum und Uhrzeit in Python


📈 23.13 Punkte

✅ Convert a String to DateTime in PowerShell


📈 23.13 Punkte

✅ Demystifying the Python Datetime Library


📈 23.13 Punkte

✅ SQL CONVERT – The DATE to String or DATETIME Function


📈 23.13 Punkte

✅ Samba up to 4.9.12/4.10.7/4.11.0rc2 Configuration Configuration File directory traversal


📈 16.79 Punkte

✅ Piwigo 2.9.2 Configuration admin/configuration.php order_by sql injection


📈 16.79 Punkte

✅ Configuration Scanners Adding Java Specific Configuration Files, (Mon, Jun 24th)


📈 16.79 Punkte

✅ Azure App Configuration - Making Centralized Configuration Easy


📈 16.79 Punkte











matomo

Datei nicht gefunden!