| // Copyright 2022 The Pigweed Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| // use this file except in compliance with the License. You may obtain a copy of |
| // the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| // License for the specific language governing permissions and limitations under |
| // the License. |
| |
| #[cfg(feature = "python")] |
| mod py; |
| |
| #[must_use] |
| pub fn name() -> &'static str { |
| "qg" |
| } |
| |
| #[cfg(feature = "python")] |
| pub fn py_demo(code: &str) { |
| use py::Py; |
| |
| let mut py = Py::new(); |
| let py_result = py.run( |
| // Scope initialization function. |
| |vm, scope| { |
| // Add a `rust_print` function to the scope before running the code. |
| scope.globals.set_item( |
| "rust_print", |
| vm.ctx |
| .new_function("rust_print", |s: String| println!("[R] {}", s)) |
| .into(), |
| vm, |
| )?; |
| |
| Ok(()) |
| }, |
| code, |
| ); |
| |
| if let Err(e) = py_result { |
| // Placeholder until qg has an error type |
| println!("py_demo error: {:?}", e); |
| } |
| } |
| |
| #[cfg(test)] |
| mod tests { |
| use super::*; |
| |
| #[test] |
| fn it_works() { |
| assert_eq!(name(), "qg"); |
| } |
| } |