nu_plugin_engine/
source.rs1use super::GetPlugin;
2use nu_protocol::{PluginIdentity, ShellError, Span};
3use std::sync::{Arc, Weak};
4
5#[derive(Debug, Clone)]
8pub struct PluginSource {
9 pub(crate) identity: Arc<PluginIdentity>,
11 pub(crate) persistent: Weak<dyn GetPlugin>,
16}
17
18impl PluginSource {
19 pub fn new(plugin: Arc<dyn GetPlugin>) -> PluginSource {
21 PluginSource {
22 identity: plugin.identity().clone().into(),
23 persistent: Arc::downgrade(&plugin),
24 }
25 }
26
27 pub fn new_fake(name: &str) -> PluginSource {
31 PluginSource {
32 identity: PluginIdentity::new_fake(name).into(),
33 persistent: Weak::<crate::PersistentPlugin>::new(),
34 }
35 }
36
37 pub fn persistent(&self, span: Option<Span>) -> Result<Arc<dyn GetPlugin>, ShellError> {
40 self.persistent
41 .upgrade()
42 .ok_or_else(|| ShellError::GenericError {
43 error: format!("The `{}` plugin is no longer present", self.identity.name()),
44 msg: "removed since this object was created".into(),
45 span,
46 help: Some("try recreating the object that came from the plugin".into()),
47 inner: vec![],
48 })
49 }
50
51 pub(crate) fn is_compatible(&self, other: &PluginSource) -> bool {
53 self.identity == other.identity
54 }
55}
56
57impl std::ops::Deref for PluginSource {
58 type Target = PluginIdentity;
59
60 fn deref(&self) -> &PluginIdentity {
61 &self.identity
62 }
63}