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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
use std::{
    collections::HashMap,
    env, fmt, fs,
    ops::Not,
    path::{Path, PathBuf},
    sync::Arc,
};

use anyhow::format_err;
use arci::{JointTrajectoryClient, Localization, MoveBase, Navigation, Speaker};
#[cfg(feature = "ros")]
use arci_ros::{
    RosCmdVelMoveBase, RosCmdVelMoveBaseConfig, RosControlActionClientConfig,
    RosControlClientConfig, RosEspeakClient, RosEspeakClientConfig, RosLocalizationClient,
    RosLocalizationClientConfig, RosNavClient, RosNavClientConfig,
};
use arci_speak_audio::AudioSpeaker;
use arci_speak_cmd::LocalCommand;
use arci_urdf_viz::{UrdfVizWebClient, UrdfVizWebClientConfig};
use openrr_client::{OpenrrClientsConfig, PrintSpeaker, RobotClient};
use openrr_plugin::PluginProxy;
use openrr_tracing::Tracing;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::{debug, error, info};

use crate::Error;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "kebab-case")]
pub enum BuiltinClient {
    /// [ROS1](https://ros.org)
    Ros,
    /// [urdf-viz](https://github.com/openrr/urdf-viz)
    UrdfViz,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum ClientKind {
    // Use builtin client, ros or urdf-viz.
    Builtin(BuiltinClient),
    // Use plugin.
    Plugin(String),
    // true: auto-selection
    // false: disable
    Auto(bool),
}

impl ClientKind {
    /// Returns true if the use of the ros client is explicitly specified.
    ///
    /// This is always false when ros feature is disabled. (ensured by validate_ros_config)
    #[cfg(feature = "ros")]
    fn is_builtin_ros(&self) -> bool {
        matches!(self, Self::Builtin(BuiltinClient::Ros))
    }

