pub struct TypedPath {
pub link_type: ScopedLinkType,
pub path: Path,
}
Expand description
Fields§
§link_type: ScopedLinkType
The LinkType
within the scope of the zome where it’s defined.
path: Path
Implementations§
source§impl TypedPath
impl TypedPath
sourcepub fn new(link_type: impl Into<ScopedLinkType>, path: Path) -> Self
pub fn new(link_type: impl Into<ScopedLinkType>, path: Path) -> Self
Examples found in repository?
264 265 266 267 268 269 270 271 272 273 274 275 276
pub fn into_typed(self, link_type: impl Into<ScopedLinkType>) -> TypedPath {
TypedPath::new(link_type, self)
}
/// Try attaching a [`LinkType`] to this path
/// so its type is known for [`create_link`] and [`get_links`].
pub fn typed<TY, E>(self, link_type: TY) -> Result<TypedPath, WasmError>
where
ScopedLinkType: TryFrom<TY, Error = E>,
WasmError: From<E>,
{
Ok(TypedPath::new(ScopedLinkType::try_from(link_type)?, self))
}
sourcepub fn exists(&self) -> ExternResult<bool>
pub fn exists(&self) -> ExternResult<bool>
Does an entry exist at the hash we expect?
Examples found in repository?
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
pub fn ensure(&self) -> ExternResult<()> {
if !self.exists()? {
if self.is_root() {
create_link(
root_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
} else if let Some(parent) = self.parent() {
parent.ensure()?;
create_link(
parent.path_entry_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
}
}
Ok(())
}
sourcepub fn ensure(&self) -> ExternResult<()>
pub fn ensure(&self) -> ExternResult<()>
Recursively touch this and every parent that doesn’t exist yet.
Examples found in repository?
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
pub fn anchor<T, E>(
link_type: T,
anchor_type: String,
anchor_text: String,
) -> ExternResult<holo_hash::EntryHash>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let path: Path = (&Anchor {
anchor_type,
anchor_text: Some(anchor_text),
})
.into();
let path = path.typed(link_type)?;
path.ensure()?;
path.path_entry_hash()
}
/// Returns every entry hash in a vector from the root of an anchor.
/// Hashes are sorted in the same way that paths sort children.
pub fn list_anchor_type_addresses<T, E>(link_type: T) -> ExternResult<Vec<AnyLinkableHash>>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let links = Path::from(vec![Component::new(ROOT.to_vec())])
.typed(link_type)?
.children()?
.into_iter()
.map(|link| link.target)
.collect();
Ok(links)
}
/// Returns every entry hash in a vector from the second level of an anchor.
/// Uses the string argument to build the path from the root.
/// Hashes are sorted in the same way that paths sort children.
pub fn list_anchor_addresses<T, E>(
link_type: T,
anchor_type: String,
) -> ExternResult<Vec<AnyLinkableHash>>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let path: Path = (&Anchor {
anchor_type,
anchor_text: None,
})
.into();
let links = path
.typed(link_type)?
.children()?
.into_iter()
.map(|link| link.target)
.collect();
Ok(links)
}
/// Old version of holochain that anchors was designed for had two part link tags but now link
/// tags are a single array of bytes, so to get an external interface that is somewhat backwards
/// compatible we need to rebuild the anchors from the paths serialized into the links and then
/// return them.
pub fn list_anchor_tags<T, E>(link_type: T, anchor_type: String) -> ExternResult<Vec<String>>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let path: Path = (&Anchor {
anchor_type,
anchor_text: None,
})
.into();
let path = path.typed(link_type)?;
path.ensure()?;
let hopefully_anchor_tags: Result<Vec<String>, WasmError> = path
.children_paths()?
.into_iter()
.map(|path| match Anchor::try_from(&path.path) {
Ok(anchor) => match anchor.anchor_text {
Some(text) => Ok(text),
None => Err(wasm_error!(WasmErrorInner::Serialize(
SerializedBytesError::Deserialize("missing anchor text".into(),)
))),
},
Err(e) => Err(e),
})
.collect();
let mut anchor_tags = hopefully_anchor_tags?;
anchor_tags.sort();
anchor_tags.dedup();
Ok(anchor_tags)
}
More examples
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
pub fn ensure(&self) -> ExternResult<()> {
if !self.exists()? {
if self.is_root() {
create_link(
root_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
} else if let Some(parent) = self.parent() {
parent.ensure()?;
create_link(
parent.path_entry_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
}
}
Ok(())
}
/// The parent of the current path is simply the path truncated one level.
pub fn parent(&self) -> Option<Self> {
if self.path.as_ref().len() > 1 {
let parent_vec: Vec<Component> =
self.path.as_ref()[0..self.path.as_ref().len() - 1].to_vec();
Some(Path::from(parent_vec).into_typed(self.link_type))
} else {
None
}
}
/// Touch and list all the links from this path to paths below it.
/// Only returns links between paths, not to other entries that might have their own links.
pub fn children(&self) -> ExternResult<Vec<holochain_zome_types::link::Link>> {
Self::ensure(self)?;
let mut unwrapped = get_links(
self.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
None,
)?;
// Only need one of each hash to build the tree.
unwrapped.sort_unstable_by(|a, b| a.tag.cmp(&b.tag));
unwrapped.dedup_by(|a, b| a.tag.eq(&b.tag));
Ok(unwrapped)
}
/// Touch and list all the links from this path to paths below it.
/// Same as `Path::children` but returns `Vec<Path>` rather than `Vec<Link>`.
/// This is more than just a convenience. In general it's not possible to
/// construct a full `Path` from a child `Link` alone as only a single
/// `Component` is encoded into the link tag. To build a full child path
/// the parent path + child link must be combined, which this function does
/// to produce each child, by using `&self` as that parent.
pub fn children_paths(&self) -> ExternResult<Vec<Self>> {
let children = self.children()?;
let components: ExternResult<Vec<Option<Component>>> = children
.into_iter()
.map(|link| {
let component_bytes = &link.tag.0[..];
if component_bytes.is_empty() {
Ok(None)
} else {
Ok(Some(
SerializedBytes::from(UnsafeBytes::from(component_bytes.to_vec()))
.try_into()
.map_err(|e: SerializedBytesError| wasm_error!(e))?,
))
}
})
.collect();
Ok(components?
.into_iter()
.map(|maybe_component| {
let mut new_path = self.path.clone();
if let Some(component) = maybe_component {
new_path.append_component(component);
}
new_path.into_typed(self.link_type)
})
.collect())
}
pub fn children_details(&self) -> ExternResult<holochain_zome_types::link::LinkDetails> {
Self::ensure(self)?;
get_link_details(
self.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(holochain_zome_types::link::LinkTag::new([])),
)
}
sourcepub fn parent(&self) -> Option<Self>
pub fn parent(&self) -> Option<Self>
The parent of the current path is simply the path truncated one level.
Examples found in repository?
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
pub fn exists(&self) -> ExternResult<bool> {
if self.0.is_empty() {
Ok(false)
} else if self.is_root() {
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
root_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
} else {
let parent = self
.parent()
.expect("Must have parent if not empty or root");
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
parent.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
}
}
/// Recursively touch this and every parent that doesn't exist yet.
pub fn ensure(&self) -> ExternResult<()> {
if !self.exists()? {
if self.is_root() {
create_link(
root_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
} else if let Some(parent) = self.parent() {
parent.ensure()?;
create_link(
parent.path_entry_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
}
}
Ok(())
}
sourcepub fn children(&self) -> ExternResult<Vec<Link>>
pub fn children(&self) -> ExternResult<Vec<Link>>
Touch and list all the links from this path to paths below it. Only returns links between paths, not to other entries that might have their own links.
Examples found in repository?
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
pub fn list_anchor_type_addresses<T, E>(link_type: T) -> ExternResult<Vec<AnyLinkableHash>>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let links = Path::from(vec![Component::new(ROOT.to_vec())])
.typed(link_type)?
.children()?
.into_iter()
.map(|link| link.target)
.collect();
Ok(links)
}
/// Returns every entry hash in a vector from the second level of an anchor.
/// Uses the string argument to build the path from the root.
/// Hashes are sorted in the same way that paths sort children.
pub fn list_anchor_addresses<T, E>(
link_type: T,
anchor_type: String,
) -> ExternResult<Vec<AnyLinkableHash>>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let path: Path = (&Anchor {
anchor_type,
anchor_text: None,
})
.into();
let links = path
.typed(link_type)?
.children()?
.into_iter()
.map(|link| link.target)
.collect();
Ok(links)
}
More examples
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
pub fn children_paths(&self) -> ExternResult<Vec<Self>> {
let children = self.children()?;
let components: ExternResult<Vec<Option<Component>>> = children
.into_iter()
.map(|link| {
let component_bytes = &link.tag.0[..];
if component_bytes.is_empty() {
Ok(None)
} else {
Ok(Some(
SerializedBytes::from(UnsafeBytes::from(component_bytes.to_vec()))
.try_into()
.map_err(|e: SerializedBytesError| wasm_error!(e))?,
))
}
})
.collect();
Ok(components?
.into_iter()
.map(|maybe_component| {
let mut new_path = self.path.clone();
if let Some(component) = maybe_component {
new_path.append_component(component);
}
new_path.into_typed(self.link_type)
})
.collect())
}
sourcepub fn children_paths(&self) -> ExternResult<Vec<Self>>
pub fn children_paths(&self) -> ExternResult<Vec<Self>>
Touch and list all the links from this path to paths below it.
Same as Path::children
but returns Vec<Path>
rather than Vec<Link>
.
This is more than just a convenience. In general it’s not possible to
construct a full Path
from a child Link
alone as only a single
Component
is encoded into the link tag. To build a full child path
the parent path + child link must be combined, which this function does
to produce each child, by using &self
as that parent.
Examples found in repository?
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
pub fn list_anchor_tags<T, E>(link_type: T, anchor_type: String) -> ExternResult<Vec<String>>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let path: Path = (&Anchor {
anchor_type,
anchor_text: None,
})
.into();
let path = path.typed(link_type)?;
path.ensure()?;
let hopefully_anchor_tags: Result<Vec<String>, WasmError> = path
.children_paths()?
.into_iter()
.map(|path| match Anchor::try_from(&path.path) {
Ok(anchor) => match anchor.anchor_text {
Some(text) => Ok(text),
None => Err(wasm_error!(WasmErrorInner::Serialize(
SerializedBytesError::Deserialize("missing anchor text".into(),)
))),
},
Err(e) => Err(e),
})
.collect();
let mut anchor_tags = hopefully_anchor_tags?;
anchor_tags.sort();
anchor_tags.dedup();
Ok(anchor_tags)
}
pub fn children_details(&self) -> ExternResult<LinkDetails>
Methods from Deref<Target = Path>§
sourcepub fn path_entry_hash(&self) -> ExternResult<EntryHash>
pub fn path_entry_hash(&self) -> ExternResult<EntryHash>
What is the hash for the current Path
?
Examples found in repository?
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
pub fn anchor<T, E>(
link_type: T,
anchor_type: String,
anchor_text: String,
) -> ExternResult<holo_hash::EntryHash>
where
ScopedLinkType: TryFrom<T, Error = E>,
WasmError: From<E>,
{
let path: Path = (&Anchor {
anchor_type,
anchor_text: Some(anchor_text),
})
.into();
let path = path.typed(link_type)?;
path.ensure()?;
path.path_entry_hash()
}
More examples
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
pub fn exists(&self) -> ExternResult<bool> {
if self.0.is_empty() {
Ok(false)
} else if self.is_root() {
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
root_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
} else {
let parent = self
.parent()
.expect("Must have parent if not empty or root");
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
parent.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
}
}
/// Recursively touch this and every parent that doesn't exist yet.
pub fn ensure(&self) -> ExternResult<()> {
if !self.exists()? {
if self.is_root() {
create_link(
root_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
} else if let Some(parent) = self.parent() {
parent.ensure()?;
create_link(
parent.path_entry_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
}
}
Ok(())
}
/// The parent of the current path is simply the path truncated one level.
pub fn parent(&self) -> Option<Self> {
if self.path.as_ref().len() > 1 {
let parent_vec: Vec<Component> =
self.path.as_ref()[0..self.path.as_ref().len() - 1].to_vec();
Some(Path::from(parent_vec).into_typed(self.link_type))
} else {
None
}
}
/// Touch and list all the links from this path to paths below it.
/// Only returns links between paths, not to other entries that might have their own links.
pub fn children(&self) -> ExternResult<Vec<holochain_zome_types::link::Link>> {
Self::ensure(self)?;
let mut unwrapped = get_links(
self.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
None,
)?;
// Only need one of each hash to build the tree.
unwrapped.sort_unstable_by(|a, b| a.tag.cmp(&b.tag));
unwrapped.dedup_by(|a, b| a.tag.eq(&b.tag));
Ok(unwrapped)
}
/// Touch and list all the links from this path to paths below it.
/// Same as `Path::children` but returns `Vec<Path>` rather than `Vec<Link>`.
/// This is more than just a convenience. In general it's not possible to
/// construct a full `Path` from a child `Link` alone as only a single
/// `Component` is encoded into the link tag. To build a full child path
/// the parent path + child link must be combined, which this function does
/// to produce each child, by using `&self` as that parent.
pub fn children_paths(&self) -> ExternResult<Vec<Self>> {
let children = self.children()?;
let components: ExternResult<Vec<Option<Component>>> = children
.into_iter()
.map(|link| {
let component_bytes = &link.tag.0[..];
if component_bytes.is_empty() {
Ok(None)
} else {
Ok(Some(
SerializedBytes::from(UnsafeBytes::from(component_bytes.to_vec()))
.try_into()
.map_err(|e: SerializedBytesError| wasm_error!(e))?,
))
}
})
.collect();
Ok(components?
.into_iter()
.map(|maybe_component| {
let mut new_path = self.path.clone();
if let Some(component) = maybe_component {
new_path.append_component(component);
}
new_path.into_typed(self.link_type)
})
.collect())
}
pub fn children_details(&self) -> ExternResult<holochain_zome_types::link::LinkDetails> {
Self::ensure(self)?;
get_link_details(
self.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(holochain_zome_types::link::LinkTag::new([])),
)
}
sourcepub fn leaf(&self) -> Option<&Component>
pub fn leaf(&self) -> Option<&Component>
Accessor for the last Component
of this Path
.
This can be thought of as the leaf of the implied tree structure of
which this Path
is one branch of.
sourcepub fn make_tag(&self) -> ExternResult<LinkTag>
pub fn make_tag(&self) -> ExternResult<LinkTag>
Examples found in repository?
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
pub fn exists(&self) -> ExternResult<bool> {
if self.0.is_empty() {
Ok(false)
} else if self.is_root() {
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
root_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
} else {
let parent = self
.parent()
.expect("Must have parent if not empty or root");
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
parent.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
}
}
/// Recursively touch this and every parent that doesn't exist yet.
pub fn ensure(&self) -> ExternResult<()> {
if !self.exists()? {
if self.is_root() {
create_link(
root_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
} else if let Some(parent) = self.parent() {
parent.ensure()?;
create_link(
parent.path_entry_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
}
}
Ok(())
}
sourcepub fn is_root(&self) -> bool
pub fn is_root(&self) -> bool
Check if this Path
is the root.
Examples found in repository?
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
pub fn exists(&self) -> ExternResult<bool> {
if self.0.is_empty() {
Ok(false)
} else if self.is_root() {
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
root_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
} else {
let parent = self
.parent()
.expect("Must have parent if not empty or root");
let this_paths_hash: AnyLinkableHash = self.path_entry_hash()?.into();
let exists = get_links(
parent.path_entry_hash()?,
LinkTypeFilter::single_type(self.link_type.zome_index, self.link_type.zome_type),
Some(self.make_tag()?),
)?
.iter()
.any(|Link { target, .. }| *target == this_paths_hash);
Ok(exists)
}
}
/// Recursively touch this and every parent that doesn't exist yet.
pub fn ensure(&self) -> ExternResult<()> {
if !self.exists()? {
if self.is_root() {
create_link(
root_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
} else if let Some(parent) = self.parent() {
parent.ensure()?;
create_link(
parent.path_entry_hash()?,
self.path_entry_hash()?,
self.link_type,
self.make_tag()?,
)?;
}
}
Ok(())
}