valid_toml/value/
string_value.rs

1/* Copyright 2016 Joshua Gentry
2 *
3 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 * option. This file may not be copied, modified, or distributed
7 * except according to those terms.
8 */
9use shareable::SharedObject;
10
11use item_value::ItemValue;
12use notify::Notify;
13
14//*************************************************************************************************
15/// This object will provide the value of an item read from the TOML file.  It's value is
16/// initialized to "" until the TOML file is actually loaded.
17#[derive(Clone)]
18pub struct StringValue
19{
20    //---------------------------------------------------------------------------------------------
21    /// The data object that actually holds the value.
22    value : SharedObject<String>
23}
24
25impl StringValue
26{
27    //---------------------------------------------------------------------------------------------
28    /// Create a new instance of the object.
29    pub fn new() -> StringValue
30    {
31        StringValue {
32            value  : SharedObject::new(String::from(""))
33        }
34    }
35
36    //---------------------------------------------------------------------------------------------
37    /// Returns a clone of the string read from the TOML file.
38    pub fn get(&self) -> String
39    {
40        String::from(self.value.get().as_str())
41    }
42}
43
44use std::fmt::{Debug, Display, Formatter, Error};
45
46impl Debug for StringValue
47{
48    //*********************************************************************************************
49    /// Implementation of Debug.
50    fn fmt(
51        &self,
52        f : &mut Formatter
53        ) -> Result<(), Error>
54    {
55        write!(f, "{:?}", self.get())
56    }
57}
58
59impl Display for StringValue
60{
61    //*********************************************************************************************
62    /// Implementation of Display.
63    fn fmt(
64        &self,
65        f : &mut Formatter
66        ) -> Result<(), Error>
67    {
68        write!(f, "{}", self.get())
69    }
70}
71
72impl Notify for StringValue
73{
74    //---------------------------------------------------------------------------------------------
75    /// This is called when the TOML file is read to set the value.
76    fn send(
77        &self,
78        value : &ItemValue
79        )
80    {
81        self.value.set(String::from(value.get_str()));
82    }
83}