blob: c4e64fbc37c031ceac93c6fc048bf9dabafbfe2f [file] [log] [blame]
// 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.
// Allows presubmit to fail on warnings.
#![cfg_attr(feature = "strict", deny(warnings))]
use std::path::{Path, PathBuf};
pub mod project;
#[cfg(feature = "python")]
mod py;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("I/O error")]
Io(#[from] std::io::Error),
/// Error occurred when attempting to modify a file.
#[error("{0}: {1}")]
File(PathBuf, std::io::Error),
#[error("failed to serialize data: {0}")]
Serialization(String),
}
impl Error {
pub(crate) fn file(path: impl AsRef<Path>) -> impl FnOnce(std::io::Error) -> Self {
move |e| Self::File(path.as_ref().to_path_buf(), e)
}
}
impl From<toml::ser::Error> for Error {
fn from(e: toml::ser::Error) -> Self {
Self::Serialization(e.to_string())
}
}
impl From<toml::de::Error> for Error {
fn from(e: toml::de::Error) -> Self {
Self::Serialization(e.to_string())
}
}
#[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");
}
}