capability_example/
try_filling_from_clipboard.rs

1// ---------------- [ File: capability-example/src/try_filling_from_clipboard.rs ]
2crate::ix!();
3
4impl PartiallyGrownModel {
5
6    /// Attempts to fill the next missing field from the clipboard, stopping after the first attempt.
7    #[tracing::instrument(level = "trace", skip_all)]
8    pub fn try_filling_next_none_field_from_clipboard(&mut self) -> bool {
9
10        trace!("self={:#?}",self);
11
12        let mut did_fill = false;
13
14        // 1) JustifiedGrowerTreeConfiguration
15        if self.maybe_ungrown_justified_grower_tree_configuration().is_none() {
16            trace!("attempting parse clipboard as JustifiedGrowerTreeConfiguration");
17            match fuzzy_parse_clipboard_contents::<JustifiedGrowerTreeConfiguration>(false) {
18                Ok(val) => {
19                    trace!("Clipboard provided a valid JustifiedGrowerTreeConfiguration => setting it.");
20                    self.set_maybe_ungrown_justified_grower_tree_configuration(Some(val));
21                    did_fill = true;
22                }
23                Err(e) => {
24                    warn!("Fuzzy parse for JustifiedGrowerTreeConfiguration from clipboard failed => leaving None. Error: {:?}", e);
25                }
26            }
27            return did_fill;
28        }
29
30        // 2) JustifiedStringSkeleton
31        if self.maybe_ungrown_justified_string_skeleton().is_none() {
32            trace!("attempting parse clipboard as JustifiedStringSkeleton");
33            match fuzzy_parse_clipboard_contents::<JustifiedStringSkeleton>(false) {
34                Ok(val) => {
35                    trace!("Clipboard provided a valid JustifiedStringSkeleton => setting it.");
36                    self.set_maybe_ungrown_justified_string_skeleton(Some(val));
37                    did_fill = true;
38                }
39                Err(e) => {
40                    warn!("Fuzzy parse for JustifiedStringSkeleton from clipboard failed => leaving None. Error: {:?}", e);
41                }
42            }
43            return did_fill;
44        }
45
46        // 3) StrippedStringSkeleton (precise parse)
47        if self.maybe_ungrown_stripped_string_skeleton().is_none() {
48            trace!("attempting parse clipboard as StrippedStringSkeleton");
49            match (|| -> Result<StrippedStringSkeleton, std::io::Error> {
50                let mut ctx = ClipboardContext::new().map_err(|e| {
51                    std::io::Error::new(std::io::ErrorKind::Other, format!("Clipboard context error: {e}"))
52                })?;
53                let contents = ctx.get_contents().map_err(|e| {
54                    std::io::Error::new(std::io::ErrorKind::Other, format!("Clipboard get_contents error: {e}"))
55                })?;
56                let json_val: serde_json::Value = serde_json::from_str(&contents).map_err(|e| {
57                    std::io::Error::new(std::io::ErrorKind::InvalidData, format!("Clipboard JSON parsing error: {e}"))
58                })?;
59                let parsed = try_deserialize_with_path::<StrippedStringSkeleton>(&json_val).map_err(|e| {
60                    std::io::Error::new(std::io::ErrorKind::InvalidData, format!("StrippedStringSkeleton parse error from clipboard: {e}"))
61                })?;
62                Ok(parsed)
63            })() {
64                Ok(parsed) => {
65                    trace!("Clipboard provided a valid StrippedStringSkeleton => setting it.");
66                    self.set_maybe_ungrown_stripped_string_skeleton(Some(parsed));
67                    did_fill = true;
68                }
69                Err(e) => {
70                    warn!("Parse for StrippedStringSkeleton from clipboard failed => leaving None. Error: {e}");
71                }
72            }
73            return did_fill;
74        }
75
76        // 4) CoreStringSkeleton
77        if self.maybe_ungrown_core_string_skeleton().is_none() {
78            trace!("attempting parse clipboard as CoreStringSkeleton");
79            match fuzzy_parse_clipboard_contents::<CoreStringSkeleton>(false) {
80                Ok(val) => {
81                    trace!("Clipboard provided a valid CoreStringSkeleton => setting it.");
82                    self.set_maybe_ungrown_core_string_skeleton(Some(val));
83                    did_fill = true;
84                }
85                Err(e) => {
86                    warn!("Fuzzy parse for CoreStringSkeleton from clipboard failed => leaving None. Error: {:?}", e);
87                }
88            }
89            return did_fill;
90        }
91
92        // 5) AnnotatedLeafHolderExpansions
93        if self.maybe_ungrown_annotated_leaf_holder_expansions().is_none() {
94            trace!("attempting parse clipboard as AnnotatedLeafHolderExpansions");
95            match fuzzy_parse_clipboard_contents::<AnnotatedLeafHolderExpansions>(false) {
96                Ok(val) => {
97                    trace!("Clipboard provided a valid AnnotatedLeafHolderExpansions => setting it.");
98                    self.set_maybe_ungrown_annotated_leaf_holder_expansions(Some(val));
99                    did_fill = true;
100                }
101                Err(e) => {
102                    warn!("Fuzzy parse for AnnotatedLeafHolderExpansions failed => leaving None. Error: {:?}", e);
103                }
104            }
105            return did_fill;
106        }
107
108        trace!("All optional fields are already set; nothing to fill from clipboard.");
109        did_fill
110    }
111}