MCPcopy Create free account
hub / github.com/MegEngine/MegFlow / toml2dict

Function toml2dict

flow-rs/src/loader/python/node.rs:208–260  ·  view source on GitHub ↗
(py: Python<'a>, args: &toml::value::Table)

Source from the content-addressed store, hash-verified

206}
207
208pub fn toml2dict<'a>(py: Python<'a>, args: &toml::value::Table) -> PyResult<&'a PyDict> {
209 fn append_list(py: Python, value: &toml::Value, list: &PyList) -> PyResult<()> {
210 match value {
211 toml::Value::String(s) => list.append(s),
212 toml::Value::Float(f) => list.append(f),
213 toml::Value::Integer(i) => list.append(i),
214 toml::Value::Boolean(b) => list.append(b),
215 toml::Value::Datetime(t) => list.append(t.to_string()),
216 toml::Value::Array(l) => {
217 let pylist = PyList::empty(py);
218 for e in l {
219 append_list(py, e, pylist)?;
220 }
221 list.append(pylist)
222 }
223 toml::Value::Table(d) => {
224 let pydict = PyDict::new(py);
225 for (key, value) in d {
226 fill_dict(py, key, value, pydict)?;
227 }
228 list.append(pydict)
229 }
230 }
231 }
232 fn fill_dict(py: Python, key: &str, value: &toml::Value, dict: &PyDict) -> PyResult<()> {
233 match value {
234 toml::Value::String(s) => dict.set_item(key, s),
235 toml::Value::Float(f) => dict.set_item(key, f),
236 toml::Value::Integer(i) => dict.set_item(key, i),
237 toml::Value::Boolean(b) => dict.set_item(key, b),
238 toml::Value::Datetime(t) => dict.set_item(key, t.to_string()),
239 toml::Value::Array(l) => {
240 let pylist = PyList::empty(py);
241 for e in l {
242 append_list(py, e, pylist)?;
243 }
244 dict.set_item(key, pylist)
245 }
246 toml::Value::Table(d) => {
247 let pydict = PyDict::new(py);
248 for (key, value) in d {
249 fill_dict(py, key, value, pydict)?;
250 }
251 dict.set_item(key, pydict)
252 }
253 }
254 }
255 let dict = PyDict::new(py);
256 for (key, value) in args {
257 fill_dict(py, key, value, dict)?;
258 }
259 Ok(dict)
260}

Callers 2

submitMethod · 0.85
newMethod · 0.85

Calls 1

fill_dictFunction · 0.85

Tested by

no test coverage detected