    fn is_auto(&self) -> bool {
        matches!(self, Self::Auto(true))
    }
}

impl Default for ClientKind {
    fn default() -> Self {
        Self::Auto(true)
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", content = "args")]
#[serde(deny_unknown_fields)]
#[non_exhaustive] // The variants will increase depending on the feature flag.
pub enum SpeakConfig {
    #[default]
    Print,
    Command,
    #[cfg(feature = "ros")]
    RosEspeak {
        config: RosEspeakClientConfig,
    },
    // Not public API.
    // A dummy variant to catch that there is a config that requires the ros feature.
    #[doc(hidden)]
    #[cfg(not(feature = "ros"))]
    RosEspeak {
        #[schemars(schema_with = "unimplemented_schema")]
        config: toml::Value,
    },
    Audio {
        map: HashMap<String, PathBuf>,
    },
}

impl SpeakConfig {
    pub fn build(&self) -> Result<Box<dyn Speaker>, Error> {
        Ok(match self {
            SpeakConfig::RosEspeak { config } => self.create_ros_espeak_client(config),
            SpeakConfig::Audio { map } => self.create_audio_speaker(map.clone())?,
            SpeakConfig::Command => self.create_local_command_speaker(),
            SpeakConfig::Print => self.create_print_speaker(),
        })
    }

    fn create_print_speaker(&self) -> Box<dyn Speaker> {
        Box::new(arci::Lazy::new(move || {
            debug!("create_print_speaker: creating PrintSpeaker");
            Ok(PrintSpeaker::new())
        }))
    }

    fn create_local_command_speaker(&self) -> Box<dyn Speaker> {
        Box::new(arci::Lazy::new(move || {
            debug!("create_local_command_speaker: creating LocalCommand");
            Ok(LocalCommand::new())
        }))
    }

    fn create_audio_speaker(
        &self,
        mut hash_map: HashMap<String, PathBuf>,
    ) -> Result<Box<dyn Speaker>, Error> {
        for path in hash_map.values_mut() {
            *path = openrr_config::evaluate(path.to_str().unwrap(), None)
                .map_err(arci::Error::Other)?
                .into();
        }
        Ok(Box::new(arci::Lazy::new(move || {
            debug!("create_audio_speaker: creating AudioSpeaker");
            Ok(AudioSpeaker::new(hash_map))
        })))
    }

    #[cfg(feature = "ros")]
    fn create_ros_espeak_client(&self, config: &RosEspeakClientConfig) -> Box<dyn Speaker> {
        let topic = config.topic.to_string();
        Box::new(arci::Lazy::new(move || {
            debug!("create_ros_espeak_client: creating RosEspeakClient");
            Ok(RosEspeakClient::new(&topic))
        }))
    }

    #[cfg(not(feature = "ros"))]
    fn create_ros_espeak_client(&self, _config: &toml::Value) -> Box<dyn Speaker> {
        unreachable!()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PluginConfig {
    /// Path to the plugin. If no extension is specified, the default extension
    /// for `cdylib` on the current OS will be selected.
    /// (linux: `.so`, macos: `.dylib`, windows: `.dll`)
    pub path: PathBuf,
    pub instances: Vec<PluginInstance>,
}

impl PluginConfig {
    fn find_instances_by_name<'a>(
        map: &'a HashMap<String, Self>,
        instance_name: &'a str,
        instance_kind: PluginInstanceKind,
    ) -> impl Iterator<Item = (&'a str, &'a PluginInstance)> {
        map.iter().flat_map(move |(plugin_name, plugin_config)| {
            plugin_config
                .instances
                .iter()
                .filter(move |instance| {
                    instance.name == instance_name && instance.type_ == instance_kind
                })
                .map(move |instance| (plugin_name.as_str(), instance))
        })
    }

    fn find_instances_by_kind(
        map: &HashMap<String, Self>,
        instance_kind: PluginInstanceKind,
    ) -> impl Iterator<Item = (&str, &PluginInstance)> {
        map.iter().flat_map(move |(plugin_name, plugin_config)| {
            plugin_config
                .instances
                .iter()
                .filter(move |instance| instance.type_ == instance_kind)
                .map(move |instance| (plugin_name.as_str(), instance))
        })
    }

    fn resolve_instance<'a>(
        map: &'a HashMap<String, Self>,
        instance_name: Option<&'a str>,
        instance_kind: PluginInstanceKind,
    ) -> Result<(&'a str, &'a PluginInstance), Error> {
        let instances: Vec<_> = if let Some(instance_name) = instance_name {
            Self::find_instances_by_name(map, instance_name, instance_kind).collect()
        } else {
            Self::find_instances_by_kind(map, instance_kind).collect()
        };

        if instances.is_empty() {
            return Err(Error::NoPluginInstance {
                name: instance_name.unwrap_or_default().to_string(),
                kind: instance_kind.to_string(),
            });
        }
        if instances.len() == 1 {
            return Ok(instances[0]);
        }

        if let Some(instance_name) = instance_name {
            Err(Error::DuplicateInstance(format!(
                "Multiple {instance_kind:?} plugin instances {instance_name:?} are found. Consider renaming one of the instances",
            )))
        } else {
            Err(Error::DuplicateInstance(format!(
                "Multiple plugin instances for {instance_kind:?} are found. Consider specifying the instance to use",
            )))
        }
    }
}

#[allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/13170
pub(crate) fn resolve_plugin_path(
    plugin_path: &mut PathBuf,
    base_path: impl AsRef<Path>,
) -> Result<(), Error> {
    *plugin_path = openrr_client::resolve_relative_path(base_path, &plugin_path)?;
    if plugin_path.extension().is_none() {
        plugin_path.set_extension(env::consts::DLL_EXTENSION);
    }
    Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PluginInstance {
    /// Name of this plugin instance.
    pub name: String,
    /// Trait kind of this instance.
    #[serde(rename = "type")]
    pub type_: PluginInstanceKind,
    /// Arguments passed when creating this instance.
    pub args: Option<String>,
    /// Pass the contents of the specified file as an argument.
    pub args_from_path: Option<PathBuf>,
}

impl PluginInstance {
    pub fn load_args(&self) -> Result<String, Error> {
        if let Some(path) = &self.args_from_path {
            fs::read_to_string(path).map_err(|e| Error::NoFile(path.to_owned(), e))
        } else {
            Ok(self.args.clone().unwrap_or_default())
        }
    }

    fn create_lazy_instance<T, F>(
        &self,
        plugins: &mut PluginMap,
        plugin_name: &str,
        f: F,
    ) -> Result<arci::Lazy<'static, T>, Error>
    where
        T: fmt::Debug,
        F: FnOnce(&PluginProxy, String) -> Result<Option<T>, arci::Error> + Send + Sync + 'static,
    {
        let plugin = if let Some(plugin) = plugins.load(plugin_name)? {
            plugin
        } else {
            return Err(Error::NoPluginInstance {
                name: plugin_name.to_string(),
                kind: self.type_.to_string(),
            });
        };
        let args = self.load_args()?;
        let plugin_name = plugin_name.to_string();
        let instance_name = self.name.clone();
        let instance_kind = self.type_;
        Ok(arci::Lazy::new(move || match f(&plugin, args) {
            Ok(Some(instance)) => {
                info!(
                    "created `{instance_kind:?}` instance `{instance_name}` from plugin `{plugin_name}`",
                );
                Ok(instance)
            }
            res => instance_create_error(res, instance_kind, instance_name, plugin_name)?,
        }))
    }
}

/// Trait kind of the instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub enum PluginInstanceKind {
    JointTrajectoryClient,
    Localization,
    MoveBase,
    Navigation,
    Speaker,
}

impl fmt::Display for PluginInstanceKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

#[derive(Debug, Default)]
pub struct PluginMap {
    path: HashMap<String, PathBuf>,
    cache: HashMap<String, Arc<PluginProxy>>,
}

impl PluginMap {
    pub fn load(&mut self, name: impl AsRef<str>) -> Result<Option<Arc<PluginProxy>>, arci::Error> {
        let name = name.as_ref();
        if let Some((name, path)) = self.path.remove_entry(name) {
            let plugin = Arc::new(PluginProxy::from_path(path)?);
            self.cache.insert(name, plugin.clone());
            Ok(Some(plugin))
        } else {
            Ok(self.cache.get(name).cloned())
        }
    }
}

impl<S: Into<String>, P: Into<PathBuf>> FromIterator<(S, P)> for PluginMap {
    fn from_iter<T: IntoIterator<Item = (S, P)>>(iter: T) -> Self {
        let path: HashMap<_, _> = iter
            .into_iter()
            .map(|(name, path)| (name.into(), path.into()))
            .collect();
        Self {
            cache: HashMap::with_capacity(path.len() / 2),
            path,
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive] // The fields will increase depending on the feature flag.
pub struct RobotConfig {
    // TOML format has a restriction that if a table itself contains tables,
    // all keys with non-table values must be emitted first.
    // Therefore, these fields must be located at the start of the struct.
    /// Joint trajectory clients to be used.
    pub joint_trajectory_clients: Option<Vec<String>>,
    /// Speakers to be used.
    pub speakers: Option<Vec<String>>,
    /// Localization to be used. `"ros"`, `"urdf-viz"`, `false`, or plugin instance name.
    #[serde(skip_serializing_if = "ClientKind::is_auto")]
    #[serde(default)]
    pub localization: ClientKind,
    /// MoveBase to be used. `"ros"`, `"urdf-viz"`, `false`, or plugin instance name.
    #[serde(skip_serializing_if = "ClientKind::is_auto")]
    #[serde(default)]
    pub move_base: ClientKind,
    /// Navigation to be used. `"ros"`, `"urdf-viz"`, `false`, or plugin instance name.
    #[serde(skip_serializing_if = "ClientKind::is_auto")]
    #[serde(default)]
    pub navigation: ClientKind,

    #[cfg(feature = "ros")]
    #[serde(default)]
    // https://github.com/alexcrichton/toml-rs/issues/258
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub ros_clients_configs: Vec<RosControlClientConfig>,
    // A dummy field to catch that there is a config that requires the ros feature.
    #[cfg(not(feature = "ros"))]
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[schemars(schema_with = "unimplemented_schema")]
    ros_clients_configs: Vec<toml::Value>,

    #[cfg(feature = "ros")]
    #[serde(default)]
    // https://github.com/alexcrichton/toml-rs/issues/258
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub ros_action_clients_configs: Vec<RosControlActionClientConfig>,
    // A dummy field to catch that there is a config that requires the ros feature.
    #[cfg(not(feature = "ros"))]
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[schemars(schema_with = "unimplemented_schema")]
    ros_action_clients_configs: Vec<toml::Value>,

    #[serde(default)]
    // https://github.com/alexcrichton/toml-rs/issues/258
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub urdf_viz_clients_configs: Vec<UrdfVizWebClientConfig>,

    #[serde(default)]
    pub speak_configs: HashMap<String, SpeakConfig>,

    #[cfg(feature = "ros")]
    pub ros_cmd_vel_move_base_client_config: Option<RosCmdVelMoveBaseConfig>,
    // A dummy field to catch that there is a config that requires the ros feature.
    #[cfg(not(feature = "ros"))]
    #[schemars(schema_with = "unimplemented_schema")]
    ros_cmd_vel_move_base_client_config: Option<toml::Value>,

    #[cfg(feature = "ros")]
    pub ros_navigation_client_config: Option<RosNavClientConfig>,
    // A dummy field to catch that there is a config that requires the ros feature.
    #[cfg(not(feature = "ros"))]
    #[schemars(schema_with = "unimplemented_schema")]
    ros_navigation_client_config: Option<toml::Value>,

    #[cfg(feature = "ros")]
    pub ros_localization_client_config: Option<RosLocalizationClientConfig>,
    // A dummy field to catch that there is a config that requires the ros feature.
    #[cfg(not(feature = "ros"))]
    #[schemars(schema_with = "unimplemented_schema")]
    ros_localization_client_config: Option<toml::Value>,

    #[serde(default)]
    pub openrr_clients_config: OpenrrClientsConfig,

    #[serde(default)]
    pub openrr_tracing_config: OpenrrTracingConfig,

    #[serde(default)]
    pub plugins: HashMap<String, PluginConfig>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct OpenrrTracingConfig {
    /// Enable openrr-tracing for Localization. `true or `false` (default to `false`).
    #[serde(skip_serializing_if = "Not::not")]
    #[serde(default)]
    pub localization: bool,
    /// Enable openrr-tracing for MoveBase. `true or `false` (default to `false`).
    #[serde(skip_serializing_if = "Not::not")]
    #[serde(default)]
    pub move_base: bool,
    /// Enable openrr-tracing for Navigation. `true or `false` (default to `false`).
    #[serde(skip_serializing_if = "Not::not")]
    #[serde(default)]
    pub navigation: bool,
}

// Creates dummy schema for dummy fields.
#[cfg(not(feature = "ros"))]
fn unimplemented_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
    unimplemented!()
}

impl RobotConfig {
    const DEFAULT_SPEAKER_NAME: &'static str = "Default";

    /// Returns true if ros-related configs exist, or it is explicitly specified
    /// that the ros client is to be used.
    ///
    /// This is always false when ros feature is disabled.
    #[cfg(feature = "ros")]
    pub fn has_ros_clients(&self) -> bool {
        let mut has_ros_clients = false;
        let speak_configs = self.speak_configs.clone();
        for (_, speak_config) in speak_configs {
            has_ros_clients |= matches!(speak_config, SpeakConfig::RosEspeak { .. });
        }
        has_ros_clients |= !self.ros_clients_configs.is_empty();
        has_ros_clients |= !self.ros_action_clients_configs.is_empty();
        has_ros_clients |= self.move_base.is_builtin_ros()
            || (self.move_base.is_auto() && self.ros_cmd_vel_move_base_client_config.is_some());
        has_ros_clients |= self.navigation.is_builtin_ros()
            || (self.navigation.is_auto() && self.ros_navigation_client_config.is_some());
        has_ros_clients |= self.localization.is_builtin_ros()
            || (self.localization.is_auto() && self.ros_localization_client_config.is_some());
        has_ros_clients
    }

    #[cfg(not(feature = "ros"))]
    pub fn has_ros_clients(&self) -> bool {
        false
    }

    pub fn create_robot_client<L, M, N>(&self) -> Result<RobotClient<L, M, N>, Error>
    where
        L: Localization + From<Box<dyn Localization>>,
        M: MoveBase + From<Box<dyn MoveBase>>,
        N: Navigation + From<Box<dyn Navigation>>,
    {
        let mut plugins: PluginMap = self
            .plugins
            .iter()
            .map(|(plugin_name, config)| (plugin_name, &config.path))
            .collect();

        let joint_trajectory_clients = self.create_raw_joint_trajectory_clients(&mut plugins)?;
        let speakers = self.create_speakers(&mut plugins)?;
        let localization = self.create_localization(&mut plugins)?;
        let move_base = self.create_move_base(&mut plugins)?;
        let navigation = self.create_navigation(&mut plugins)?;

        Ok(RobotClient::new(
            self.openrr_clients_config.clone(),
            joint_trajectory_clients,
            speakers,
            localization.map(L::from),
            move_base.map(M::from),
            navigation.map(N::from),
        )?)
    }

    fn create_localization_urdf_viz(&self) -> Box<dyn Localization> {
        let loc = arci::Lazy::new(move || {
            debug!("create_localization_urdf_viz: creating UrdfVizWebClient");
            Ok(UrdfVizWebClient::default())
        });
        if self.openrr_tracing_config.localization {
            Box::new(Tracing::new(loc))
        } else {
            Box::new(loc)
        }
    }

    #[cfg(feature = "ros")]
    fn create_localization_ros(&self) -> Option<Box<dyn Localization>> {
        let config = self.ros_localization_client_config.clone()?;
        let loc = arci::Lazy::new(move || {
            debug!("create_localization_ros: creating RosLocalizationClient");
            Ok(RosLocalizationClient::new_from_config(config))
        });
        if self.openrr_tracing_config.localization {
            Some(Box::new(Tracing::new(loc)))
        } else {
            Some(Box::new(loc))
        }
    }

    #[cfg(not(feature = "ros"))]
    fn create_localization_ros(&self) -> Option<Box<dyn Localization>> {
        unreachable!()
    }

    fn create_localization(
        &self,
        plugins: &mut PluginMap,
    ) -> Result<Option<Box<dyn Localization>>, Error> {
        let (plugin_name, instance) = match &self.localization {
            ClientKind::Auto(false) => return Ok(None),
            ClientKind::Auto(true) => {
                if self.ros_localization_client_config.is_some() {
                    return Ok(self.create_localization_ros());
                }
                match PluginConfig::resolve_instance(
                    &self.plugins,
                    None,
                    PluginInstanceKind::Localization,
                ) {
                    Err(Error::NoPluginInstance { .. }) => {
                        // If ros is already used, it would *not* usually be
                        // assumed that urdf-viz would also be used.
                        // Users who want to use both at the same time need to
                        // specify it explicitly by `localization = "urdf-viz"`.
                        if self.has_ros_clients() {
                            return Ok(None);
                        }
                        return Ok(Some(self.create_localization_urdf_viz()));
                    }
                    res => res?,
                }
            }
            ClientKind::Builtin(BuiltinClient::Ros) => {
                return Ok(self.create_localization_ros());
            }
            ClientKind::Builtin(BuiltinClient::UrdfViz) => {
                return Ok(Some(self.create_localization_urdf_viz()));
            }
            ClientKind::Plugin(instance_name) => PluginConfig::resolve_instance(
                &self.plugins,
                Some(instance_name),
                PluginInstanceKind::Localization,
            )?,
        };

        Ok(Some(Box::new(instance.create_lazy_instance(
            plugins,
            plugin_name,
            PluginProxy::new_localization,
        )?)))
    }

    fn create_navigation_urdf_viz(&self) -> Box<dyn Navigation> {
        let nav = arci::Lazy::new(move || {
            debug!("create_navigation_urdf_viz: creating UrdfVizWebClient");
            Ok(UrdfVizWebClient::default())
        });
        if self.openrr_tracing_config.navigation {
            Box::new(Tracing::new(nav))
        } else {
            Box::new(nav)
        }
    }

    #[cfg(feature = "ros")]
    fn create_navigation_ros(&self) -> Option<Box<dyn Navigation>> {
        let config = self.ros_navigation_client_config.clone()?;
        let nav = arci::Lazy::new(move || {
            debug!("create_navigation_ros: creating RosNavClient");
            Ok(RosNavClient::new_from_config(config))
        });
        if self.openrr_tracing_config.navigation {
            Some(Box::new(Tracing::new(nav)))
        } else {
            Some(Box::new(nav))
        }
    }

    #[cfg(not(feature = "ros"))]
    fn create_navigation_ros(&self) -> Option<Box<dyn Navigation>> {
        unreachable!()
    }

    fn create_navigation(
        &self,
        plugins: &mut PluginMap,
    ) -> Result<Option<Box<dyn Navigation>>, Error> {
        let (plugin_name, instance) = match &self.navigation {
            ClientKind::Auto(false) => return Ok(None),
            ClientKind::Auto(true) => {
                if self.ros_navigation_client_config.is_some() {
                    return Ok(self.create_navigation_ros());
                }
                match PluginConfig::resolve_instance(
                    &self.plugins,
                    None,
                    PluginInstanceKind::Navigation,
                ) {
                    Err(Error::NoPluginInstance { .. }) => {
                        // If ros is already used, it would *not* usually be
                        // assumed that urdf-viz would also be used.
                        // Users who want to use both at the same time need to
                        // specify it explicitly by `navigation = "urdf-viz"`.
                        if self.has_ros_clients() {
                            return Ok(None);
                        }
                        return Ok(Some(self.create_navigation_urdf_viz()));
                    }
                    res => res?,
                }
            }
            ClientKind::Builtin(BuiltinClient::Ros) => {
                return Ok(self.create_navigation_ros());
            }
            ClientKind::Builtin(BuiltinClient::UrdfViz) => {
                return Ok(Some(self.create_navigation_urdf_viz()));
            }
            ClientKind::Plugin(instance_name) => PluginConfig::resolve_instance(
                &self.plugins,
                Some(instance_name),
                PluginInstanceKind::Navigation,
            )?,
        };

        Ok(Some(Box::new(instance.create_lazy_instance(
            plugins,
            plugin_name,
            PluginProxy::new_navigation,
        )?)))
    }

    fn create_move_base_urdf_viz(&self) -> Box<dyn MoveBase> {
        let base = arci::Lazy::new(move || {
            debug!("create_move_base_urdf_viz: creating UrdfVizWebClient");
            let urdf_viz_client = UrdfVizWebClient::default();
            urdf_viz_client.run_send_velocity_thread();
            Ok(urdf_viz_client)
        });
        if self.openrr_tracing_config.move_base {
            Box::new(Tracing::new(base))
        } else {
            Box::new(base)
        }
    }

    #[cfg(feature = "ros")]
    fn create_move_base_ros(&self) -> Option<Box<dyn MoveBase>> {
        let topic = self
            .ros_cmd_vel_move_base_client_config
            .as_ref()?
            .topic
            .to_string();
        let base = arci::Lazy::new(move || {
            debug!("create_move_base_ros: creating RosCmdVelMoveBase");
            Ok(RosCmdVelMoveBase::new(&topic))
        });
        if self.openrr_tracing_config.move_base {
            Some(Box::new(Tracing::new(base)))
        } else {
            Some(Box::new(base))
        }
    }

    #[cfg(not(feature = "ros"))]
    fn create_move_base_ros(&self) -> Option<Box<dyn MoveBase>> {
        unreachable!()
    }

    fn create_move_base(
        &self,
        plugins: &mut PluginMap,
    ) -> Result<Option<Box<dyn MoveBase>>, Error> {
        let (plugin_name, instance) = match &self.move_base {
            ClientKind::Auto(false) => return Ok(None),
            ClientKind::Auto(true) => {
                if self.ros_cmd_vel_move_base_client_config.is_some() {
                    return Ok(self.create_move_base_ros());
                }
                match PluginConfig::resolve_instance(
                    &self.plugins,
                    None,
                    PluginInstanceKind::MoveBase,
                ) {
                    Err(Error::NoPluginInstance { .. }) => {
                        // If ros is already used, it would *not* usually be
                        // assumed that urdf-viz would also be used.
                        // Users who want to use both at the same time need to
                        // specify it explicitly by `move_base = "urdf-viz"`.
                        if self.has_ros_clients() {
                            return Ok(None);
                        }
                        return Ok(Some(self.create_move_base_urdf_viz()));
                    }
                    res => res?,
                }
            }
            ClientKind::Builtin(BuiltinClient::Ros) => {
                return Ok(self.create_move_base_ros());
            }
            ClientKind::Builtin(BuiltinClient::UrdfViz) => {
                return Ok(Some(self.create_move_base_urdf_viz()));
            }
            ClientKind::Plugin(instance_name) => PluginConfig::resolve_instance(
                &self.plugins,
                Some(instance_name),
                PluginInstanceKind::MoveBase,
            )?,
        };

        Ok(Some(Box::new(instance.create_lazy_instance(
            plugins,
            plugin_name,
            PluginProxy::new_move_base,
        )?)))
    }

    fn create_speakers(
        &self,
        plugins: &mut PluginMap,
    ) -> Result<HashMap<String, Arc<dyn Speaker>>, Error> {
        let mut speakers: HashMap<_, Arc<dyn Speaker>> = HashMap::new();
        for (name, speak_config) in self
            .speak_configs
            .iter()
            .filter(|(name, _)| self.speakers.as_ref().map_or(true, |v| v.contains(name)))
        {
            speakers.insert(name.to_owned(), speak_config.build()?.into());
        }

        for (plugin_name, config) in &self.plugins {
            for instance in config.instances.iter().filter(|instance| {
                instance.type_ == PluginInstanceKind::Speaker
                    && self
                        .speakers
                        .as_ref()
                        .map_or(true, |v| v.contains(&instance.name))
            }) {
                if speakers.contains_key(&instance.name) {
                    return Err(Error::DuplicateInstance(format!(
                        "Multiple {:?} instances {:?} are found. Consider renaming one of the instances",
                        instance.type_, instance.name,
                    )));
                }

                speakers.insert(
                    instance.name.clone(),
                    Arc::new(instance.create_lazy_instance(
                        plugins,
                        plugin_name,
                        PluginProxy::new_speaker,
                    )?),
                );
            }
        }

        if self.speakers.is_none() && speakers.is_empty() {
            speakers.insert(
                Self::DEFAULT_SPEAKER_NAME.to_owned(),
                SpeakConfig::default().build()?.into(),
            );
        }
        Ok(speakers)
    }

    fn create_raw_joint_trajectory_clients(
        &self,
        plugins: &mut PluginMap,
    ) -> Result<HashMap<String, Arc<dyn JointTrajectoryClient>>, Error> {
        // True if the name is included in the list of clients to use
        // or if no list is specified.
        let is_used = |client_name| {
            self.joint_trajectory_clients
                .as_ref()
                .map_or(true, |v| v.contains(client_name))
        };
        let urdf_viz_clients_configs: Vec<_> = self
            .urdf_viz_clients_configs
            .iter()
            .filter(|c| is_used(&c.name))
            .cloned()
            .collect();
        #[cfg(feature = "ros")]
        let ros_clients_configs: Vec<_> = self
            .ros_clients_configs
            .iter()
            .filter(|c| is_used(&c.name))
            .collect();
        #[cfg(feature = "ros")]
        let ros_action_clients_configs: Vec<_> = self
            .ros_action_clients_configs
            .iter()
            .filter(|c| is_used(&c.name))
            .collect();

        let mut urdf_robot = None;
        #[cfg(not(feature = "ros"))]
        let use_urdf = !urdf_viz_clients_configs.is_empty();
        #[cfg(feature = "ros")]
        let use_urdf = !urdf_viz_clients_configs.is_empty()
            || !ros_clients_configs.is_empty()
            || !ros_action_clients_configs.is_empty();
        if use_urdf {
            if let Some(urdf_path) = self.openrr_clients_config.urdf_full_path() {
                urdf_robot = Some(urdf_rs::utils::read_urdf_or_xacro(urdf_path)?);
            }
        }

        let mut clients = arci_urdf_viz::create_joint_trajectory_clients_lazy(
            urdf_viz_clients_configs,
            urdf_robot.as_ref(),
        )?;
        #[cfg(feature = "ros")]
        {
            clients.extend(arci_ros::create_joint_trajectory_clients_lazy(
                ros_clients_configs,
                urdf_robot.as_ref(),
            )?);
            clients.extend(arci_ros::create_joint_trajectory_clients_lazy(
                ros_action_clients_configs,
                urdf_robot.as_ref(),
            )?);
        }

        for (plugin_name, config) in &self.plugins {
            for instance in config.instances.iter().filter(|instance| {
                instance.type_ == PluginInstanceKind::JointTrajectoryClient
                    && self
                        .joint_trajectory_clients
                        .as_ref()
                        .map_or(true, |v| v.contains(&instance.name))
            }) {
                if clients.contains_key(&instance.name) {
                    return Err(Error::DuplicateInstance(format!(
                        "Multiple {:?} instances {:?} are found. Consider renaming one of the instances",
                        instance.type_, instance.name,
                    )));
                }

                let client = instance.create_lazy_instance(
                    plugins,
                    plugin_name,
                    PluginProxy::new_joint_trajectory_client,
                )?;
                // If the `PluginProxy::new_joint_trajectory_client` returns
                // `Err` or `None`, `JointTrajectoryClient::joint_names` will
                // panic. Therefore, initialize it here to allow the user to
                // handle error.
                // `JointTrajectoryClientsContainer::new`, which is called inside
                // `RobotClient::new`, calls `JointTrajectoryClient::joint_names`,
                // so it makes no sense to make `JointTrajectoryClient` lazy here.
                client.get_ref()?;
                clients.insert(instance.name.clone(), Arc::new(client));
            }
        }

        Ok(clients)
    }
}

/// Convert relative path into absolute one
#[allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/13170
fn resolve_audio_file_path<P: AsRef<Path>>(
    base_path: P,
    relative_hash_map: &mut HashMap<String, PathBuf>,
) -> Result<(), Error> {
    for v in relative_hash_map.values_mut() {
        let full_path = openrr_client::resolve_relative_path(base_path.as_ref(), &v)?;
        *v = full_path;
    }
    Ok(())
}

fn instance_create_error<T: fmt::Debug, U>(
    res: Result<T, arci::Error>,
    instance_kind: PluginInstanceKind,
    instance_name: String,
    plugin_name: String,
) -> Result<U, arci::Error> {
    error!(
        "failed to create `{instance_kind:?}` instance `{instance_name}` from plugin `{plugin_name}`: {res:?}",
    );
    res.and_then(|_| {
        // TODO: error msg
        Err(format_err!(
            "failed to create `{instance_kind:?}` instance `{instance_name}` from plugin `{plugin_name}`: None",
        )
        .into())
    })
}

impl RobotConfig {
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
        Self::from_str(
            &std::fs::read_to_string(&path)
                .map_err(|e| Error::NoFile(path.as_ref().to_owned(), e))?,
            &path,
        )
    }

    pub fn from_str<P: AsRef<Path>>(s: &str, path: P) -> Result<Self, Error> {
        let path = path.as_ref();

        let mut config: RobotConfig =
            toml::from_str(s).map_err(|e| Error::TomlParseFailure(path.to_owned(), e))?;

        config.validate_ros_config()?;

        if config.openrr_clients_config.urdf_path.is_some() {
            config.openrr_clients_config.resolve_path(path)?;
        }
        for speak_config in config.speak_configs.values_mut() {
            if let SpeakConfig::Audio { map } = speak_config {
                resolve_audio_file_path(path, map)?;
            }
        }
        for plugin_config in config.plugins.values_mut() {
            resolve_plugin_path(&mut plugin_config.path, path)?;
            for instance in &mut plugin_config.instances {
                if let Some(args_path) = instance.args_from_path.take() {
                    instance.args_from_path =
                        Some(openrr_client::resolve_relative_path(path, args_path)?);
                }
            }
        }
        debug!("{config:?}");
        Ok(config)
    }

    /// Returns an error if a config requires ros feature but ros feature is disabled.
    fn validate_ros_config(&self) -> Result<(), Error> {
        if cfg!(feature = "ros") {
            return Ok(());
        }

        for (name, speak_config) in &self.speak_configs {
            if matches!(speak_config, SpeakConfig::RosEspeak { .. }) {
                return Err(Error::ConfigRequireRos(format!("speak_configs.{name}")));
            }
        }
        if !self.ros_clients_configs.is_empty() {
            return Err(Error::ConfigRequireRos("ros_clients_configs".into()));
        }
        if !self.ros_action_clients_configs.is_empty() {
            return Err(Error::ConfigRequireRos("ros_action_clients_configs".into()));
        }
        match self.move_base {
            ClientKind::Builtin(BuiltinClient::Ros) => {
                return Err(Error::ConfigRequireRos("move_base".into()));
            }
            ClientKind::Auto(true) if self.ros_cmd_vel_move_base_client_config.is_some() => {
                return Err(Error::ConfigRequireRos(
                    "ros_cmd_vel_move_base_client_config".into(),
                ));
            }
            _ => {}
        }
        match self.navigation {
            ClientKind::Builtin(BuiltinClient::Ros) => {
                return Err(Error::ConfigRequireRos("navigation".into()));
            }
            ClientKind::Auto(true) if self.ros_navigation_client_config.is_some() => {
                return Err(Error::ConfigRequireRos(
                    "ros_navigation_client_config".into(),
                ));
            }
            _ => {}
        }
        match self.localization {
            ClientKind::Builtin(BuiltinClient::Ros) => {
                return Err(Error::ConfigRequireRos("localization".into()));
            }
            ClientKind::Auto(true) if self.ros_localization_client_config.is_some() => {
                return Err(Error::ConfigRequireRos(
                    "ros_localization_client_config".into(),
                ));
            }
            _ => {}
        }
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_resolve_audio_file_path() {
        let mut hash = HashMap::new();
        hash.insert("a".to_owned(), PathBuf::from("dir1/file.mp3"));
        hash.insert("b".to_owned(), PathBuf::from("../dir2/file.mp3"));
        resolve_audio_file_path("/config/some_file.toml", &mut hash).unwrap();
        assert_eq!(hash.len(), 2);
        assert_eq!(hash["a"], PathBuf::from("/config/dir1/file.mp3"));
        assert_eq!(hash["b"], PathBuf::from("/config/../dir2/file.mp3"));
    }
}