scatter_net/legacy/net/config/implementations/
from_str.rs

1use std::str::FromStr;
2
3use crate::NetConfig;
4
5impl FromStr for NetConfig {
6    type Err = NetConfigFromStrError;
7
8    /// Attempts to deserialize a `NetConfig`.
9    fn from_str(s: &str) -> Result<Self, Self::Err> {
10        if s.is_empty() {
11            return Ok(Self::default());
12        }
13
14        let first_char = s.as_bytes()[0];
15
16        let config: Self = if first_char == b'{' {
17            serde_json::from_str(s)?
18        } else {
19            toml::from_str(s)?
20        };
21
22        Ok(config)
23    }
24}
25
26#[derive(thiserror::Error, Debug)]
27pub enum NetConfigFromStrError {
28    #[error(transparent)]
29    Json(#[from] serde_json::Error),
30    #[error(transparent)]
31    Toml(#[from] toml::de::Error),
32}