ucglib/convert/
traits.rs

1// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
2//
3//  Licensed under the Apache License, Version 2.0 (the "License");
4//  you may not use this file except in compliance with the License.
5//  You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14
15//! The traits used by the ucg compiler for converting Val intermediate format into the output formats..
16use std::error::Error;
17use std::io::Write;
18use std::rc::Rc;
19use std::result;
20
21use crate::build::Val;
22
23pub type ConvertResult = result::Result<(), Box<dyn Error>>;
24
25pub type ImportResult = result::Result<Rc<Val>, Box<dyn Error>>;
26
27/// The trait that Converters from Val to different output formats for the
28/// final conversion stage of the ucg compiler.
29pub trait Converter {
30    fn convert(&self, vs: Rc<Val>, w: &mut dyn Write) -> ConvertResult;
31    fn file_ext(&self) -> String;
32    fn description(&self) -> String;
33    fn help(&self) -> String;
34}
35
36pub trait Importer {
37    fn import(&self, bytes: &[u8]) -> result::Result<Rc<Val>, Box<dyn Error>>;
38}