1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (C) 2022 Lily Lyons
//
// This file is part of alox-48.
//
// alox-48 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// alox-48 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with alox-48.  If not, see <http://www.gnu.org/licenses/>.

use serde::de::Error as SerdeError;
use serde::de::{MapAccess, Unexpected, Visitor};

use super::VisitorExt;
use crate::value::RbFields;

impl<'de, T> VisitorExt<'de> for T
where
    T: Visitor<'de>,
{
    default fn visit_userdata<E>(self, _class: &'de str, _data: &'de [u8]) -> Result<Self::Value, E>
    where
        E: SerdeError,
    {
        Err(SerdeError::invalid_type(
            Unexpected::Other("userdata"),
            &self,
        ))
    }

    default fn visit_object<A>(self, _class: &'de str, fields: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        self.visit_map(fields)
    }

    default fn visit_symbol<E>(self, sym: &'de str) -> Result<Self::Value, E>
    where
        E: SerdeError,
    {
        self.visit_borrowed_str(sym)
    }

    default fn visit_ruby_string<A>(
        self,
        str: &'de [u8],
        fields: A,
    ) -> Result<Self::Value, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        if !str.is_empty() {
            use crate::Value;
            use serde::Deserialize;

            let de = serde::de::value::MapAccessDeserializer::new(fields);
            let fields = RbFields::deserialize(de)?;

            match fields.get("E").or_else(|| fields.get("encoding")) {
                Some(f) => match f {
                    Value::Bool(b) if !*b => {
                        eprintln!("warning: converting ascii ruby string to utf8");
                    }
                    Value::Bool(b) if *b => {}
                    Value::String(s) => {
                        eprintln!(
                            "warning: converting non-utf8 ruby string to utf8: {}",
                            s.to_string_lossy()
                        );
                    }
                    v => eprintln!("warning: unexpected encoding type on ruby string: {v:?}"),
                },
                None => eprintln!(
                    "warning: converting ruby string with no encoding (likely binary data) to utf8"
                ),
            }
        }

        let str = String::from_utf8_lossy(str);

        match str {
            std::borrow::Cow::Borrowed(str) => self.visit_borrowed_str(str),
            std::borrow::Cow::Owned(str) => self.visit_string(str),
        }
    }
}

/// Default implementation for [`VisitorExt`].
impl<'de> VisitorExt<'de> for serde::de::IgnoredAny {
    fn visit_userdata<E>(self, _class: &'de str, _data: &'de [u8]) -> Result<Self::Value, E>
    where
        E: SerdeError,
    {
        Ok(serde::de::IgnoredAny)
    }

    fn visit_object<A>(self, _class: &'de str, _fields: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        Ok(serde::de::IgnoredAny)
    }

    fn visit_symbol<E>(self, _sym: &'de str) -> Result<Self::Value, E>
    where
        E: SerdeError,
    {
        Ok(serde::de::IgnoredAny)
    }

    fn visit_ruby_string<A>(self, _str: &'de [u8], _fields: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        Ok(serde::de::IgnoredAny)
    }
}