pub fn quote_if_needed<'a, Spec, WQImpl>(
    input: &'a str,
    validator: &mut WQImpl
) -> Result<Cow<'a, str>, CoreError>
    where Spec: GeneralQSSpec,
          WQImpl: WithoutQuotingValidator
{
    let mut needs_quoting_from = None;
    for (idx, ch) in input.char_indices() {
        let pcp = PartialCodePoint::from_code_point(ch as u32);
        if !validator.next(pcp) {
            needs_quoting_from = Some(idx);
            break;
        } else {
            //FIXME check if is this even enabled in the right context
            #[cfg(debug_assertions)]
            {
                use self::QuotingClass::*;
                match Spec::Quoting::classify_for_quoting(pcp) {
                    QText => {},
                    Invalid => panic!(concat!("[BUG] representable without quoted string,",
                                            "but invalid in quoted string: {}"), ch),
                    NeedsQuoting => panic!(concat!("[BUG] representable without quoted string,",
                                            "but not without escape in quoted string: {}"), ch)
                }
            }
        }
    }

    let start_quoting_from =
        if input.len() == 0 {
            0
        } else if let Some(offset) = needs_quoting_from {
            offset
        } else {
            return if validator.end() {
                Ok(Cow::Borrowed(input))
            } else {
                let mut out = String::with_capacity(input.len() + 2);
                out.push('"');
                out.push_str(input);
                out.push('"');
                Ok(Cow::Owned(out))
            };
        };


    let mut out = String::with_capacity(input.len() + 3);
    out.push('"');
    out.push_str(&input[0..start_quoting_from]);
    quote_inner::<Spec>(&input[start_quoting_from..], &mut out)?;
    out.push('"');
    Ok(Cow::Owned(out))
}
/// quotes the input string if needed
///
/// The `validator` decides if the value is valid without
/// quoting it, the `Spec` type decides how quoting is done if
/// needed. The `Spec` only specifies the format of quoting
/// e.g. which values are allowed in a quoted-string but
/// wether or not a string needs quoting can often depend
/// on additional factor.
///
/// Note that this implementation expects the
/// validator and spec to be in sync, i.e. what
/// is valid without quoting does not need to
/// be escaped when appearing in a quoted string.
///
/// # Example
///
/// ```
/// # use std::borrow::Cow;
/// // use your own Spec
/// use quoted_string::test_utils::{TestSpec, TestUnquotedValidator};
/// use quoted_string::quote_if_needed;
///
/// let mut without_quoting = TestUnquotedValidator::new();
/// let quoted = quote_if_needed::<TestSpec, _>("simple", &mut without_quoting)
///     .expect("only fails if input can not be represented as quoted string with used Spec");
///
/// // The used spec states a 6 character us-ascii word does not need to be represented as
/// // quoted string
/// assert_eq!(quoted, Cow::Borrowed("simple"));
///
/// let mut without_quoting = TestUnquotedValidator::new();
/// let quoted2 = quote_if_needed::<TestSpec, _>("more complex", &mut without_quoting).unwrap();
/// let expected: Cow<'static, str> = Cow::Owned("\"more complex\"".into());
/// assert_eq!(quoted2, expected);
/// ```
///
quotes the input string if needed
The `validator` decides if the value is valid without
quoting it, the `Spec` type decides how quoting is done if
needed. The `Spec` only specifies the format of quoting
e.g. which values are allowed in a quoted-string but
wether or not a string needs quoting can often depend
on additional factor.

