serde-command-opts 0.1.1

A crate for serializing command line options (structopt in reverse).
Documentation
/*
 * Copyright 2020 fsyncd, Berlin, Germany.
 *
 * 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
 *
 *     http://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.
 */

//! A crate for serializing command line options (structopt in reverse).

use serde::ser::{
    self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
    SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,
};
use snafu::Snafu;
use std::fmt::Display;

/// Errors emitted during the serialization process.
#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(display("Failed to serialize object: {}", msg))]
    SerializationError { msg: String },
}

impl serde::ser::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: Display,
    {
        Self::SerializationError {
            msg: msg.to_string(),
        }
    }
}

/// A serializer intended for converting objects into command options..
pub struct Serializer {
    /// Output list of arguments / options.
    args: Vec<String>,
    /// Mode to use for serializing boolean values.
    boolean_type: BooleanType,
}

/// Mode to use for serializing boolean values.
pub enum BooleanType {
    /// True is true and False is false.
    TrueFalse,
    /// True is on and False is off.
    OnOff,
}

impl Serializer {
    /// Construct a new command options serializer using the specified mode for boolean values.
    pub fn new(boolean_type: BooleanType) -> Self {
        Self {
            args: vec![],
            boolean_type,
        }
    }

    /// Serialize an object into command options.
    pub fn into_args<T>(mut self, value: &T) -> Result<Vec<String>, Error>
    where
        T: Serialize,
    {
        value.serialize(&mut self)?;
        Ok(self.args)
    }
}

/// Serialize an object into command options, using the default serializer settings.
pub fn to_args<T>(value: &T) -> Result<Vec<String>, Error>
where
    T: Serialize,
{
    Serializer::new(BooleanType::TrueFalse).into_args(value)
}

impl<'a> ser::Serializer for &'a mut Serializer {
    type Ok = ();
    type Error = Error;
    type SerializeSeq = Self;
    type SerializeTuple = Self;
    type SerializeTupleStruct = Self;
    type SerializeTupleVariant = Self;
    type SerializeMap = Self;
    type SerializeStruct = Self;
    type SerializeStructVariant = Self;

    fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
        self.args.push(
            match self.boolean_type {
                BooleanType::TrueFalse => {
                    if v {
                        "true"
                    } else {
                        "false"
                    }
                }
                BooleanType::OnOff => {
                    if v {
                        "on"
                    } else {
                        "off"
                    }
                }
            }
            .into(),
        );
        Ok(())
    }

    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
        self.serialize_i64(i64::from(v))
    }

    fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
        self.serialize_i64(i64::from(v))
    }

    fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
        self.serialize_i64(i64::from(v))
    }

    fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
        self.args.push(v.to_string());
        Ok(())
    }

    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
        self.serialize_u64(u64::from(v))
    }

    fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
        self.serialize_u64(u64::from(v))
    }

    fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
        self.serialize_u64(u64::from(v))
    }

    fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
        self.args.push(v.to_string());
        Ok(())
    }

    fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
        self.serialize_f64(f64::from(v))
    }

    fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
        self.args.push(v.to_string());
        Ok(())
    }

    fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
        self.serialize_str(&v.to_string())
    }

    fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
        self.args.push(v.into());
        Ok(())
    }

    fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
        unimplemented!()
    }

    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
        self.args.pop();
        self.serialize_unit()
    }

    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        value.serialize(self)
    }

    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        // NOP
        Ok(())
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
        self.serialize_unit()
    }

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        variant: &'static str,
    ) -> Result<Self::Ok, Self::Error> {
        self.serialize_str(variant)
    }

    fn serialize_newtype_struct<T: ?Sized>(
        self,
        _name: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        value.serialize(self)
    }

    fn serialize_newtype_variant<T: ?Sized>(
        self,
        _name: &'static str,
        _variant_index: u32,
        variant: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        variant.serialize(&mut *self)?;
        value.serialize(&mut *self)?;
        Ok(())
    }

    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        self.args.pop();
        Ok(self)
    }

    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
        self.serialize_seq(Some(len))
    }

    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        len: usize,
    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
        self.serialize_seq(Some(len))
    }

    fn serialize_tuple_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        variant.serialize(&mut *self)?;
        Ok(self)
    }

    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        Ok(self)
    }

    fn serialize_struct(
        self,
        _name: &'static str,
        len: usize,
    ) -> Result<Self::SerializeStruct, Self::Error> {
        self.serialize_map(Some(len))
    }

    fn serialize_struct_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant, Self::Error> {
        variant.serialize(&mut *self)?;
        Ok(self)
    }

    fn collect_str<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: Display,
    {
        unimplemented!()
    }
}

