toml_write/
key.rs

1#[cfg(feature = "alloc")]
2use alloc::borrow::Cow;
3#[cfg(feature = "alloc")]
4use alloc::string::String;
5
6use crate::TomlWrite;
7
8#[cfg(feature = "alloc")]
9pub trait ToTomlKey {
10    fn to_toml_key(&self) -> String;
11}
12
13#[cfg(feature = "alloc")]
14impl<T> ToTomlKey for T
15where
16    T: WriteTomlKey + ?Sized,
17{
18    fn to_toml_key(&self) -> String {
19        let mut result = String::new();
20        let _ = self.write_toml_key(&mut result);
21        result
22    }
23}
24
25pub trait WriteTomlKey {
26    fn write_toml_key<W: TomlWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result;
27}
28
29impl WriteTomlKey for str {
30    fn write_toml_key<W: TomlWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
31        crate::TomlKeyBuilder::new(self)
32            .as_default()
33            .write_toml_key(writer)
34    }
35}
36
37#[cfg(feature = "alloc")]
38impl WriteTomlKey for String {
39    fn write_toml_key<W: TomlWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
40        self.as_str().write_toml_key(writer)
41    }
42}
43
44#[cfg(feature = "alloc")]
45impl WriteTomlKey for Cow<'_, str> {
46    fn write_toml_key<W: TomlWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
47        self.as_ref().write_toml_key(writer)
48    }
49}
50
51impl<V: WriteTomlKey + ?Sized> WriteTomlKey for &V {
52    fn write_toml_key<W: TomlWrite + ?Sized>(&self, writer: &mut W) -> core::fmt::Result {
53        (*self).write_toml_key(writer)
54    }
55}