Note that this implementation expects the
validator and spec to be in sync, i.e. what
is valid without quoting does not need to
be escaped when appearing in a quoted string.
quotes the input string if needed The ` validator ` decides if the value is valid without quoting it the ` Spec ` type decides how quoting is done if needed . The ` Spec ` only specifies the format of quoting e . g . which values are allowed in a quoted - string but wether or not a string needs quoting can often depend on additional factor . Note that this implementation expects the validator and spec to be in sync i . e . what is valid without quoting does not need to be escaped when appearing in a quoted string .
pub fn quote_if_needed<'a, Spec, WQImpl>(
    input: &'a str,
    validator: &mut WQImpl
) -> Result<Cow<'a, str>, CoreError>
    where Spec: GeneralQSSpec,
          WQImpl: WithoutQuotingValidator
{
    let mut needs_quoting_from = None;
    for (idx, ch) in input.char_indices() {
        let pcp = PartialCodePoint::from_code_point(ch as u32);
        if !validator.next(pcp) {
            needs_quoting_from = Some(idx);
            break;
        } else {
            //FIXME check if is this even enabled in the right context
            #[cfg(debug_assertions)]
            {
                use self::QuotingClass::*;
                match Spec::Quoting::classify_for_quoting(pcp) {
                    QText => {},
                    Invalid => panic!(concat!("[BUG] representable without quoted string,",
                                            "but invalid in quoted string: {}"), ch),
                    NeedsQuoting => panic!(concat!("[BUG] representable without quoted string,",
                                            "but not without escape in quoted string: {}"), ch)
                }
            }
        }
    }
    let start_quoting_from =
        if input.len() == 0 {
            0
        } else if let Some(offset) = needs_quoting_from {
            offset
        } else {
            return if validator.end() {
                Ok(Cow::Borrowed(input))
            } else {
                let mut out = String::with_capacity(input.len() + 2);
                out.push('"');
                out.push_str(input);
                out.push('"');
                Ok(Cow::Owned(out))
            };
        };
    let mut out = String::with_capacity(input.len() + 3);
    out.push('"');
    out.push_str(&input[0..start_quoting_from]);
    quote_inner::<Spec>(&input[start_quoting_from..], &mut out)?;
    out.push('"');
    Ok(Cow::Owned(out))
}
pub fn quote_if_needed < ' a , Spec , WQImpl > ( input : & ' a str , validator : & mut WQImpl ) -> Result < Cow < ' a , str > , CoreError > where Spec : GeneralQSSpec , WQImpl : WithoutQuotingValidator { let mut needs_quoting_from = None ; for ( idx , ch ) in input . char_indices ( ) { let pcp = PartialCodePoint :: from_code_point ( ch as u32 ) ; if ! validator . next ( pcp ) { needs_quoting_from = Some ( idx ) ; break ; } else { //FIXME check if is this even enabled in the right context # [ cfg ( debug_assertions ) ] { use self :: QuotingClass :: * ; match Spec :: Quoting :: classify_for_quoting ( pcp ) { QText => { } , Invalid => panic ! ( concat ( " " " " ) ch ) , NeedsQuoting => panic ! ( concat ( " " " " ) ch ) } } } } let start_quoting_from = if input . len ( ) == 0 { 0 } else if let Some ( offset ) = needs_quoting_from { offset } else { return if validator . end ( ) { Ok ( Cow :: Borrowed ( input ) ) } else { let mut out = String :: with_capacity ( input . len ( ) + 2 ) ; out . push ( '"' ) ; out . push_str ( input ) ; out . push ( '"' ) ; Ok ( Cow :: Owned ( out ) ) } ; } ; let mut out = String :: with_capacity ( input . len ( ) + 3 ) ; out . push ( '"' ) ; out . push_str ( & input [ 0 .. start_quoting_from ] ) ; quote_inner :: < Spec > ( & input [ start_quoting_from .. ] , & mut out ) ? ; out . push ( '"' ) ; Ok ( Cow :: Owned ( out ) ) }
quotes the input string if needed
The `validator` decides if the value is valid without
quoting it, the `Spec` type decides how quoting is done if
needed.
quotes the input string if needed The ` validator ` decides if the value is valid without quoting it the ` Spec ` type decides how quoting is done if needed .
input
&'a str
validator
&mut WQImpl
input
&'a str
validator
&mut WQImpl
example
None
use std::borrow::cow;


let mut without_quoting = TestUnquotedValidator::new();
let quoted = quote_if_needed::("simple", &mut without_quoting)
.expect("only fails if input can not be represented as quoted string with used Spec").

The used spec states a 6 character us-ascii word does not need to be represented as
quoted string
assert_eq!(quoted, Cow::Borrowed("simple")).

let mut without_quoting = TestUnquotedValidator::new();
let quoted2 = quote_if_needed::("more complex", &mut without_quoting).unwrap();
let expected: Cow<'static, str> = Cow::Owned("\"more complex\"".into());
assert_eq!(quoted2, expected);
```
let mut without_quoting = TestUnquotedValidator :: new () ; let quoted = quote_if_needed :: ( " simple " &mut without_quoting ) . expect ( " only fails if input can not be represented as quoted string with used Spec " ) . The used spec states a 6 character us - ascii word does not need to be represented as quoted string assert_eq! ( quoted Cow :: Borrowed ( " simple " )) . let mut without_quoting = TestUnquotedValidator :: new () ; let quoted2 = quote_if_needed :: ( " more complex " &mut without_quoting ) . unwrap () ; let expected : Cow< ' static str > = Cow :: Owned ( " \ " more complex \ " " . into () ) ; assert_eq! ( quoted2 expected ) ; ` ` `