impl<'a> SerializeSeq for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a> SerializeTuple for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a> SerializeTupleStruct for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a> SerializeTupleVariant for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a> SerializeMap for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        key.serialize(&mut **self)
    }

    fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a> SerializeStruct for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T: ?Sized>(
        &mut self,
        key: &'static str,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        key.serialize(&mut **self)?;
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a> SerializeStructVariant for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T: ?Sized>(
        &mut self,
        key: &'static str,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: Serialize,
    {
        key.serialize(&mut **self)?;
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use maplit::hashmap;
    use serde::Serialize;
    use std::collections::HashMap;

    #[test]
    fn test_simple_struct() {
        #[derive(Serialize)]
        struct Test {
            signed_int: i32,
            unsigned_int: u32,
            string: String,
            character: char,
        }

        let options = to_args(&Test {
            signed_int: -10,
            unsigned_int: 10,
            string: String::from("Hello World"),
            character: 'X',
        })
        .unwrap();

        let expected: Vec<String> = vec![
            "signed_int".into(),
            "-10".into(),
            "unsigned_int".into(),
            "10".into(),
            "string".into(),
            "Hello World".into(),
            "character".into(),
            "X".into(),
        ];
        assert_eq!(options, expected);
    }

    #[test]
    fn test_nested_list() {
        #[derive(Serialize)]
        struct Test {
            example: i32,
            opts: Vec<String>,
        }

        let options = to_args(&Test {
            example: 1,
            opts: vec!["a".into(), "b".into(), "c".into()],
        })
        .unwrap();

        let expected: Vec<String> = vec![
            "example".into(),
            "1".into(),
            "a".into(),
            "b".into(),
            "c".into(),
        ];
        assert_eq!(options, expected);
    }

    #[test]
    fn test_nested_map() {
        #[derive(Serialize)]
        struct Test {
            example: i32,
            fields: HashMap<String, i32>,
        }

        let options = to_args(&Test {
            example: 1,
            fields: hashmap! {
                "a".into() => 1,
                "b".into() => 2,
            },
        })
        .unwrap();

        let expected: Vec<String> = vec![
            "example".into(),
            "1".into(),
            "fields".into(),
            "a".into(),
            "1".into(),
            "b".into(),
            "2".into(),
        ];
        assert_eq!(options, expected);
    }

    #[test]
    fn test_boolean_modes() {
        #[derive(Serialize)]
        struct Test {
            #[serde(rename = "my-feature")]
            feature: bool,
        }

        let options = Serializer::new(BooleanType::OnOff)
            .into_args(&Test { feature: true })
            .unwrap();

        let expected: Vec<String> = vec!["my-feature".into(), "on".into()];
        assert_eq!(options, expected);
    }

    #[test]
    fn test_optional_field() {
        #[derive(Serialize)]
        struct Test {
            name: Option<String>,
        }

        let options = to_args(&Test {
            name: Some("Alice".into()),
        })
        .unwrap();

        let expected: Vec<String> = vec!["name".into(), "Alice".into()];
        assert_eq!(options, expected);

        let options = to_args(&Test { name: None }).unwrap();

        let expected: Vec<String> = vec![];
        assert_eq!(options, expected);
    }
}